CGI (Common Gateway Interface)
October 4, 1999
Well, the most basic tool to access system resources is
CGI
(Common Gateway Interface). CGI is a service provided by
all web servers that allows you to 1) create an executable
script that the web server may call on demand, 2) pass
incoming
HTTP
GET or
POST
data to the CGI script, and 3)
filter CGI generated answers back to the browser.
You can think of a CGI script as taking the place of the
HTML file in the standard web server activity. However,
the key difference is dynamism. Rather than containing a
pre-defined set of text (like an HTML document) which can
only change when the author edits it, a CGI script can
dynamically generate any information it is programmed to
generate.
A simple example would be a CGI-generated clock that would
always show the current time when it is loaded. To do the
same thing with HTML, an HTML author would have to edit the
HTML document every second with the new time.
A CGI script, on the other hand, can access the time/date
resources of the operating system it runs on and independently
output the current time whenever it is called without the
author ever having to do anything.
If you know Perl, here is the code for that CGI script:
#!/usr/local/bin/perl
use Time::localtime;
print "Content-type:text/html\n\n";
my $time = localtime;
print "Today is: " .
($time->year() + 1900) . "/" .
$time->mon() . "/" .
$time->mday();
Of course, CGI is not a
programming language. CGI is an "interface". It defines a way
of bridging the web server to the back end resources. It
does not say anything about how that bridging will be
implemented. In fact, CGI applications can be written in
just about any programming language in use today.
|
NOTE: If you need to handle the data coming in
and going out to a web browser, your best bet is to use
CGI.PM written by Lincoln Stein. Like
ASP,
CGI.PM packages
up requests and responses into easy to use objects.
|
So why exactly do most web developers
choose to use the programming language Perl for their CGI
applications? Could one use another language like C, C++,
Apple Script or Visual Basic instead?
This is a good and extremely frequently asked question.
In fact, CGI applications can be written in any
programming language that is able to accept and process input,
and that is able to output the results of that processing.
However, for most of the CGI applications
on the web, Perl has been by far the best choice for two main
reasons: 1) Perl is the right tool for the job and
2) Perl is easy.
Introduction to Server-Side Processing
Introduction to the Web Application Development Environment (Tools)
Perl is the Right Tool for the Job
|