Common
Gateway
Interface

A Basic Introduction to CGI, Perl, and CGI Scripting
Introduction and Overview of Project
A general introduction by the author as well as an overview is posted to a separate page.
Definitions
CGI, Common Gateway Interface
, is a protocol, a way of doing things, an agreed-on set of rules. It's the usual way that browsers (like Netscape & Internet Explorer) communicate with servers.

A server is the machine (the computer) owned by ISP (the company that takes your $ and provides your connection) that does your bidding on the internet.

The most basic function of the server is to grant your  request (from your browser) to see a file or page held by another machine connected to the internet. For example, you type an internet address, click return and after a bit of buzzing and whirring you see the page that matches that URL (internet address). In the background, mind the lingo here, your server has linked to the machine holding the file and "served" it to your browser.

The plot thickens, not only can your server go get stuff for you to see, but it will do other things for you. By writing CGI script, the server will be even more interactive allowing you, for example, to make a form on your web site and get the results as people fill it out (perhaps, making and sending an email with the results).

We can see many uses today for CGI scripts such as; forms (most popular) requesting information from us, hit counters, guest books, bulletin boards, shopping carts (for commercial sites), time or temperature responses.

In the case if a form, your interactive creation on your website (your interface) will contain two main parts: a form which will obtain some type of useful information, and a program that will perform some type of computation on that information ultimately writing its results into yet another HTML document (like email). 

Scripts and Perl
A script is simply a collection of lines of code that give instructions to the computer. Generally smaller and simpler than programs, scripts are not compiled (like programs), but are just text files that are interpreted as required.

CGI scripts can be written in many languages; some of the more popular are C or C++, Perl, Python, TCL, shells and others. The most popular is Perl for three reasons: it is powerful, it is easily moved between systems (Windows, Mac, Unix), it is easy to learn (compared to programming). Another very important reason is that many Perl scripts are available for free on the internet. Let me say that again, slowly and with emphasis, there exist archive libraries which will let you download and use already written Perl scripts at no charge. I have included the URL of several archives in "Links to Related Sites" at the end of this page.

What is Perl?  I offer this quote from the official Perl site.
"Perl is an interpreted high-level programming language developed by Larry Wall. According to Larry, he included in Perl all the cool features found in other languages and left out those features that weren't so cool.

Perl has become the premier scripting language of the Web, as most CGI programs are written in Perl. However, Perl is widely used as a rapid prototyping language and a "glue" language that makes it possible for different systems to work well together. Perl is popular with system administrators who use it for an infinite number of automation tasks.

Perl's roots are in UNIX but you will find Perl on a wide range of computing platforms. Because Perl is an interpreted language, Perl programs are highly portable across systems.

Finally, Perl is more than a programming language. It is a part of the Internet culture. It is a very creative way of thinking about almost anything." ( http://www.perl.com/pub/qa/What_is_Perl?)

Examples of Uses for CGI Scripting

#1 Example of a Form (caution- it's live... I get the results)

Name:
first

last
e-mail address
primary

secondary
Telephone number
(in case e-mail doesn't work)

primary

secondary


#2 Example of a Hit Counter

Hit Counter Counts the times this web site is visited.

#3 Example of a Perl Script
Say hello to CGI genie
(click here)

Note: This is a small, simple script written in Perl. The text to the right is exactly what is used to generate the response when you click the above link "Say hello to a CGI genie."

#!/usr/bin/perl

           $t = "Hello, World!";
           print <<EOT;
Content-type: text/html

           <Title> $t </Title>
           <H1> $t </H1>
EOT
#4 Another Example of Perl
Click here to see my first Perl script

Note: The text to the right is exactly what is used to generate the response to "Click here to see my first Perl script".

#!/usr/local/bin/perl
use CGI qw(:standard);

print "Content-type: text/html\n\n"; 
  #this is the mime header that tells Perl
  #the output will be HTML

print <<EOH;
<HTML>
<HEAD>
<TITLE>Hello world</TITLE>
</HEAD>
<H1>Hey - this works!!</H1>
<P>
<HR>
Last week I couldn't spell CGI, now I am making one.
</P>
EOH
;
;
Links to Related Sites
Webteacher Homepage and CGI Scripts (scroll to topic 19)-a wonderful site            sponsored by NationalCableTVAssoc.-great for basic knowledge +adv. topics
CGI Programming 101 - learn to write CGI scripts, online class format
HTTP Interface a bit more advance showing sample scripts from Indiana Univ.
CGI Resource Index - the name says it nicely
Scripts Search
- "world's largest CGI library"
Matt's Script Archive - a library of CGI scripts, a shareware site
Extropia.com - folks offering free open-source apps including CGI scripts
Sources:
Castro, Elizabeth. Perl and CGI for the World Wide Web: Visual Quickstart Guide.
        
Berkeley: Peachpit Press, 1999.
Official Perl site: http://www.perl.com/pub
 
Webteacher: http://www.webteacher.org/winnet/indextc.html 
Sample scripts:    
                 http://www.cs.indiana.edu/hyplan/nswoboda/cgi_tutorial/prog_html.html
Revised March 31, 2000
by Bill Teysko, teysko@home.com
for CIS212, Cuyamaca College
URL: http://cis3.cuyamaca.net/teysko/report/Default.htm