Документация
HTML CSS PHP PERL другое

[Chapter 19] CGI Programming

 
PreviousChapter 19Next
 

19. CGI Programming

Contents:
The CGI.pm Module
Your CGI Program in Context
Simplest CGI Program
Passing Parameters via CGI
Less Typing
Form Generation
Other Form Elements
Creating a Guestbook Program
Troubleshooting CGI Programs
Perl and the Web: Beyond CGI Programming
Further Reading
Exercises

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 fires up a program at the web server that examines your input and generates new output. Sometimes this program (commonly known as a 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. Since 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.

19.1 The CGI.pm Module

Starting with the 5.004 release, the standard Perl distribution includes the all-singing, all-dancing CGI.pm module.[1]

[1] If you have an earlier release of Perl (but at least Version 5.001) and haven't gotten around to upgrading yet, just grab CGI.pm from CPAN.

Written by Lincoln Stein, author of the acclaimed book How to Setup and Maintain Your Web Site, 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 UNIX and Linux to VMS; it even runs on systems like Windows and the MacOS.

Assuming CGI.pm is already installed on your system, you can read its complete documentation in whatever fashion you're used to reading the Perl manpages, such as with the man (1) or perldoc (1) commands or as HTML. If all else fails, just read the CGI.pm file: the documentation for the module is embedded in the module itself, written in simple pod format.[2]

[2] Pod stands for "plain old documentation," the simplistic mark-up used for all Perl documentation. See the perlpod (1) manpage for how it works, plus pod2man (1), pod2html (1), or pod2text (1) for some of the pod translators.

While developing CGI programs, keep a copy of the CGI.pm manpage handy. Not only does it describe the module's functions, it's also loaded with examples and tips.


PreviousHomeNext
18.4 ExerciseBook Index19.2 Your CGI Program in Context

© 2000- NIV