![]() |
![]() |
![]() |
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
![]() |
![]() |
To access the contents, click the chapter and section titles.
Perl CGl Programming: No experience required.
The next statement, else, goes hand-in-hand with if. It forms kind of an otherwise clause for the if statement, saying execute my code if the statement(s) above is false. A variation of else combines the two conditional statements: elseif. A full conditional block of code using all three statements looks like this: if (This is true) { do this; } elseif (That is true) { do that; } else { do some other thing; } You can string as many of the tests together as you like. The conditionals are processed sequentially until one of them is true, then the rest of the full block is ignored. Another noteworthy aspect of this if-else block is the way the names are printed. Notice that, because of the way the descriptive indexes were set at the top of the program, you can get to the last name with $Value[$LastName] and so forth. It gets a tad more complicated with the other values, however, because the form shipped numbers to you to denote the radio buttons; you use the numbers to pick a string from the arrays you made in the beginning of the program. So, if $Value[$Sex] holds the number 1, you can say $Gender[$Value[$Sex]] to get $Gender[1], which is the string female. Likewise, if $Value[$FaveFilm] holds the number 0, you can easily use that to get element 0 from your @Film array: $Film[$Value[$FaveFilm]] = $Film[0] = Citizen Kane.
A last code block to consider in quiz.pl is this one: print "<H3>You"; if (!$OldStones) { print " don't"; } print " think the Rolling Stones are too old</H3>\n"; Remember that the $OldStones flag has been set or cleared depending on whether the Rolling Stones check box sent a value to the program or not. The exclamation point (!) in Perl is a logical NOT operator; in other words, the statement says if NOT $OldStones The value of $OldStones will be either 1 or 0, true or false, which are also the only possible results of the test made in an if statement. If $OldStones is 0, then NOT $OldStones would be its logical opposite, or true! The test succeeds; the dont is printed. Get it? Processing Forms in the Real WorldYoull probably encounter very few situations on your Web site where simply printing back the information a visitor has enteredno matter how prettily is the only thing youll want to do with it. In a real poll, you would want to store the data somewhere, probably in a file, adding the data from other visitors to it as they connected with your site and filled out the form. Then, you could pull up all of the data and process the results. quiz.pl and its HTML counterpart could be used in a survey of favorite films, for example, with very little modification. You wouldnt need the first- and last-name information at all. This is the kind of thing that keeps Web visitors coming back to your site. Remember once again: You are limited only by your imagination. If you can think of it, chances are it can be done. The Polling Booth Web site, described at the beginning of this skill, keeps track of the scores of visitors who take the Problem Solving IQ test. The scores are collated and can be displayed in a variety of ways. Take a look at this Web site again for some ideas on how you might display the results of your own quizzes. You could show the number of correct answers versus incorrect answers; you could calculate the percentage of correct answers and show the results graphically, with a bar or some other artifice. Again, if you feel insecure about your HTML skills, you can download the source code of the Polling Booth Web site pages with your Web browser. This code will show you how to set up the various fields of information on the Polling Booths pages; you can easily tailor the code to fit your own requirements.
Moving On In the next skill, youll learn how to stuff information from an HTML form into a Perl data structure and write it to a disk file. Youll design and implement a guest book for your Web site that visitors can both sign and display. Youll also learn the basics of Web security as you build your guest book program to ensure that harmful data cant be entered into it. Are You Experienced?Now you can
|
![]() |
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. |