Perl/CGI Tutorial

This tutorial provides a problem-oriented introduction to Perl within the context of CGI. The approach is narrative. A problem is introduced and the tutorial then proceeds, step by step, to solve the problem. Perl concepts and features are introduced as they are needed to accomplish each step. The Perl Basics discussion, by contrast, provides a systematic description in which concepts and features are presented in a logical order consistent with the structure of the Perl language, itself. The two discussions are intended to complement one another. While the tutorial can be used with different operating systems, the discussion is cast in the context of UNIX.

The problem that will be solved is extracting the data passed to a Perl program by a WWW server through the Common Gateway Interface (CGI) and constructing a reply expressed in HTML that is passed by the program back through the interface to the server and, then, to the client/user for display. This basic flow of data is shown in the figure, below. The terminal on the right represents the system you use to develop your Perl programs. You then store them within the file system used by the WWW server. They are subsequently accessed, through a WWW server and the CGI, by a WWW browser somewhere on the Internet.

Basic WWW architecture, with CGI Interface and user terminal.


Steps

The problem this tutorial will address is divided into seven steps:
  1. Perl framework and mechanics
  2. Hello, World
  3. Hello, World, from CGI
  4. Hello, World, from CGI, in HTML
  5. Echo Environment Variables
  6. Echo STDIN Variables
  7. Perl Library
Each of these steps is introduced, below; more detailed discussions and example programs can be accessed from each of the summaries.

1. Perl Framework and Mechanics

Several suggestions are offered for organizing your work in Perl, and instructions are provided for accessing the Perl interpreter.

2. Hello, World

The goal of step 2 is to get the Perl interpreter to give a minimal response so that you know it is functioning. You will do this by creating a very simple "Hello, World" program.

3. Hello, World, from CGI

The next step is to modify your Hello, World program so that it can be run from a Web browser, as opposed to running it directly from the Perl interpreter, e.g., through the UNIX shell.

4. Hello, World, in HTML

In the previous step, you generated CGI header lines and a single plain text "content" line. In this step, you will expand the content portion and embed HTML tags within it. As a result, when the data are displayed by a Web browser, they will be formatted as conventional HTML data.

5. Echo Environment Variables

When a user fills out a form in a Web browser, those data are returned to the server. If the form uses the GET method, the server places them along with HTTP header values in a set of environment variables which can be accessed from within a CGI program. The goal for step 5 is understanding what environment variable data look like and how they can be accessed by a Perl program.

6. Echo STDIN Variables

If a form uses the POST method, the user's data will not be placed in environment variables (although HTTP values will be), as discussed in step 5, but instead will be passed as a character string from the server to a CGI program through the STDIN file. The task in step 6 is to parse the data passed through STDIN.

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.