Perl CGI: The Happy Path: Creating a Dynamic Web Page

Prepared by Bill Kilgallon, Bill@KilgallonFamily.com

Unfortunately for you, this type of stuff is inordinately painfull on Windows machines. Albert Einstien said "Make things as simple as possible, but no simpler". Microsoft was not paying attention. Fortunately for you, we have the Cygnus toolkit to simplify things significantly.

  1. First, bring up a command line prompt. Click on your xterm shortcut icon on the desktop.
  2. type:
    goapache
  3. type:
    cd cgi-bin
  4. You are now in the root directory for all your dynamic CGI files.
  5. Now use the following command to create or edit your dynamic web page named "simple.pl":
    emacs simple.pl
  6. Enter the desired perl program for this file. See bottom of this page for sample program.
  7. Save the file, exit the editor.
  8. Enter the following URL into your browser
    http://localhost/cgi-bin/simple.pl
  9. You should see the html generated when the program you entered was executed.

Congratulations! You have published a dynamic web page!


The following would be decent sample program for your dynamic Perl CGI program. Customize as you wish.

#!C:\PERL\BIN\PERL.EXE
# The line above is called the shebang line.  It must be the first line 
# of your program, and it tells the server where to find the executable 
# program to be used to interpret the commands in this text file.  In 
# this case, this file is a perl program and should be processed with 
# the Perl interpreter program.

# Tell Perl we want to use one of it's helper modules.  In this case,
# the CGI module simplifies lots of the steps necessary to generate
# html instructions as output.

use CGI;

# This is another handy module to use.  It tells perl to be pretty
# fussy about syntax and complain a lot.  This sounds like a bad
# thing, but if you think about it, the only thing worse then a broken
# perl program complaining about lots of little problems is a broken
# perl program that just does the wrong thing without a whimper.  Fussy
# and verbose programs are much easier to debug.

use strict;

# This, for the time being, is magic.  We are creating a CGI object
# and naming it "query".  We will use this object as a helper to do
# most of the actual html work of our program.  We could do it all by
# hand if we wanted, but some really smart people have written some
# really nice tools, and we would be fools to ignore them.

my $query=new CGI;

# Create a header, and let it default arguments, it likely knows
# better then we do.

print $query->header();

# Start the body of our document, and give it a title (shows up on the
# browser bar and on printouts).  All the legal fields within the HTML
# "BODY" tag are supported, and are optional.  Other unsupported
# options can be specified as well, and will be correctly added.  Note
# that there is nothing actually in the body tag, just HTML modifiers
# being specified.

print $query->start_html({-title=>'Testing...',
                          -author=>'Bill Kilgallon'});

print $query->div({-align=>'center'});
print $query->h1('Hello World!');

print $query->hr();

print $query->cite('This is a citation');
print $query->br();
print $query->code('This is code');
print $query->br();
print $query->dfn('This is the defining instance');
print $query->br();
print $query->em('This is emphasized');
print $query->br();
print $query->kbd('This is keyboard input');
print $query->br();
print $query->samp('This is a sequence of literal characters');
print $query->br();
print $query->strong('This is strong text');
print $query->br();
print $query->var('This is a variable');

# Try some rules
print $query->hr();
print "This should be between two horizontal rules\n";
print $query->hr();

# And finally, end the document.

print $query->end_html;


Return to Index