Glory

星期一, 三月 19, 2007

Perl-CGI

CGI.pm Method Groupings

Group Description

:all Contains all of the methods available in the CGI.pm module

:cgi Contains methods specific to the CGI protocol

:form Contains methods used to generate forms

:html A superset of the :html2, :html3, :html4, and :netscape groups

:html2 Contains methods that enable the developer to quickly use elements from theHTML version 2 specification

:html3 Like the :html2 group, contains methods that enable the developer to quickly use elements from the HTML version 3 specification

:html4 Contains methods that enable the developer to quickly use elements from theHTML version 4 specification

:netscape Contains the Netscape extensions for the HTML 3 specification, including a shortcut to my favorite HTML tag <BLINK>

:multipart Contains methods used to help work with MIME types

:standard Probably the most frequently used group of methods, a superset of the :html2,:html3, :html4, :form, and :cgi groups


A function-oriented Fashion CGI

#!/usr/bin/perl T


use strict;

use CGI ':standard';


print header;

print start_html('Hello World');

print h1('Hello World');

print end_html();


exit;


An object-oriented Fashion CGI

#!/usr/bin/perl T


use strict;

use CGI;


my $cgi = new CGI;

print $cgi->header;

print $cgi->start_html('Hello World');

print $cgi->h1('Hello World');

print $cgi->end_html();


exit;


The CGI module has a large number of functions to make the life of the programmer easier. These functions range from those that create HTML tags, HTTP headers, and cookies to those for working with web forms.

HTML Shortcuts

Most of the HTML version 3 and 4 tags are available through a function in the CGI module. Simply calling the HTML function with a print statement will cause it to be sent to the outputted page.


Dynamic Pages and Forms

Just as standard HTML elements are available through functions in the CGI module, so are form elements. For example, tags to start and end forms, code for text fields and other input fields, and code for buttons are available as CGI module functions. The syntax for these functions is the same as the syntax for calling HTML tag functions.

Printing the Name Input Using the CGI Module

#!/usr/bin/perl -T

use strict;

use CGI qw/:standard/;

print header,

start_html('Hello'),

start_form,

"Enter your name: ",textfield('name'),

submit,

end_form,

hr;

if (param()) {

print "Hello ",

param('name'),

p;

}

print end_html;

exit;


Cookies

A Simple Cookie Example

#!/usr/bin/perl -T

use strict;

print "Content-type: text/html\n";

print "Set-Cookie: testcookie=testvalue;";

print "\n\n";

print "You've received a cookie<p>\n";

exit;



Post by Word2007

标签: ,

0 Comments:

发表评论

<< Home