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


Extensive changes are required; crank up your text editor.

   #!/perl/bin/perl

   # quiz.pl
   #
   # A little Perl script to read, decode and print the names
   # and values passed to it from an HTML form through CGI.

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

        require "/pub/scripts/perl-cgi/html.pl";  # Full path.
        $Title = "Your Personal Preferences";
        $MaxData = 6;

   # Assign names to indexes to make them more descriptive.

        ($Sex, $HowOld, $FaveFilm, $OldStones, $LastName, $FirstName) =
            (0 .. 5);

   # Set up a flag for the Stones checkbox.

        $OldStones = 1;

   # Set up a series of arrays to handle the form data.

        @Gender = ("male", "female", "neither");
        @Age = ("12-18", "19-25", "26-30", "older than 30", "real old");
        @Film =
             (
             "Citizen Kane",
             "Benji",
             "Dawn of the Dead",
             "It's a Wonderful Life",
             "Intolerance",
             "none that we mentioned"
             );

   # 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. Then get the values.

        @NameValuePairs = split (/&/, $QueryString);
        $n = 0;
        foreach $NameValue (@NameValuePairs)
             {
             ($Name, $Value[$n]) = split (/=/, $NameValue);
             $n++;
             }

   # Set or clear $OldStones flag based on the number of pairs
   # counted in $n. The checkbox value won't be sent if it's
   # not checked. Adjust indexes, too.

        if ($n != $MaxData)
             {
             $OldStones = 0;
             $FirstName--;
             $LastName--;
             }

   # Put up an HTML header, page title and a rule.

        &HTML_Header ($Title);
        print "<BODY>\n";
        print "<H1>$Title</H1>\n";
        print "<HR>\n";

   # Print the information in a friendly manner.

        print "<H3>Your name is ";

   # See if anything has been entered in the name.

        if (($Value[$LastName] eq "") && ($Value[$FirstName] eq ""))
             {
             print "a mystery to us!\n";
             }
        else
             {
             print "$Value[$FirstName] $Value[$LastName]\n";
             }

        print "</H3>\n";
        print "<H3>You are a $Gender[$Value[$Sex]]</H3>\n";
        print "<H3>Your age is $Age[$Value[$HowOld]]</H3>\n";
        print "<H3>Your favorite film is ";
        print   "\"$Film[$Value[$FaveFilm]]\"</H3>\n";
        print "<H3>You";
        if (!$OldStones)
             {
             print " don't";
             }

        print " think the Rolling Stones are too old</H3>\n";

   # End the HTML document.

        &HTML_Ender;

   #                         End quiz.pl

Once again, install the new quiz.pl in a directory accessible to the Web server, connect with quiz.html through your Web browser, fill out the form, and submit it. Figure 6.5 illustrates an sample form.


Figure 6.5:  The quiz form revisited

The result is shown in Figure 6.6.


Figure 6.6:  A “friendlier” quiz form display

This is the biggest Perl program you have written so far, and it brings up several new concepts. Let’s cover them from the top.


NOTE:  You may have noticed right away that some code from Skill 5’s geturl.pl has been removed from the quiz.pl version: the lines that decode URL-encoded characters and turn + into spaces. Brevity is the main reason for this. The program assumes that the only text fields (for last name and first name) will include no spaces or special characters. The translation code should (and will) be included in all of your subsequent programs.

The New Quiz Form Processor

The advantage of designing your own HTML documents and CGI programs is that you generally know what’s going to what from what. In other words, you made the quiz form, so there are no mysteries about what’s in it when you design a Perl program to deal with it. The only thing you don’t know is what your visitor has entered in the form.

But you do know how many radio buttons it has, how many categories will be filled out, and the titles of the movies in the Favorite Film list. The friendly quiz form display takes advantage of this information, but you have to transfer it into your Perl program first.

The first new line in the program reflects an important bit of information you need: the number of categories you might expect to receive.

   $MaxData = 6;

You’re looking for your visitor’s:

1.  Gender
2.  Age
3.  Favorite film (from your list)
4.  Opinion of the Rolling Stones
5.  Last name
6.  First name

All of which add up to six categories. $MaxData will be used later in the program to check the actual number of categories sent, because of a quirk in HTML check boxes that we’ll discuss presently.

In a general way, you know already that all of the categories will be placed in an array based on the order in which they’re sent. You can make your programming life easier by assigning descriptive variable names to the categories. You know that they will be sent in the same order in which they are written in the HTML document, so you can assign indexes in quiz.pl:

   ($Sex, $HowOld, $FaveFilm, $OldStones, $LastName, $FirstName) =
   (0 .. 5);

Notice how we have taken advantage of a Perl trick to do the assignments in one line of code. You could have just as correctly written:

   $Sex = 0;
   $HowOld = 1;
   $FaveFilm = 2;


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.