|
A CGI program always has 3 basic parts:
- The MIME type
- The HTML document
- A return value
The MIME type
Every document on the world-wide-web has a type associated with it.
Most documents are HTML, and have the associated text/html
type. To specify the type of a CGI program's document, we print
out what is known as the "content type" of the document:
Content-type: text/html<CRLF><CRLF>
This tells the client's browser (and possibly the webserver itself)
how to interpret the information that follows the declaration. In Perl
(and C/C++), the <CRLF> code is described by the escape sequence
\r\n\r\n, so you will often see the following line in a Perl
CGI program:
print "Content-type: text/html\r\n\r\n";
Which tells the browser that the document is written in HTML. For a
better discussion of the possibilities, see the vernacular
page of this tutorial.
HTML Document
The meat of the CGI transaction is in the HTML. Perl is a very flexible
string-processing language, and there are endless possibilities in this
section. Some examples are:
- Print out HTML code verbatim from Perl print statements such as
print "<H1>This is a header</H1>\n";
- Read a template and substitute a portion of the template document
with some dynamic code. This technique can be thought of as the reverse
of server-side embedded systems such as PHP, JSP or ASP, but it offers
the same benefits.
- Connect to a database and dynamically generate HTML from query results
- Read in form variables and values from an HTTP request and compute
the next HTML page using some process
- Print out HTML code, which contains image links that call
another CGI program that dynamically creates images on the fly (maps,
phone book pictures, etc.)
- Have Perl initiate an HTTP request to some other web server, mutate
the results of that document and pass the results back to the client
(meta search-engines, unified interfaces to disparate systems)
- Dynamically generate file-browsing indices to make a pretty front-end
to an FTP site
- Offer a web-form to cell-phone-messaging gateway that will display
text messages on an employee's cell phone
Basically, if you can code it with Perl, then you can make it web-enabled
with not much more effort.
The Return Value
After a CGI program ends, the webserver collects the "return value"
of the program so that it knows if there was an error or not. If the
return value is zero, then everything is okay. If the return value is
not zero, then it should display an error and log the error to the log
files. This is sometimes useful for debugging purposes because the programmer
is allowed to determine the meaning of non-zero return values. In Perl,
you can specify what the return value of the CGI program is with the
exit keyword:
exit (0);
exit (1):
Change the return value in your Hello World CGI program to a '1' and
see if you can find the resulting error code in your webserver's error
log.
All pages written by Craig Kelley unless otherwise specified.
Please use the Contact link from the menu to submit changes or suggestions.
Permission is given to use this tutorial in any way you wish including
re-publishing or "mirroring". The most up-to-date version of
this document currently resides at http://inconnu.isu.edu/~ink/perl_cgi.
This page updated:
May 6, 2002 22:21
|