» Perl CGI » CGI Program with Perl CGI Module
CGI Program with Perl CGI Module
Now, lets take our Hello World program and make it more sophisticated:
-
#!/usr/bin/perl
-
# It's always a good idea to use the strict pragma while developing
-
# Perl code, it makes Perl warn about dangerous code
-
use strict;
-
-
# We're also going to include the CGI module, so that we can take
-
# advantage of other programmer's efforts (One of Larry Wall's basic
-
# tennants is that programmers are fundamentally lazy -- he's probably
-
# right, but I can't be bothered to prove it right now)
-
use CGI;
-
-
# instantiate a new CGI object
-
my $cgi = new CGI;
-
-
# perform a single print statement, with liberal use of the perl
-
# string concatenator "." and some CGI methods
-
print
-
$cgi->header .
-
$cgi->start_html('Hello World!') .
-
$cgi->h1('Hello World!') .
-
$cgi->end_html;
-
-
# Tell the web server everything is fine
-
exit (0);
Look familiar?
If you remove the comments from the above code you will find that it is much shorter than the previous version, but it looks exactly the same. The reason for this is the CGI module (if you get an error about not finding CGI.pm, then you need to install it from CPAN), which is a native Perl interface to HTTP and HTML.
We first tell Perl that we want to use the CGI module with the use directive:
Then we instantiate a new CGI object for our use:
Now, we can call the many methods that the CGI object exposes:
-
print $cgi->start_html();
Which is the same as
Which is easier? Good question. Use what happens to be best at the moment, but keep in mind that CGI.pm is always updated to use the latest in HTML technologies and standards, while hand-crafted code must be hand-maintained as well. CGI.pm also provides very handy interfaces for reading and writing HTTP (form) variables, cookies and it integrates with other Perl web technologies to provide a holistic development platform.
Here is the abstract for CGI.pm:
ABSTRACT
This perl library uses perl5 objects to make it easy to create Web fill-out forms and parse their contents. This package defines CGI objects, entities that contain the values of the current query string and other state variables. Using a CGI object's methods, you can examine keywords and parameters passed to your script, and create forms whose initial values are taken from the current query (thereby preserving state information). The module provides shortcut functions that produce boilerplate HTML, reducing typing and coding errors. It also provides functionality for some of the more advanced features of CGI scripting, including support for file uploads, cookies, cascading style sheets, server push, and frames.
CGI.pm also provides a simple function-oriented programming style for those who don't need its object-oriented features.
The current version of CGI.pm is available at
http://stein.cshl.org/WWW/CGI
ftp://ftp-genome.wi.mit.edu/pub/software/WWW/
You can view the CGI module documentation by visiting the above two links, or you can consult the local documentation on your system using one of the following methods, depending on your system:
UNIX
man CGI
Windows
Start -> Programs -> ActiveState Perl -> Documentation
Scroll down in the left pane until you find "CGI"
Click on CGI, and the documentation will appear in the right pane