Your First CGI Program

Previous Table of Contents Next

Your First CGI Program

After all the fanfare, warnings, checklists, and runaround, you're ready to type in your first CGI program. It is shown in Listing 21.1.

Type and save this program as hello. If, in the checklist, you are required to use a certain extension for CGI programsas noted in the "Extension to be used for CGI programs" itemuse that extension. Thus, if you're required to name your CGI programs with a .cgi extension, save this script as hello.cgi. If you're required to use .pl, save this script as

You did fill out the checklist, didn't you?

Listing 21.1. Your First CGI Program

1:   #!/usr/bin/perl -w

2:   use CGI qw(:standard);

3:   use strict;


5:   print header;

6:   print "<b>Hello, World!</b>";

Line 1: This line is the standard #! line. You must substitute the path from the checklist item "Location of Perl on the web server" for this script to work. The -w, of course, enables warnings.

Line 2: The CGI module is included in the program. The qw(:standard) causes the standard set of functions from the CGI module to be imported into your program.

Line 3: use strict enforces good coding practices; this is no different for CGI programs.

Line 5: The header function is imported from the CGI module. It prints a standard header that the server (and the client) must see to process the output of the CGI program.

Line 6: After the header is printed, any output will appear normally to the browser. In this case, when the CGI is run, the browser will display the words Hello, World.

That's it!

Well, not quite. You still have to install this CGI program and test it. You've won only half the battle.

Installing the CGI Program on the Server

How you install your CGI program depends heavily on what kind of server you have, whether you have local access to it, whether it's a server that you can only FTP files to, and so on. The following sections describe how to install for the different scenarios.

Local Access to a File System on a Unix Web Server

Use these instructions if you're able to telnet, rlogin, or otherwise log in to the Unix web server:

  1. Put the CGI program hello.cgi (or onto the Unix server, perhaps with FTP.

    You may have actually written it on the server with vi. That's fine, too.

  2. Move the CGI program into the correct directory by using the mv or cp command.

    You should have found the correct directory in the checklist under "Location of CGI program directory."

  3. Under Unix, you need to make the program executable. Do so by issuing the following command:

    chmod 755 hello.cgi

    Use instead if it is the program's name. This command makes the file writable by the owner and readable and executable by everyone else (this is correct for a CGI program).

FTP-Only Access to a Unix Web Server

Use these instructions if you have FTP-only access:

  1. Use your FTP client to put the hello.cgi (or program into the CGI program directory. You should have found the correct directory in the checklist under "Locati

Previous Table of Contents Next
© 2000- NIV