![]() |
![]() |
![]() |
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
![]() |
![]() |
To access the contents, click the chapter and section titles.
Perl CGl Programming: No experience required.
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.
The result is shown in Figure 6.6.
This is the biggest Perl program you have written so far, and it brings up several new concepts. Lets cover them from the top.
The New Quiz Form ProcessorThe advantage of designing your own HTML documents and CGI programs is that you generally know whats going to what from what. In other words, you made the quiz form, so there are no mysteries about whats in it when you design a Perl program to deal with it. The only thing you dont 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; Youre looking for your visitors:
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 well 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 theyre 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;
|
![]() |
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. |