CGI Program with Perl CGI Module

Now, lets take our Hello World program and make it more sophisticated:

  1. #!/usr/bin/perl
  2. # It's always a good idea to use the strict pragma while developing
  3. # Perl code, it makes Perl warn about dangerous code
  4. use strict;
  5.  
  6. # We're also going to include the CGI module, so that we can take
  7. # advantage of other programmer's efforts (One of Larry Wall's basic
  8. # tennants is that programmers are fundamentally lazy -- he's probably
  9. # right, but I can't be bothered to prove it right now)
  10. use CGI;
  11.  
  12. # instantiate a new CGI object
  13. my $cgi = new CGI;
  14.  
  15. # perform a single print statement, with liberal use of the perl
  16. # string concatenator "." and some CGI methods
  17. print
  18.    $cgi->header .
  19.    $cgi->start_html('Hello World!') .
  20.    $cgi->h1('Hello World!') .
  21.    $cgi->end_html;
  22.  
  23. # Tell the web server everything is fine
  24. 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:

  1. use CGI;

Then we instantiate a new CGI object for our use:

  1. my $cgi = new CGI;

Now, we can call the many methods that the CGI object exposes:

  1. print $cgi->start_html();

Which is the same as

  1. print "<HTML>\n";

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