CGI with Perl
Recommended background
Perl is a computer language developed by Larry Wall for writing programs
in a Unix environment and later adopted in many other operating environments.
Perl is commonly used for writing CGI scripts for use with the World Wide Web
(Web).
For more information about Perl, you might want to review the "Introduction
to Perl" by Larry Wall, et al. Marshall Brain of Interface Technologies has
written "A Quick Introduction to PERL," which is a succinct introduction for
users who already understand programming. For a more general approach to using
Perl to write CGI scripts, see
A Tour of HTML Forms and CGI Scripts, by Sanford Morton. Morton's document
may make a good companion for the document you are now reading; it covers much
of the same material from a different point of view.
Some knowledge of HTML and HTML forms processing is necessary before learning
to write CGI scripts; for background information about these topics, see
-
http://www.ncsa.uiuc.edu/General/Internet/WWW/HTMLPrimer.html
- for an introduction to HTML and
- http://www.ku.edu/~techdocs/documentation/docs/web-authoring_forms.pdf
- for an introduction to HTML forms and CGI scripts.
This tutorial is somewhat specific to writing scripts within the environment
provided by the Unix operating system, in general, and on systems operated by
Academic Computing Services (ACS) at The University of Kansas (KU), in particular.
The general principles will hold for any environment providing Perl, but the
specifics may change.
Users of ACS systems usually store their HTML documents in a directory called
public_html in their login directories. The Perl scripts they create
are stored in a subdirectory of public_html called cgi-bin.
Programs in this directory can be run as CGI scripts on most Unix systems operated
by Academic Computing Services (ACS) at the University of Kansas (e.g., FALCON,
EAGLE, LARK, RAVEN, etc.).
Writing Perl CGI scripts without forms
As mentioned earlier, this tutorial presents an erector set of Perl program
fragments for performing tasks commonly used with CGI scripts. These building
blocks allow you to
- Send an HTML document to the user
- Execute a script and possibly send its output to the user
- Send the contents of a file to the user
- Send a mail message to the user
- Record information submitted by a user in a file
Note that these building blocks are useful for building scripts that work both
with and without forms. Since scripts that work without forms are simpler, we
start with those and then enhance those scripts to work with data supplied by
forms.
Most readers will find it useful to review the entire tutorial to get the
big picture and then concentrate on those building blocks which they want to
use in their own CGI scripts.
Sending an HTML document to the user
Most scripts will return a page of information (usually an HTML document)
to the Web browser screen after a user submits a request for the script file.
Such a page may do as little as thank the person for filling out the form; it
may list the data that the user entered; or it may even contain another form that
points to another script file.
Perl may be used to return an HTML document by using a program like
#!/usr/local/bin/perl
print "Content-type:text/html\n\n";
print <
.
.
.
|