Home > Perl > Web programming > CGI Programming - Part 2
CGI Programming - Part 2
Written by Philip L Yuson   
Who is this for
This article provides a sample CGI script to show what you can do with CGI programming.

What you Need to Have by Now
Skills: Perl programming, Basic HTML
Environment: Webserver setup to run you CGI scripts.

What Can You Do With CGI Scripts
When you have a static HTML page, you can display whatever information you want. However, if you want to get a little fancier, like allow users to enter data on your page and process the data, or simply to include the current date and time in your HTML page, you will need to write some scripts. This is where CGI comes in. Simply put, a CGI script is a program that generates an HTML page. How you generate your HTML page depends your requirements. Your CGI script can be written in any language but most CGI scripts are written in Perl.

The %ENV hash
When your server starts your CGI script, it contains several general information that your script can use. Among the more useful information in the %ENV hash are:




Length of the input data


Input data to be passed to the script


IP address of your server


Name of your server


Name of your script. This is the string after the http://hostname


The starting path for all your documents


Specifies how your CGI is to process the data that is being passed from the HTML page


Cookie set for this CGI

To see all the keys in the %ENV hash, create this program and save it in your cgi-bin directory. Remember, your cgi-bin directory should be defined to run CGI scripts. Otherwise, you will get weird results.

print "Content-type: text/html\n\n";
foreach (sort keys %ENV) {
print "<B>$_</B>$ENV{$_}<BR>\n";

To see the result, save this as parm.cgi in your cgi-bin directory. Then on your browser, typehttp://localhost/cgi-bin/parm.cgi This is assuming that your cgi-bin directory is called cgi-bin. I have saved this on a free CGI server and unfortunately, the server is not reliable. You may want to check it anyway by clicking on this link. Now that you see that you can get data from an HTML page, you probably have an idea of what you can do with CGI scripts.

A Sample Form
Let us say that you want to create a sample Guestbook. The Guestbook can have the following fields:

  1. Name

  2. e-Mail Address:


You will also need two buttons. One for sending the information and one to reset the form.

If you were to code a static HTML page, you can code it this way:

<FORM METHOD=POST ENCTYPE='application/x-www-form-urlencoded'>
<INPUT TYPE=TEXT NAME=guestname></TD></TR>
<TR><TD>e-Mail Address:</TD><TD>
<TR><TD VALIGN=TOP>Comments:</TD><TD>
<TEXTAREA rows=10 cols=40 NAME=comment></TEXTAREA>
<TR><TD><INPUT TYPE=submit NAME=Send Value=Send>
<INPUT TYPE=reset NAME=Reset Value=Reset></TD><TD> </TABLE><HR>

The Form will look something like this:

e-Mail Address:

We said earlier that a CGI script is a program that generates HTML code. To make it more interesting, we can make it print out the %ENV parameters if there is data passed to it. The code will look like this:

print "Content-type: text/html\n\n";
foreach (sort keys %ENV) {
print "<B>$_:</B>$ENV{$_}\n";
} else {
print <<EndHTML;
<FORM METHOD=POST ENCTYPE='application/x-www-form-urlencoded'>
<TD>e-Mail Address:</TD>
<TD VALIGN=TOP>Comments:</TD>
<TD><TEXTAREA rows=10 cols=40 NAME=comment></TEXTAREA></TD>
<TD><INPUT TYPE=submit NAME=Send Value=Send></TD>
<TD ALIGN=RIGHT><INPUT TYPE=reset NAME=Reset Value=Reset></TD>

Notice that we just put the entire HTML form as data in the CGI script. This makes it a lot easier for us.

To see how this works, you can copy the code and save it in your server and try it out. Once the form is displayed, try entering something on the fields and click the Submit button and see the result.

This is the first step part in writing a CGI script.

For more information, you can check out:
http://www.webmonkey.com - for HTML basics
http://www.cgi101.com - for more detail discussion on CGI programming

Copyright: © 2017 Philip Yuson