Home Page
make a choice: Photoshop 7 or Photoshop CS/CS 2
Uncle Gugl - a giant
How to name a Web-site?
Spam - a problem of a century
register_globals=oN? You in danger!
Gathering of statistics on PHP
Transformation XML + XSLT with help Sablotron
Patterns of documents and Perl
The signature or avatars on pkhp
Privatnost` in the Internet
Use of the module for job with patterns
Alternative MIME:: Parser and Email:: Simple
Job with sessions in perl
Creation of dynamic forms with help JavaScript
*.JS when to be loaded you think?
Module CGI.pm
XML in 10 theses
Platformo-independent dynamic site - a myth or a reality?
Something about WAP
 

Module CGI.pm

Module CGI is intended for simplification of creation HTML of documents by a script written on Perl'S. Module CGI is connected to your script on perl, the same as also other modules:

use CGI;



There are two ways of use of module CGI: it is object-oriented style and as a set of functions. At use of object-oriented style, you create one or several objects, and then use their methods for formation of HTML-page. an example of use of object-oriented style:

*!/usr/bin/perl


use CGI; * To connect module CGI


$q = new CGI; * To create new object CGI


print $q-> header


* A call of a method header which prints HTTP heading


* It instead of print " Content-Type: text/html\n\n "


$q-> start_html (' Hello World ')


* We print the beginning of the document (tags <HEAD> and <BODY>),


* Thus we insert a line <TITLE> Hello World </TITLE>


$q-> h1 (' Hello World ')


* We print heading of the first level


$q-> end_html;


* We print the end of the document (</BODY> </HTML>)



To use CGI as a set of functions, even easier, in this case, you do not need to create new object, you simply use functions CGI. The following example shows use CGI as a set of functions, as a result of his  performance the page similar will be given to page given out in the previous example.

*!/usr/bin/perl


use CGI qw/:standard/; * It is loaded standard procedures CGI


print header


* We print HTTP heading


* It instead of print " Content-Type: text/html\n\n "


start_html (' Hello World ')


* We print the beginning of the document (tags <HEAD> and <BODY>),


* Thus we insert a line <TITLE> Hello World </TITLE>


h1 (' Hello World ')


* We print heading of the first level


end_html;


* We print the end of the document (</BODY> </HTML>)



Syntax of functions in CGI is those:

name ({-atr => value,-atr2 => value2}, ' text ')



name is a name of function which as a rule coincides with a name of an interesting tag. About tags which names do not coincide with names of corresponding functions, the question will be further.


atr and atr2 are names of attributes of the given tag.


value and value2 are values of tags.


text is a text taking place between opening and closing tags.


Any of the specified parameters can fall. We shall consider all stated on examples.

print h1 () * Prints <H1>


print h1 (' Hello ',' world '); * Prints <H1> Hello world </H1>


print h1 ({-align => left}); * Prints <H1 ALIGN = "LEFT">


print h1 ({-align => left}, ' Hello '); * Prints <H1 ALIGN = "LEFT"> Hello </H1>



Some functions in CGI are used by pairs, similarly to closing and opening tags in HTML, for example:

start_html And end_html (<HTML> <HTML/>)


start_table And end_table (<TABLE> </TABLE>)


start_ul And end_ul (<UL> </UL>)


start_form And end_form (<FORM> </FORM>)



You probably had already time to notice, that names of functions are similar to names of tags HTML. In it simplicity also consists in development CGI, it is enough to know HTML that through 30 minutes freely to use CGI. the list of functions which names do not coincide with names of tags:

print redirect (' http: // www.perl.ru / '); * To readdress a browser to other page.


* An example of function of carrying out function known to us at once several tags


print start_html (-title =>'Hello World ', * Heading of the document


-author =>'flp@mail.ru ', * the Author


-meta => {'keywords' =>'world hello flp ', * Keywords


' copyright ' => ' copyright 2000 Philip Koryaka '}, * Copyrights


-style => {'src ' =>/styles/style1.css '}, * Style for the document


-BGCOLOR =>'blue '); * BGCOLOR for the document


print header (-Refresh => ' 10; URL=http: // www.perl.ru ', * Updating of the document


-cookie => $ cookie, * It Is established{installed} Cookie


-type =>'image/gif ', * Type of the document


-expires => ' + 3d '); * Working life


* The following tag creates the list of radio-buttons (analogue <INPUT TYPE=RADIO>):


print radio_group (' name ' => ' OS ',


' values ' => ['Unix ', 'Windows','Macintosh ']


' default ' => ' Unix ');


* The following tag creates a field of input of the password (analogue <INPUT TYPE=PASSWORD>):


password_field (-name =>'secret ',


-value =>'starting value ',


-size => 50,


-maxlength => 80);


* Similarly given function are used function file_field



Strength CGI is use of the parameters transmitted to a script. Function param () vzrahhaet value of any of parameters.


Let's call a script script.cgi? name=bob ? = en*opt=1*opt=4*opt=8

* A variable $a we appropriate{give} an option value name ($a=bob)


$a = param (' name ');


* A variable $a we appropriate{give} an option value lang ($a=en)


$a = param (' lang ');


* To a file @a we appropriate{give} option values opt (@a [0] =1; @a [1] =4; @a [2] =8;)


@a = param (' opt ');






© Web Development Company Conkurent, LLC 2007-2009. All rights reserved.