CGI Form Handling in Perl

Perl is an excellent language for a variety of tasks, especially those which require text management and data-parsing. Thus, it is well suited for writing code to manage the common gateway interface (CGI) forms which have become the mainstay of world wide web interactive communication via HTML+. (Note: the reverse is not true: CGI is a very poor way to learn Perl because of the difficulties of debugging and testing.) There is a tutorial on making forms, but unfortunately, it is out-of-date and doesn't cover CGI.

cgi-lib.pl is a simple Perl library which is designed to make writing CGI scripts in Perl easy. Some sample forms and scripts are provided here

A form, such as the one here, is just a normal html file. (You can use the "view source" option on your browser to see what the html of that file looks like.) After the user makes selections, the data is processed by a script, whose source is here, and whose name is specified in the original form.

It is often convenient to conflate the form and the script. This can done by having one script file which acts differently depending upon whether it is given parameters or not. (Sophisticated scripts may actually output new forms on the basis of previous forms.) The simple-form.html and simple-form.cgi have been combined into a single combined-form.cgi. The source to this script is here.

A minimalist form and script of only 7 lines can be made this way. (The source is here .)

Note: normally scripts reside in a 'cgi-bin' directory, so that the server knows to execute these scripts rather than simply display their contents. However, it is frequently more convenient to put scripts in the main document space, as has been done on this server. With NCSA's httpd, this can be done by adding the line AddType application/x-httpd-cgi .cgi to srm.conf, and by having every script have the extension .cgi. Note that enabling scripts in the main document space may have implications for the security of your server.

Steven E. Brenner / cgi-lib@pobox.com
$Date: 1995/06/13 23:25:21 $