Introduction to cgi advanced programming in perl

Source: Internet
Author: User
Tags html comment html header

A routines call in CGI. pm

1. CGI. pm implements two usage methods: Object-oriented method and traditional perlmodule method.
Object-Oriented Approach:

Copy codeThe Code is as follows :#! /Usr/local/bin/perl-w
Use CGI; # load CGI routines
$ Q = CGI-> new; # create new CGI object
Print $ q-> header, # create the HTTP header
$ Q-> start_html ('Hello World'), # start the HTML
$ Q-> h1 ('Hello World'), # level 1 header
$ Q-> end_html; # end the HTML

The traditional module method:

Copy codeThe Code is as follows :#! /Usr/local/bin/perl
Use CGI qw/: standard/; # load standard CGI routines
Print header, # create the HTTP header
Start_html ('Hello World'), # start the HTML
H1 ('Hello World'), # level 1 header
End_html; # end the HTML

2. Methods in CGI. pm.

Methods in CGI. pm usually have many parameters, So we generally call them using named parameters, for example:Copy codeThe Code is as follows: print $ q-> header (-type => 'image/gif',-expires => '+ 3d ');

The value of the named parameter can be of the scalar or array reference type, for example:Copy codeThe Code is as follows: $ q-> param (-name => 'veggi',-value => 'tomato ');
$ Q-> param (-name => 'veggie',-value => ['tomato', 'tomahto', 'potato ', 'potahto']);

3. the html element (html shortcuts) method in CGI. pm

All html elements (such as h1 and br) are stored in CGI. pm has corresponding methods. These methods are dynamically generated as needed and contain two parameters. The first parameter is of the hash type and corresponds to the attributes of html elements, the string type of the second parameter, corresponding to the content of the html element. For example, the method corresponding to h1 in html is h1 ():
Code Generated HTML
------------------
H1 () H1 ('some', 'tents'); H1 ({-align => left});

H1 ({-align => left}, 'contents');

If you want to process the start and end of an element, you can use start_tag_name and end_tag_name, for example
Print start_h1, 'level 1 header', end_h1;
Sometimes the start and end methods are not automatically generated and need to be specified. For example:
Use CGI qw/: standard * table start_ul /;
Used to automatically generate start_table, end_table, start_ul, and end_ul methods.

Another instance:

Copy codeThe Code is as follows: print a ({-href => 'fred.html ',-target =>' _ new'}, "Open a new frame ");
<A href = "fred.html", target = "_ new"> Open a new frame </a>

2. Obtain CGI parameters in cgi. pm

@ Names = $ query-> param # get all params
@ Values = $ query-> param ('foo'); # get param foo as list
$ Value = $ query-> param ('foo'); # get param foo as scalar
The result obtained by param () can be of the scalar or array type. For example, if the result of a parameter comes from the multiple-choice scrollinglist, it is of the array type. If the parameter value is not specified in querystring ("name1 = & name2 ="), param () returns emptystring. If the parameter does not exist in querystring, param () returns undef or emptylist. When there are multiple parameters, the format of querystring is var1 = value1 & var1 = value2 & var1 = value3.

Three headers and start_html
1. the header specifies the html header, for example

Copy codeThe Code is as follows: print header; # The default type: text/html is returned.
Print header ('image/gif '); # Set the type to image/gif.
Print header ('text/html ', '2014 No response ');
$ Cookie1 = $ q-> cookie (-name => 'maid _ name',-value => "The Sphynx's Question ");
$ Cookie2 = $ q-> cookie (-name => 'answers',-value => \ % answers );
Print header (-type => 'image/gif ',
-Nph => 1,
-Status => '2017 Payment required ',
-Expires => '+ 3d ',
-Cookie => [$ cookie1, $ cookie2],
-Charset => 'utf-7 ',
-Attachment => 'foo.gif ',
-Cost => '$2.00 ');

Among them,-type,-status,-expires,-cookie is a parameter that can be set, and other naming parameters are converted to the html header attribute.
-The expires value can be:
+ 30 s 30 seconds from now
+ 10 m ten minutes from now
+ 1 h one hour from now
-1d yesterday (I. e. "ASAP! ")
Now immediately
+ 3 M in three months
+ 10y in ten years time
Thursday, 25-Apr-1999 00:40:33 GMT at the indicated time & date
2. start_html: top-level elements of the page For example:

Copy codeThe Code is as follows: print start_html (-title => 'crets of the pyramkids ',
-Author => 'fred @ jbxue.org ',
-Base => 'true ',
-Target => '_ blank ',
-Meta => {'keyword' => 'pharaoh secret mummy ',
'Copyright' => 'copyright 1996 King tut '},
-Style => {'src' => '/styles/style1.css '},
-BGCOLOR => 'Blue ');

Or:

Copy codeThe Code is as follows: print start_html (-head => [
Link ({-rel => 'shortcut icon ', href => 'favicon. ico '}),
Meta ({-http_equiv => 'content-type',-Content => 'text/html '})
]
);

Example of adding javascript to the header:

Copy codeThe Code is as follows: $ query = CGI-> new;
Print header;
$ JSCRIPT = <END;
// Ask a silly question
Function riddle_me_this (){
Var r = prompt ("What walks on four legs in the morning," +
"Two legs in the afternoon," +
"And three legs in the evening? ");
Response (r );
}
// Get a silly answer
Function response (answer ){
If (answer = "man ")
Alert ("Right you are! ");
Else
Alert ("Wrong! Guess again .");
}
END
Print start_html (-title => The Riddle of the Sphinx ',
-Script => $ JSCRIPT );
Print $ q-> start_html (-title => The Riddle of the Sphinx ',
-Script =>{-type => 'javascript ',
-Src => '/javascript/sphpipeline. js '}
);
Print $ q-> start_html (-title => The Riddle of the Sphinx ',
-Script => [
{-Type => 'text/javascript ',
-Src => '/javascript/utilities10.js'
},
{-Type => 'text/javascript ',
-Src => '/javascript/utilities11.js'
},
{-Type => 'text/jscript ',
-Src => '/javascript/utilities12.js'
},
{-Type => 'text/ecmascript ',
-Src => '/javascript/utilities219.js'
}
]
);

Example of using css in header:Copy codeThe Code is as follows: use CGI qw/: standard: html3 /;
# Here's a stylesheet inreceivated directly into the page
$ NewStyle = <END;
<! --
P. Tip {
Margin-right: 50pt;
Margin-left: 50pt;
Color: red;
}
P. Alert {
Font-size: 30pt;
Font-family: sans-serif;
Color: red;
}
-->
END
Print header ();
Print start_html (-title => 'cgi with style ',
-Style => {-src => 'HTTP: // www.jbxue.com/style/st1.css ',
-Code => $ newStyle}
);
Print h1 ('cgi with style '),
P ({-class => 'tip '},
"Better read the cascading style sheet spec before playing with this! "),
Span ({-style => 'color: magenta '},
"Look Mom, no hands! ",
P (),
"Whooo wee! "
);
Print end_html;

Four URLs

Copy codeCode: $ full_url = url (); # http://your.host.com/path/to/script.cgi
$ Full_url = url (-full => 1); # http://your.host.com/path/to/script.cgi
$ Relative_url = url (-relative => 1); # script. cgi
$ Absolute_url = url (-absolute => 1); # path/to/script. cgi
$ Url_with_path = url (-path_info => 1 );
$ Url_with_path_and_query = url (-path_info => 1,-query => 1 );
$ Netloc = url (-base => 1); # http://your.host.com

5. Special Use of the html Element Method in CGI. pm

If the second parameter of an element is of the list type, it is decomposed. For example:

Copy codeThe Code is as follows: print ul (
Li ({-type => 'disc'}, ['sneezy', 'Doc', 'sleepy', 'happy '])
);

Equivalent:
<Ul>
<Li type = "disc"> Sneezy </li>
<Li type = "disc"> Doc </li>
<Li type = "disc"> Sleepy </li>
<Li type = "disc"> Happy </li>
</Ul>
For example, table can be written as follows:Copy codeThe Code is as follows: print table ({-border => undef },
Caption ('when shocould You Eat Your Vegetables? '),
Tr ({-align => 'center',-valign => 'top '},
[
Th (['vegetable', 'Breakfast ', 'lunch', 'dinner']),
Td (['tomates', 'No', 'yes', 'yes']),
Td (['broccol', 'No', 'No', 'yes']),
Td (['onons', 'yes'])
]
)
);

6. Non-standard html Element Methods in CGI. pm

Print comment ('here is my comment'); # generates an HTML comment (<! -- Comment -->)
Because it conflicts with the perl method, it is capitalized:
Select
Tr
Link
Delete
Accept
Sub
Other special html Element Methods: start_html (), end_html (), start_form (), end_form (), start_multipart_form () and all the fill-out form tags.

7. form-related in CGI. pm

1 start_form and start_multipart_form

Copy codeThe Code is as follows: print start_form (-method => $ method,
-Action => $ action,
-Enctype => $ encoding );
<... Various form stuff...>
Print end_form;
-Or-
Print start_form ($ method, $ action, $ encoding );
<... Various form stuff...>
Print end_form;

If method, action, and enctype are not specified, the default value is:
Method: POST
Action: this script
Enctype: application/x-www-form-urlencoded for non-XHTML
Multipart/form-data for XHTML, see multipart/form-data below.
When start_form is used, enctype is application/x-www-form-urlencoded. If new xhtml is required, start_multipart_form is used. In this case, enctype is multipart/form-data.

For more information, see cgi man page http://search.cpan.org /~ Markstos/CGI. pm-3.60/lib/CGI. pm

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.