CGI/Perl 

 
   
Introduction to CGI/PERL

The Common Gateway Interface (CGI) is a standard for interfacing external applications with information servers, such as HTTP or Web servers. A plain HTML document that the Web daemon retrieves is static, which means it exists in a constant state: a text file that doesn't change. A CGI program, on the other hand, is executed in real-time, so that it can output dynamic information.

Since a CGI program is executable, it is basically the equivalent of letting the world run a program on your system, which isn't the safest thing to do. Therefore, there are some security precautions that need to be implemented when it comes to using CGI programs. Probably the one that will affect the typical Web user the most is the fact that CGI programs need to reside in a special directory, so that the Web server knows to execute the program rather than just display it to the browser. This directory is usually under direct control of the webmaster, prohibiting the average user from creating CGI programs. There are other ways to allow access to CGI scripts, but it is up to your webmaster to set these up for you. At this point, you may want to contact them about the feasibility of allowing CGI access.

If you have a version of the NCSA HTTPd server distribution, you will see a directory called /cgi-bin. This is the special directory mentioned above where all of your CGI programs currently reside. A CGI program can be written in any language that allows it to be executed on the system, such as:

  • C/C++

  • Fortran

  • PERL

  • TCL

  • Any Unix shell

  • Visual Basic

  • AppleScript

It just depends what you have available on your system. If you use a programming language like C or Fortran, you know that you must compile the program before it will run. If you look in the /cgi-src directory that came with the server distribution, you will find the source code for some of the CGI programs in the /cgi-bin directory. If, however, you use one of the scripting languages instead, such as PERL, TCL, or a Unix shell, the script itself only needs to reside in the /cgi-bin directory, since there is no associated source code. Many people prefer to write CGI scripts instead of programs, since they are easier to debug, modify, and maintain than a typical compiled program. 

What is Perl? 

Perl is a high-level programming language with an eclectic heritage written by Larry Wall and a cast of thousands. It derives from the ubiquitous C programming language and to a lesser extent from sed, awk, the Unix shell, and at least a dozen other tools and languages. Perl's process, file, and text manipulation facilities make it particularly well-suited for tasks involving quick prototyping, system utilities, software tools, system management tasks, database access, graphical programming, networking, and world wide web programming. These strengths make it especially popular with system administrators and CGI script authors, but mathematicians, geneticists, journalists, and even managers also use Perl. Maybe you should, too.

Configure the script 

In order to run your script, you'll have to modify the first line with the correct path to the Perl interpreter. "/usr/bin/perl" is the path to your perl interpreter, and it should appear after '#!' (also called 'shebang') on the first line of the script.

#!/usr/bin/perl

Normally, anything following '#' is considered to be a comment, but the first line in a script is the exception.

Upload it to the server 

The most important thing to remember is to use ASCII mode when you upload the script to the server. If you think you have everything configured properly but the script won't run, reload it using ASCII mode, just to be sure. 

Create a directory called "cgi-bin" under your /web directory and put your .pl files under it.

File Permissions 

Every file has both an owner and a group. By default, the owner and the group will be the same as the user (or process) that created the file. There are three levels of permission for every file, and they are the owner's permissions, the group's permissions, and everyone else's permissions. Each level may have read, write, or execute access for a file. 

The permissions on a file are easy to understand as a matrix, in the form shown below.

Owner Group Everyone
Read 4 4 4
Write 2 2 2
Execute 1 1 1

Three columns designate three groups, Owner, Group, and Everyone. Owner is the person who owns the file (you). Group is designated for any groups that are set up by your network administrator, and may or may not apply to your script. Check with your network administrator if you're not sure. Everyone refers to the world-at-large, like anyone who will use your script on the Internet. 

The three rows of the matrix are the three actions that may be performed on your file: Read, Write, Execute. Read means that the file can be read, but not changed in any way. Write means that data can be written to the file, and Execute means that the file can be run as a program. Each is valued in the following way: Read=4, Write=2, Execute=1. The values for each column are added up, then the three digit number is used to change the permissions on your server. 

Here's an example for the permissions needed by CGI scripts:

Owner Group Everyone
Read 4 4 4
Write 2 0 0
Execute 1 1 1

Overall 7 5 5

In other words, anyone can read or execute the script, but only the owner can write to the file. Please note that you do NOT want to assign write privileges to Group or Everyone! The only updates to your files should be done by your script, and not by any hacker who happens upon your site. 

The sum of the columns is the number you'll use with chmod, like this chmod 755 myscript.pl

If you're using a graphical FTP program, you should be able to make the appropriate choice with radio buttons. See your program's documentation for more information. 

One final point about moving your files correctly is regarding the file extension. Servers may be configured to accept only certain extensions, such as .cgi, or .pl. Check with your hosting service to see if you need a specific extension on your scripts. 

Test the script 

Now that you know how to install a script, you'll need a script you can use for testing. The steps are the same no matter what the scripts do.

test.pl

#!/usr/local/bin/perl

#===============================
# Testing CGI script installation
# Created by Catchcal
#===============================
# This script is designed to test
# CGI script installation
#===============================

print "Content-type: text/plain\n\n";

print "Welcome to Catchcal";

Upload the file using the steps outlined above. Type in the URL to the script in your browser. It will look something like http://www.domain.com/cgi-bin/test.pl

Top