18. CGI Programming
Unless you've been holed up in a log cabin without electricity for the last few years, you've heard of the World Wide Web. Web addresses (better known as URLs) pop up everywhere from billboards to movie credits, from magazines and newspapers to government reports.
Many of the more interesting web pages include some sort of entry form. You supply input to this form and click on a button or picture. This step fires up a program at the web server that examines your input and generates new output. Sometimes this program (commonly known as a Common Gateway Interface (CGI) program ) is just an interface to an existing database, massaging your input into something the database understands and massaging the database's output into something a web browser can understand (usually HTML).
CGI programs do more than process form input. They are also invoked when you click on a graphic image and may in fact be used to provide whatever output that your browser sees. Instead of being dull and boring, CGI-enabled web pages can be marvelously alive with dynamic content. Dynamic information is what makes the Web an interesting and interactive place, and not just a way to read a book from your terminal.
Despite what all those bouncing balls and jumping adverts might lead you to believe, the web contains a lot of text. Because we're dealing with text, files, network communications, and a little bit of binary data now and then, Perl is perfect for web programming.
In this chapter, we'll not only explore the basics of CGI programming, but we'll also steal a little introductory knowledge about references, library modules, and object-oriented programming with Perl as we go along. Then, at the end, we'll make a quick survey of Perl's usefulness for other sorts of web programming.
As a standalone tutorial, this chapter (and most any other document shorter than a couple of hundred pages) will not be adequate to teach the more complex topics touched on here, such as object programming and the use of references. But as a means to gain a preliminary taste of what's ahead of you, the examples presented here, together with their explanations, may whet your appetite and give you some practical orientation as you slog through the appropriate textbooks. And if you're the learn-by-doing type, you'll actually start writing useful programs based on the models you find here.
We assume you already possess a basic familiarity with HTML.
Starting with the 5.004 release, the standard Perl distribution includes the all-singing, all-dancing CGI.pm module.[ 1 ]
Written by Lincoln Stein, author of the acclaimed book How to Setup and Maintain Your Web Site (Addison-Wesley), this module makes writing CGI programs in Perl a breeze. Like Perl itself, CGI.pm is platform independent, so you can use it on systems running everything from Windows NT to UNIX and VMS.
Assuming CGI.pm is already installed on your system, you can read its complete documentation in the included HTML documentation. If all else fails, just read the source file for CGI.pm : the documentation for the module is embedded in the module itself, written in simple pod format.[ 2 ]
While developing CGI programs, keep a copy of the CGI.pm documentation handy. Not only does it describe the module's functions, it's also loaded with examples and tips.