Learning Perl

Learning PerlSearch this book
Previous: 19.2 Your CGI Program in ContextChapter 19
CGI Programming
Next: 19.4 Passing Parameters via CGI

19.3 Simplest CGI Program

Here's the source code for your first CGI program; it's so simple, it doesn't even need to use the CGI.pm module:

#!/usr/bin/perl -w
# howdy--the easiest of CGI programs
print <<END_of_Multiline_Text;
Content-type: text/html
    <TITLE>Hello World</TITLE>
    <H1>Greetings, Terrans!</H1>

Every time this program is called, it displays exactly the same thing. That's not particularly interesting, of course, but we'll spice it up later.

This little program contains just one statement: a call to the print function. That somewhat funny looking argument is a here document. It starts with two less-than signs and a word that we'll call the end token. Although this may look like I/O redirection to a shell programmer, it's really just a convenient way to quote a multiline string. The string begins on the next line and continues up to a line containing the end token, which must stand by itself at the start of the line. Here documents are especially handy for generating HTML.

The first part in that long string is arguably the most important: the Content-Type line identifies the type of output you're generating. It's immediately followed by a blank line, which must not contain any spaces or tabs. Most beginners' first CGI programs fail because they forget that blank line, which separates the header (somewhat like a mail header) from an optional body following it.[4] After the blank line comes the HTML, which is sent on to be formatted and displayed on the user's browser.

[4] This header is required by the HTTP protocol we mentioned above.

First make sure your program runs correctly from the command line. This is a necessary but not a sufficient step to making sure your program will run as a server script. A lot of other things can go wrong; see the section on "Troubleshooting CGI Programs" later in this chapter.

Once it runs properly from the command line, you need to get the program installed on the server machine. Acceptable locations are server-dependent, although /usr/etc/httpd/cgi-bin/ and its subdirectories are often used for CGI scripts. Talk to your friendly system administrator or webmaster to make sure.

Once your program is installed in a CGI directory, you can execute it by giving its pathname to your browser as part of a URL. For example, if your program is called howdy, the URL might be http://www.SOMEWHERE.org /cgi-bin/howdy.

Servers typically define aliases for long pathnames. The server at www.SOMEWHERE.org might well translate cgi-bin/howdy in this URL to something like usr/etc/httpd/cgi-bin/howdy. Your system administrator or webmaster can tell you what alias to use when accessing your program.

Previous: 19.2 Your CGI Program in ContextLearning PerlNext: 19.4 Passing Parameters via CGI
19.2 Your CGI Program in ContextBook Index19.4 Passing Parameters via CGI