Click Here!
home account info subscribe login search My ITKnowledge FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

Perl CGl Programming: No experience required.
(Publisher: Sybex, Inc.)
Author(s): Erik Strom
ISBN: 0782121578
Publication Date: 11/01/97

Bookmark It

Search this book:
 
Previous Table of Contents Next


Processing the Quiz Form

Before you move on to the hard stuff, let’s make some minor modifications to the Perl program you wrote in Skill 5, geturl.pl.

The main difference between the GET and POST methods is in their modes of delivery. Where GET actually places the data in a URL and ships it along with the command line in the environment—you can see it in the browser window—POST puts it into your Perl program’s standard input stream, as if you were typing it in from the keyboard.

So, geturl.pl will need a couple of changes to reflect the difference.

    # Get HTML header, ender, define the page title.

         require "/pub/scripts/perl-cgi/html.pl";  # Full path.
         $Title = "Get POST Information From A URL";

    # Get the length of the data.

   →     $DataLen = $ENV{‘CONTENT_LENGTH’};

    # Read the data from standard input.

   →     read (STDIN, $QueryString, $DataLen);

    # Use split to make an array of name-value pairs broken at
    # the ampersand character.

         @NameValuePairs = split (/&/, $QueryString);

Store this modified Perl script as quiz.pl. It’s more accurate for what you will be doing.

POST and STDIN

Because the form information comes from the standard input in a POST submission, you can use the Perl read function to get it into your program. You learned about read when you covered Perl and files in Skill 4. To recap, the function takes this form:

       read (HANDLE, BUFFER, LENGTH);

where HANDLE is a file handle, BUFFER is a Perl scalar variable to hold whatever is read, and LENGTH is the number of characters, or bytes, to read into the buffer.

BUFFER is easy, of course—you just declare a variable. Where do you get the HANDLE value? Perl makes that easy, too. Every time you run a Perl program, it predeclares a set of file handles that allow your program to interact with the operating system and open and close files. Among these are:

  STDIN: the standard input stream
  STDOUT: the standard output stream

As you can see in the program, STDIN is already opened for you. All you have to do is use it. Last, you have to tell read how many bytes are out there waiting for it. Luck-ily, the CGI environment contains a variable with just that information in it, CONTENT_LENGTH.

As a result of all this help from Perl and CGI, you have very little to do to modify your program to accept POSTed data. You first get the length of the data:

   $DataLen = $ENV{'CONTENT_LENGTH'};

then you use the length to read the data from the standard input into a local variable:

   read (STDIN, $QueryString, $DataLen);
Save an Instruction

You could save an instruction by not putting CONTENT_LENGTH in a local variable but instead using it directly in the read, thus:

   read (STDIN, $QueryString, $ENV{‘CONTENT_LENGTH’});

What’s better? It’s a matter of personal taste, really. However, if you plan to use the length later in the program it would be more efficient to store it now rather than continually looking at $ENV{‘CONTENT_LENGTH’}; less typing that way.

More importantly, because environment variables can be subject to change from forces other than your program, it makes sense to store these incoming values early in local variables.

These modifications accomplished, install quiz.pl in the usual CGI directory, then call up the form through your Web server, fill it out, and submit it. The form illustrated in Figure 6.3 will produce the HTML page shown in Figure 6.4 in your browser.


Figure 6.4:  The results of quiz.pl


TIP:  Are you thinking about the possibility of a subroutine that would distinguish between POST and GET and fill $QueryString accordingly? Good! You’ve caught the spirit of Perl.

The Results Are In: Now What?

So far, you’ve only duplicated what you did in Skill 5, using the POST submission method instead of GET. You have a list of nicely decoded name-value pairs displayed on a Web page, but they don’t do much.

Surely there’s something more you can do with the form information, isn’t there?

Of course there is. However, this is a point at which learning to program and the philosophy of programming must necessarily diverge a bit. If you were to coax a veteran programmer out of her cubicle and ask point-blank, “What do I do now?” the supremely annoyed reply would be something along the lines of “Whatever you want!” before she climbed back into her cube.

And that’s the whole point, really. It’s the reason people start programming in the first place, because they can dream something up and then tell the computer to do it. It might not work the first or second or third time through, but the problems will be solved eventually and, in a very real sense, the dream will come true.

The purpose of this book is not to hand you a bunch of canned programs to blindly punch into your Web site and run willy-nilly. You wouldn’t gain a thing that way, except perhaps improved typing skills. You will find foundations in this book, and hints, and enough of the technical basics so that you can find your way around the HTTP-CGI-Perl pipeline. But the ideas have to be yours. You have to decide what to do.

What do you do now? Whatever you want, of course!

Basic Polling

Notice that quiz.html assigns numeric values to each of its radio buttons. The values just as easily could have been descriptive strings, such as “Citizen Kane” for the “Citizen Kane” film-preference button.


TIP:  The radio button groups in quiz.html all have the same name. This is so they can be processed as groups. When you go through a group of buttons and click them, only the last one will stay highlighted. This is the single value that will be submitted to your Perl program.

The strings would be more descriptive than numeric values, but they would be more difficult to process in a program. If you wanted quiz.pl to display the visitor’s information in a more friendly manner, you could easily use the values passed by the form as indexes in an array.


Previous Table of Contents Next


Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited.