7. Perl Library

Now that we have climbed the peak once, we'll take the chair lift this time. It is important to understand the details of both how data is coded and passed to a CGI program and how to write Perl programs to process that data. However, parsing HTTP, creating HTTP headers, and performing other similar tasks are routine procedures. Others have written Perl programs that perform tasks such as these. The big advantage of using an existing library is that you can use it to accomplish the routine part of a task while you concentrate on the non-routine parts.

In this step, you will duplicate the results of earlier steps, but do so using a CGI support library.

I have placed one such set of programs, written by Steven E. Brenner and called cgi-lib.pl, in a library, called lib, under the course programs directory, programs/Perl/lib. You should begin this step by reading the Perl code for cgi-lib.pl to get a general sense of what is included. After that, refer to the program, below, to see how its functions can be used to parse CGI input, build appropriate data structures, and echo their values.

Echo Variables Using lib Program


require "/afs/unc/proj/wwwc-f95/lib/cgi-lib.pl";
print &PrintHeader;
print "<H2>Environment variables</H2>\n";
print &PrintVariables(%ENV);
print "<HR>\n";
print "<H2>User-defined variables</H2>\n";
 if (&ReadParse(*input)) {
    print &PrintVariables(%input);
 } else {
   print '<form><input type="submit">Data: <input name="myfield">';
Below are two forms to test the program:
POST form
GET form