More Details on Calling CGI Programs

Previous Table of Contents Next

More Details on Calling CGI Programs

So far, you've learned two different techniques for starting a CGI program. The first and most obvious is simply to call the CGI program's URL from a link or have the user type it in the browser. A line like this will start and run a CGI program called time.cgi:

<A HREF="http://server/cgi-bin/time.cgi">Click here for the time</A>

When this link is followed, the web server runs the CGI program time.cgi, and its output is displayed as a new web page. This example is simple, easy, and straightforward—just like the "Hello, World!" CGI program in Hour 17, "Writing Modules."

The other way to start a CGI program is to make it the target of an HTML form. For example, the following form calls the CGI program process.cgi when the submit button is clicked:

<form method="get" action="/cgi-bin/process.cgi">

<input type="text" name="STUFF"/><br/>

<input type="submit"/>


This method of calling a CGI program has an added benefit: You can pass parameters to the CGI program for processing. Well, that's the whole point of an HTML form.

Passing Parameters to CGI Programs

Wouldn't it be nice to pass information into a Perl program from a link? For example, you could have a clickable link in a document that would "run cgi program foo.cgi, with value X=this and value Y=that." This is possible with a little bit of work.

First, you have to use a special kind of URL in your <A HREF> tag. The format of this URL is illustrated in Figure 24.2.

Figure 24.2. URL containing parameters.

Each parameter is the name of a value that you want to pass into your CGI program—such as a named HTML form element. The value is the value for that name. For example, to create a link that, when clicked, will run a CGI program with the parameter sign set to Aries and year set to 1969, you would enter the following:

<a href="http://www.server.com/cgi-bin/astrology.cgi?sign=Aries&year=1969">

Aries, year of the Rooster</a>

Inside the CGI program, the parameters are processed with the CGI module's param function as normal:

#!/usr/bin/perl -w

use CGI qw(:all);

use strict;

print header;

print "The year ", param('year'), " and being born under ",

    param('sign'), "indicates you are brilliant.\n";

You can pass as many parameters as you want. If you want to pass an empty parameter—one with no value—simply leave it off, as in author in the following example:

<a href="http://www.server.com/cgi-bin/book.cgi?author=&title=Beowulf">Beowulf</a>

Special Parameter Considerations

You need to be aware of some special considerations when calling CGI programs with parameters. Some characters are special and cannot be made part of a URL. For example, the ? (question mark) is special: It marks the separation between the main portion of the URL and the parameters. Other characters that are special are &, spaces, and quotation marks.

By the Way

The full list of special characters is enumerated in an Internet standards document called RFC 2396.

To insert one of these special characters into a URL requires that you escape the character. In this case, escaping the character means to translate its ASCII value into a two-digit hexadecimal number and then precede it with a percent sign. The encoding for "Hello, World!" is as follows:


Obviously, creating a URL-escaped string can be messy. The CGI module provides a function to create such strings automatically for you. The following snippet demonstrates how to print a URL with the proper encoding:

#!/usr/bin/perl -w

use strict;

# The 'escape' function mus
Previous Table of Contents Next
© 2000- NIV