How does the CGI program work?

The CGI program can do almost anything. Our CGI program (called geom.pl) will only analyse the name=value string and pass on the name of the protein file (in the value part) to a molecular geometry program called geom after printing a simple heading. geom will read the Brookhaven file and calculate all the bond lengths, bond angles and torsion angles of the protein. The results will be displayed by the browser.

CGI programs can be written in any language. Sometimes they are written in Unix shell languages, such as bash, or they can be written in C/C++. Perl is particularly well suited to processing HTML forms and that is what I have used for geom.pl in order to decode the CGI string. However Perl is not suitable for heavy numerical calculations and therfore the Perl script passes the file name to geom which is machine code compiled from a C++ source text.

Let us now look at the Perl script.


############################ geom.pli ###############################
# CGI script to get file name from HTML form, call molecular geometry
# calculation and pass back output to WWW server for browser to
# display as plain text

# Get CGI-parsing routines
require "cgi-lib.pl";

# Parse form data
&ReadParse;

# Flush STDOUT buffer after each command
select(STDOUT);
$| = 1;

# Print header of output
print STDOUT "Content-type: text/plain\n\n";
print STDOUT "Protein Residues and Molecular Geometry\n\n";
print STDOUT "Protein file: $in{'filename'}";

# Call molecular geometry program
system("/home/prog/classlib/apps/geom/geom $in{'filename'}");


The require statement hooks up the CGI Perl library and the &ReadParse statement calls the subroutine from the library which reads the CGI string from the server, splitting it up and putting its contents into an associative array, sometimes known as a map. An associative array is like an ordinary array except that instead of a numerical subscript, a string is employed to retrieve array items. &ReadParse puts the contents of text entry boxes into an associative array called %in. The names of the boxes can be used as array indexes to recover the text strings entered into the boxes. The box in our form was called 'filename'. Hence we can access the text in that box (hopefully the file name!) by writing $in{'filename'}. The $ prefix is required in Perl because the array element is a scalar quantity.

The output from the script which is associated with the Perl filehandle STDOUT. The select statement and the assignment to $| ensure that this output is unbuffered. This means that the output is sent to the server immediately after each print statement has been executed. Without this precaution, the output from the print statements in this script and the output from the molecular geometry program may become mixed up.


Try Out the Geometry Program
Back to Index