★ wanayoo — archive 1999 http://www.webdevelopersjournal.com/columns/cgi1.htmlNouvelle recherche | Portail wanayoo
Back to the Web Developer's Journal Main Page
internet.com
side nav bar

Internet Trade Show List

Jobs at webdeveloper.com

Gary Lee
is a co-owner of meep! media inc., an Internet and Intranet consulting company. He is also one of the programmers, and creator of meep! media's first product, meep!Board, a message board system. Gary is the Editor of Web Tech, one of meep! media's online publications.
Related Articles:

HOW DID THEY DO THAT???

Find out in:
Amazing HTML



Site Map

Search Our Site



Check out our Web-based
Discussion Groups:

Check out and join our email-based Mailing Lists for Web developers.
Privacy Policy


Ask the JavaScript Weenie:

For news and reviews of the latest computer audio tools, visit: The Tapeless Studio
Grow your revenue stream!
Discussion Groups Book Reviews Software Reviews Download Web Tools

With about ten lines (or two with CGI.pm), you can write a functioning CGI program that is the basis of ALL CGI programs, big or small.

CGI Programming Made Overly Simple

by Gary Lee

Alas, my four years of studying Basket Weaving prevents me from breaking into that group. Nevertheless, I'll try to shed some light on the subject of CGI programming. My main qualification is that I'm writing this for free.
September 18, 1997

There is a certain clique-mentality among programmers, built around years of torment in high school from the "jocks", the "bad kids", the "music students", and even from the "glee club".

Many CGI programs are written in perl, a programming language suited to manipulating text and files. Let's look at a very simple perl CGI program line-by-line to get a little feel of perl, CGI, and to fill my word quota.

This program will read input from an HTML form...something like:

Your input :
and return what you typed in each field.

This is the first line of the perl program:

#!/usr/bin/perl

It tells the computer where the perl interpreter is located. Every instruction that follows will be shoved into the interpreter, causing it to perform accordingly.

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

This tells whatever web browser is accessing this program to get ready for some HTML, so it can display something instead of acting like a blank screen saver. (Note: I prefer Netscape's blank screen to Explorer's blank screen.)

read(STDIN,$input,$ENV{'CONTENT_LENGTH'})

"STDIN" is short for "standard input"". Just like you have a mouth as standard input for food, as opposed to your nose or something more imaginative, programs have a standard input to shove in information. So, the web server shoves information from an HTML form into this standard input, the program reads the amount of info (stored in a special variable called $ENV{'CONTENT_TYPE'}) and copies it to "$input". All simple variables in perl start with a dollar sign.

foreach (split(/&/, $input))
{
    ($NAME, $VALUE) = split(/=/, $_);
    $NAME =~ s/\+/ /g;
    $NAME =~ s/%([0-9|A-F]{2})/pack(C,hex($1))/eg;
    $VALUE =~ s/\+/ /g;
    $VALUE =~ s/%([0-9|A-F]{2})/pack(C,hex($1))/eg;
    print "$NAME : $VALUE <BR>\n";   
}

This wee section digests the info from an HTML form Each input field in the form has a NAME. For example, the HTML for the input field above is:

<INPUT NAME="yourinput">

So, if you type "How do you pronounce meep?" into the input box, the web server will package it up according to the rules of CGI into a single continuous line containing the name and value you typed:

yourinput=How+do+you+pronounce+meep%3F

See how the name of the input field comes first, followed by an equal sign and its value? Also, notice how the spaces have been replaced by plus signs, and the question mark has been replaced by "%3F. This is not some obscure code...it's the world-famous ASCII code. Some weirder (i.e. non-alphanumeric) characters are replaced by a percent sign followed by their ASCII value. The question mark is number 63 on the ASCII table. In hexidecimal numbers, that's 3F. Voila.

Now, if there were two input fields, they would be packaged up into one gigantic line separated by an ampersand. For example:

yourinput=How+do+you+pronounce+meep (no break)
%3F&yourinput2=who+put+all+those+things+in+your+head

Well, the code (let me reprint it):

foreach (split(/&/, $input))
{
    ($NAME, $VALUE) = split(/=/, $_);
    $NAME =~ s/\+/ /g;
    $NAME =~ s/%([0-9|A-F]{2})/pack(C,hex($1))/eg;
    $VALUE =~ s/\+/ /g;
    $VALUE =~ s/%([0-9|A-F]{2})/pack(C,hex($1))/eg;
    print "$NAME : $VALUE <BR>\n";   
}

splits the input along the ampersands. Then, you end up with separate name=value elements. For each one of them, you split again...this time along the equal signs. During the second split, the stuff before the equal sign is assigned to the variable "$NAME", and the stuff after is assigned to "$VALUE".

Next comes what perl is famous for...text manipulation. For both "$NAME" and "$VALUE", all plus signs are substitued with spaces, and anything that matches the pattern "%xx" is replaced by the character who's ASCII value is "xx".

Finally, it prints out the name stored in "$NAME", and the value stored in "$VALUE":

yourinput : How do you pronounce meep?

And now you can write any other CGI program you want, simply by tacking stuff onto this.

Do you have to know all this? Well, not really. But it's nice to know what is going on. With the latest version of perl (perl 5), you can get something called CGI.pm, which does all the reading and substituting for you.

But the main point is, it's not particularily hard to write the mysterious CGI program. With about ten lines (or two with CGI.pm), you can write a functioning CGI program that is the basis of ALL CGI programs, big or small.

The secondary point is, I've filled my quota of words.

Gary Lee is a co-owner of meep! media inc., an Internet and Intranet consulting company. He is also one of the programmers, and creator of meep! media's first product, meep!Board, a message board system. Gary is the Editor of Web Tech, one of meep! media's online publications.


Back to the Web Developer's Journal
Contact WDJ   •    Suits!   •    Propheads!   •    Ponytails!
Masthead   •    Subscribe   •    Search

internet.com
Internet News   Internet Stocks/VC   Internet Technology   Windows Internet Tech.   Linux/Open Source   Web Developer   E-Commerce/Marketing   ISP Resources   Downloads   Internet Resources   Internet Lists   International      Search internet.com   Advertising Info   Corporate Info   Internet Trade Shows  
internet.commerce
Be a Commerce Partner   Register Domains   Accept Credit Cards   Track Time & Expenses   Freelancers   Small Biz Purchasing   Content for Websites   Hosting Services   Free Disk Space   Find a Web Host   Create a Web Store   
WebDeveloper Network
BrowserWatch   Java Boutique   JavaScript Source   ScriptSearch  SearchEngineWatch   StreamingMedia World   The WDVL   WebDeveloper.com  Developer Forums
Developer News   WebReference.com   The XML Files  
  
Copyright 1999-2000 internet.com Corp. All Rights Reserved. Legal Notices, Reprints.