Hello World with CGI.pm

Overview
Objectives
Installing Apache
Installing Perl
Lesson 1
Hello World
Anatomy of a CGI Program
Hello World with CGI.pm
Lesson 2
HTML Forms and DOM
POST and GET
A Form Example
Lesson 3
Cookie Tutorial
Database Tutorial

Software Links
FAQ / Terminology
Contact the Author

 

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 webserver everything is fine
exit (0);

To see this program in action:

http://inconnu.isu.edu/cgi-bin/perl_tut/hello_cgi.pl

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:

use CGI;

Then we instantiate a new CGI object for our use:

my $cgi = new CGI;

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

print $cgi->start_html();

Which is the same as

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 untile you find "CGI"
clik on CGI, and the documentation will appear in the right pane


All pages written by Craig Kelley unless otherwise specified. Please use the Contact link from the menu to submit changes or suggestions. Permission is given to use this tutorial in any way you wish including re-publishing or "mirroring". The most up-to-date version of this document currently resides at http://inconnu.isu.edu/~ink/perl_cgi.

This page updated: May 6, 2002 22:21