blank.gif
webreview.com - Cross-Training for Web Teams
Search for: 
Jump to:
blank.gif
blank.gif

 
 

A Songline PACE Production



CGI Programming with Perl

by Brent Michalski
June 12, 1998
 
 

Since this is my debut article, I thought I'd start out with the basics. My subsequent articles will then build upon what you have learned from the earlier ones. This article will not teach you "how to program." I am assuming that you are familiar with the Web and that you have some basic knowledge of programming. 
Try the demo
Try the demo.

This week's article covers some basics of CGI programming. This article is intended for those who have Web sites but may not have ventured into CGI programming yet. To utilize these tutorials and programs, you must have the ability to run CGI scripts on your Web server. Some ISPs do not let users run CGI programs. If you are in doubt, call your ISP and ask them.

Perl information 

I don't know who said it first, but Perl has been called, "the duct tape of the Internet." I couldn't agree more. Perl is widely used because it is simple, fast, widely supported and can run on just about any platform. If you want to find out more about Perl, please visit our sister site at: www.perl.com.

Since this is the Perl area, all of the CGI programs and issues I will be covering here will be written in Perl.

CGI information 

To set the record straight, there really is no programming language called CGI. CGI stands for Common Gateway Interface. It is a gateway that allows you to execute code on the Web server and return the results to the Web browser. This opens up a whole new world to Web developers because you are no longer stuck with static HTML pages -- you can generate Web pages on the fly with CGI.

CGI programs can be in C, C++, Java, Visual Basic, Perl or just about any language that can run on your Web server. The Web server doesn't care what language you write it in, as long as you follow the rules. However, there are hundreds, if not thousands, of modules and programs already written in Perl. Don't be afraid to take advantage of what others before you have already done.

CGI programs can be very simple or very complex. We will start out with simple programs that are not really useful, and then move on to more useful, complex programs.

Writing CGI programs 

I have seen many articles on CGI programming, but they all fail to discuss one very important topic: How do I get the program on my Web server?

There are two ways that I use to write CGI programs. I can write the program on my computer using a text editor, and then FTP the program to the Web server. Or, I can telnet to the Web server and write the program directly on it using a text editor on the server. This is assuming that the server is a UNIX machine.

Both will work, but there are a few things you need to remember. 

FTP Option:

  • You must FTP it in ASCII mode. If you send the program in Binary mode, you will get errors.
  • If you send it to a UNIX server, you need to remember to set the rights (permissions) on the file(s) so that they can be executed.
Telnet Option:
  • This is my preferred way because it allows me to test the scripts directly on the server before I even try to call it from the browser.
  • Best for debugging. When you run the programs directly on the server, you can see any error messages generated. If you get an error when you call a program from your browser, you will typically get a 500 Internal Server Error which tells you nothing about why the error occurred.
The choice is yours. For me, the clear winner is by telnetting into the server and writing the program directly on the server.

MIME types 

With CGI programming, you must specify the MIME type. Simply put, the MIME type tells the server what type of data is being sent, and it tells the browser what type of data to expect. In Perl, it looks like this:
print "Content-type: text/html\n\n";
This tells the server and browser that the data you are sending is text and that the text is html. The two \n's are carriage-returns in Perl. You must have two of them at the end of the line. If you don't, even though the program is syntactically correct, and it will run properly from the command-line, it will produce a server error if you try to call it through CGI.

Another note about this line of code: It must come before any other print statements. Normally when you issue a print statement in a program, the results are displayed on-screen. In CGI programs, the person who started the program may be in St. Louis but the Web server the program is running on may be in Tokyo. Displaying the results on the server's screen would be useless. So what happens is that you tell the Web server what kind of data you are sending, and any print statements will get re-routed through the Web server and displayed on the calling Web browser. This is why the MIME type must come first, it instructs the Web server what to do with the output.

There are many other MIME types. text/html is probably the most common for CGI programming on the Web. If we happen to use a type other than text/html, I will talk about it then, but for now we will just concentrate on using this single MIME type.

Your first CGI program 

Ready? I'll show you how to create a very simple CGI program. It won't be very useful, but it will allow me to explain a few things that will crop up in just about every CGI program you write.

This program will simply print out a title, your host name, and the current time. The time it displays is the current time on Web Review's Web server, not where you are.

In the sample code below, I have numbered the lines of code to make it easier for me to talk about the program. The line numbers are not part of the program. If you want to see the program without the line numbers, view or download the source code. Also, since we will be talking about the code itself, I only numbered the lines of code -- ignoring blank lines or those with comments.

 1: #!/usr/bin/perl
    ########################
    # A simple CGI program #
    # By: Brent Michalski  #
    ########################

 2: $time=localtime(time);

 3: print "Content-type: text/html\n\n";

 4: print<<end_of_block;
 5:   <HTML><HEAD><TITLE>My First CGI Program</TITLE></HEAD>
 6:   <BODY BGCOLOR="#FFFFFF">
 7:   <CENTER><H1>My First CGI Program</H1><CENTER>
 8:   <P>
 9:   Your host is: $ENV{'REMOTE_HOST'};
10:   <BR>
11:   The time is: $time
12:   <P>
13:   This is the end of the program.  Like I said, not very useful
      but we will be getting to the useful programs soon!
14:   </BODY></HTML>
15: end_of_block
Here is the program in action.

Line-by-line explanation 

Line 1: Tells the program where to find Perl on the Web server. This line will vary depending on where Perl is installed on your server, so you need to make any necessary changes. This line is not required if you are running the program on an NT server -- but won't hurt anything if it's included. If you are running this program on a UNIX server, this line is required.

Line 2: Assigns the variable $time from the current time on the Web server. It uses two functions in Perl, localtime and time. time grabs the time from the server, but it is in a hard-to-read format -- the number of seconds since Jan 1, 1970. localtime takes the result from time and converts it into something more readable.

Line 3: Is our MIME type which we talked about above.

Line 4: Tells Perl to print everything until it reaches the token that I assigned. The token in this case is end_of_block but it can be anything you want as long as the beginning and ending tokens match. I could have skipped this, but then every line I want to print would need to begin with print and end with "; This short-cut can save you a lot of typing.

Lines 5-8: These lines are simply HTML. I assume you know how to write HTML so I will not explain what they do.

Line 9: Introduces us to a new feature. When you run a CGI program, there are several variables that are automatically sent by your browser to the Web server. All of the variables are accessed through $ENV{'variable'}. You can find a list of most CGI Environment Variables on a site maintained by Steven Berry. This particular variable, REMOTE_HOST, is the name of the host that sent the request.

Lines 10-14: Are just more HTML. Notice, however, that Line 11 has our $time variable on it. In our resulting Web page, the value that was stored in $time will be displayed.

Line 15: This is our token to tell Perl to quit printing everything out now. This token must be all the way to the left side of the screen and there can be nothing else on this line, not even comments. If you fail to do this, you will get an error.

Wrapping it up 

Well, we have covered enough ground for this week. We covered MIME types, environment variables, printing blocks and getting the local time.

Next week, I will talk about using forms. I assure you that when we get into forms, you will see how CGI programming can be a very useful tool that we will keep building upon.


Source Code for "My First CGI Program" Demo
View and download the source code to create your first CGI program.

Next: Handling Forms in Perl

Web Review copyright © 1995-99 Songline Studios, Inc.
Web Techniques and Web Design and Development copyright © 1995-99 Miller Freeman, Inc.
ALL RIGHTS RESERVED