Perl-CGI advanced

Source: Internet
Author: User
Tags html comment html header
A routines method in CGI. PM calls 1. cgi. PM to implement two usage methods: Object-oriented method and traditional perlmodule method. Object-oriented Method :#! /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 traditional module method :#! /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. CGI. PM method. CGI. methods in PM usually have many parameters, So we generally call them using named parameters, for example, Print $ q-> header (-type => 'image/gi ', -expires => '+ 3D'); The name parameter value can be of the scalar or array reference type, for example, $ q-> param (-name => 'veggi ', -value => 'tomato'); $ q-> param (-name => 'veggie',-value => ['tomato', 'tomahto', 'potato ', 'potahto']); 3. CGI. all HTML elements (such as H1 and BR) of the HTML element (HTML shortcuts) method in PM 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 Content. For example, the method corresponding to H1 in HTML is H1 (): code generated HTML ---- ------------ H1 ()

 

Ii. cgi. obtain CGI parameters in 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 param () The result of obtaining the parameter 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. header specifies the HTML header, such as print header; # Return the default type: text/html print header ('image/gif'); # Set the type: image/GIF print header ('text/html', '2014 no response'); $ cookie1 = $ q-> cookie (-name => 'ddle _ name ', -value => "The sphynx's question"); $ cookie2 = $ q-> cookie (-name => 'answers',-value => \ % answers ); print header (-type => 'image/gif',-nph => 1,-status => '2018 payment required',-expires =>' + 3D ',-Cookie => [$ cookie1, $ cookie2],-charset => 'utf-7',-attachment => 'foo.gif ', -Cost => '$2.00'), where-type,-status,-expires,-cookie is a parameter that can be set, and other named 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 <HTML>

 

Four URLs $ 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

 

V. cgi. special Use of the HTML Element Method in PM if the second parameter of an element is of the list type, it will be decomposed, for example, 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: 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 (['onyuns', 'yes']);

6. Print comment ('here is my comment'); # generates an HTML comment (<! -- Comment -->) because it conflicts with the Perl method, the following special HTML element methods are capitalized: Select tr link Delete accept Sub: start_html (), end_html (), start_form (), end_form (), start_multipart_form () and all the fill-out form tags. VII. cgi. 1 start_form and start_multipart_form print start_form (-method => $ method,-action => $ action,-enctype => $ encoding) related to form in PM; <... 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

 

Complete!
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.