- Shopping Bag ( 0 items )
A complete guide to creating interactive Web pages using the Perl CGI.pm library from its creator.
Official Guide to Programming with CGI.pm contains all the information about CGI.pm found in Perl's online manual, but in a completely reorganized and greatly expanded format. It also includes valuable labor-saving tips and hints you won't find anywhere else. Featuring step-by-step instructions and complete source code, it shows you how to:
* Download, install, and configure CGI.pm
* Generate HTML documents on the fly
* Process fill-out forms and create multipage documents
* Perform script debugging and state maintenance
* Design interactive clickable image maps
* Store and process cookies
* Write state-maintaining scripts
* Modify and extend CGI.pm
* Tap the Comprehensive Perl Archive Network (CPAN).
Visit the companion Web site at www.wiley.com/compbooks/stein
On the Web site you'll find:
* The most recent and all future revisions to the CGI.pm module
* All the source code examples from the book
* Online documentation for CGI.pm
* Links to CPAN and other Web-related software written by the author.
Visit our Web site at www.wiley.com/compbooks/
The easiest way to learn CGI.pm is to use it. This chapter shows you the basics by starting simple and gradually growing complex. Later chapters will teach the theory behind CGI scripting.
In order to run the examples in this chapter, you'll need to have:
The examples in this chapter are available at this book's companion Web site at John Wiley & Sons. Load URL www.wiley.com/compbooks/stein and follow the links to the source code examples.
Displaying a Plain Text File
The first four examples create plain text files on the fly and display them. They're as simple as CGI scripts get. Start your favorite text editor, and type in the following code:
Most Web servers require that CGI scripts be saved to a particular scripts directory. On UNIX systems, this directory is often named cgi-bin. On Windows systems, it may be called cgi-bin, scripts, or even cgi-standard. Identify this directory and save the file to it using the name script1.pl. If you are on a UNIX system, you will need to make the file executable (by typing chmod +x script1.pl). You should also make sure that the script is world readable (chmod o+w script.pl in UNIX, or by changing the access control list to allow reading by everyone in Windows NT).
First test the script from the command line. Type the following:
If all goes well, the script will run and print out the following two lines of text:
Nothing to it!
We will cover the meaning of this text later.
If you get an error message, go back and confirm that Perl is installed correctly, that the PATH variable includes the location of the Perl executable, and that the CGI library is installed.
Now you are ready to run the script from the Web server. Open up a Web browser and point it at the URL of the script. This may be slightly different from server to server, but on most servers, you'll enter something similar to www.yoursite.com/cgi-bin/plaintext.pl.
The browser will ask the server to fetch your script's URL, the server will launch and run the script, and the script will return the document shown in Figure 2.1.
If the browser displays an error message, go back and check that the permissions of the script allow everyone to read and execute it. The Web server runs as an unprivileged user, and occasionally its privileges will deny the server permission to access a file that you can read under your own user account. If you still get an error, go to the Troubleshooting section later in this chapter.
Let's look at the script in detail:
The second string is the phrase "Nothing to it!" This becomes the contents of the document, often called the body. The document content is separated from the header by a blank line automatically provided by CGI.pm. . . .
Getting Started with CGI.pm.