Learning Perl on Win32 Systems

Learning Perl on Win32 SystemsSearch this book
Previous: 18.2 Your CGI Program in ContextChapter 18
CGI Programming
Next: 18.4 Passing Parameters via CGI
 

18.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.

# howdy--the easiest of CGI programs
print <<END_of_Multiline_Text;
Content-type: text/html
 
<HTML>
 <HEAD>
 <TITLE>Hello World</TITLE>
 </HEAD>
 <BODY>
 <H1>Greetings, Terrans!</H1>
 </BODY>
</HTML>
 
END_of_Multiline_Text

Each and every time this program is called, it displays exactly the same thing. It's not particularly dynamic, or interesting. 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, 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.

After it runs properly from the command line, you need to get the program installed on the server machine. Acceptable locations are server- and machine-dependent, although the scripts or cgi-bin directory of your server installation is a good place to start looking. Consult your server documentation or options to be sure.

After 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.plx, the pathname would be:

http://www.SOMEWHERE.org/cgi-bin/howdy.plx

Servers typically define aliases for long pathnames. The server at www.SOMEWHERE.org might well translate cgi-bin/howdy.plx in this URL to something like c:\inetpub\scripts\howdy.plx.


Previous: 18.2 Your CGI Program in ContextLearning Perl on Win32 SystemsNext: 18.4 Passing Parameters via CGI
18.2 Your CGI Program in ContextBook Index18.4 Passing Parameters via CGI