In this first article, I introduce you to the elements of CGI scripting using the basic CGI module, CGI.pm. In subsequent articles, I cover the more advanced CGI::* library, a collection of modules providing an object-oriented class hierarchy which gives you more control over the behavior of CGI scripts.
CGI stands for Common Gateway Interface; it’s the standard way to attach a piece of software to a World Wide Web URL. The majority of URLs refer to static files. When a remote user requests the file’s URL, the web server translates the request into a physical file path and returns it. However, URLs can also refer to executable files known as CGI scripts. When the server accesses this type of URL, it executes the script, sending the script’s output to the browser. This mechanism lets you create dynamic pages, questionnaires, database query screens, order forms, and other interactive documents. It’s not limited to text: CGI scripts can generate on-the-fly pictures, sounds, animations, applets, or anything else.
Basic CGI scripts are very simple:
#!/usr/bin/perl print "Content-type: text/html\015\012"; print "\015\012"; chomp($time = `date`); print <<EOF; <HTML><HEAD> <TITLE>Virtual Clock</TITLE> </HEAD> <BODY> <H1>Virtual Clock</H1> At the tone, the time will be <STRONG>$time</STRONG>. </BODY></HTML> EOF
This script begins by printing out an HTTP header, which consists of a series of email style header fields separated ...