Perl and CGI for beginners

As a beginner in Perl and CGI myself, I'm obviously the best possible person to discuss the topic! This page gives a conceptual overview of Perl and CGI, with links to resources I've used to get Perl/CGI scripts working fast.

1. CGI

Common Gateway Interface (CGI) is a way to use World Wide Web pages to run programs on a host machine. If you've ever done a Web search using Yahoo or its many competitors, you've used CGI.

CGI is a two-step dance: a web page calls and a web server responds.

1A. The web page calls. Basically, the calling web page includes a <form method> tag that names the CGI program to be run. When you submit the form (I'll discuss below how to do this), the program that you've named will be executed - wherever it may live on the web. On the Shannonizer home page, the relevant code looks like this:

<form method="GET" action="http://www.nightgarden.com/c11.cgi">

As you may have guessed, "c11.cgi" is the name of the CGI program that we want to run on the "www.nightgarden.com" server. This happens to be the same server that delivered the calling web page, but it could be any server you please.

To boss the CGI script around a little more effectively, the user can pass parameters using "input" and "select" tags. The following piece of code paints a field on your page which is 30 characters wide. Whatever the user types into this field will be passed to the CGI program as a parameter named "url":

Enter a URL: <input name="url" size=30>

This next piece of code creates a list box on your page. Only one entry at a time will be shown; the two possible items on this particular list are "Lewis Carroll" and "Raymond Chandler." Whichever one you select will be passed to the CGI script as a parameter named "editor."

Choose an editor: <select name="editor" size=1> <option>Lewis Carroll <option>Raymond Chandler</select>

All you need now is some kind of "submit" button for the user to press to get the show on the road:

Press the button to <input type="submit"> <value="START!">

When the user clicks that button, the web page sends out an http call that looks like this:

http://www.nightgarden.com/c11.cgi?url=cat.htm&editor=Lewis+Carroll

That cryptic stuff after the question mark contains the parameters that the user selected. (Note that the parameters are slightly coded for ease of transmission; the space automatically turns to "+" and so on. The called CGI program has to decode this parameter string before using it.)

1B. The web server responds. All that wonderful HTML has now been utterly wasted... unless there's an appropriate CGI script waiting on a functional web server machine somewhere. The last line of the code above means (roughly): "Find the program called 'c11.cgi' on a web server called 'www.nightgarden.com.' Run that program using the two parameters 'url=cat.htm' and 'editor=Lewis Carroll,' and send the result right back here to my browser."

You can write your own CGI scripts or borrow existing ones. (See below if you want to write your own scripts in Perl.) Either way, though, you'll need to set up the CGI script on your server machine so that it responds to web requests.

If you lease web space on a commercial Internet Service Provider (ISP), ask your provider what you need to do to set up a CGI script. Simply uploading the script to your home directory is usually not enough. At the very least, you'll probably have to set the permissions to make the script "world executable," which means that any joe on the net is allowed to run it. Note also that many ISPs are reluctant to let subscribers run amok with CGI scripts, because of possible security hazards. For this reason, CGI use may be discouraged or limited. One ISP I know of requires all scripts to be approved by their technical support staff before use.

If you're running your own web server and don't know how to set up CGI scripts... well, that's what documentation is for (good luck).

Links for learning CGI

2. Perl

You don't have to write CGI scripts in Perl. You can use pretty much any language you like, so long as your web server can run it. Perl (which stands for Practical Extraction and Report Language - remember that for your next appearance on Geek Jeopardy) has been around for years and is a very popular language for CGI for some very good reasons: Let's go back to the first point for a minute. There's probably a copy of Perl sitting around on your web server. This is a good thing, because Perl is an interpreted language, which means that a Perl program won't execute itself; it needs to be run through the Perl interpreter. For this reason, CGI scripts in Perl usually start with a line that looks something like this:

#!/usr/local/bin/perl

This line tells the UNIX machine where to find the Perl interpreter that actually runs the thing. If you omit this line, the script won't work as CGI. The web page may call the script, but the script doesn't know that it's supposed to be interpreted by Perl (nor where to find that Perl interpreter), so it just sits around, twiddling its digital thumbs. Very sad.

I won't go into the details of writing a Perl script here, as the links below give some excellent tutorials. But I would like to mention the popular Perl libraries used by the Shannonizer for the nuts and bolts of CGI calls.

Links for learning and writing Perl


[Home] . . [How it works] . . [Claude Shannon] . . [More fun & games]
[What do you think?] . . [Help!]