![]() |
![]() |
![]() |
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
![]() |
![]() |
To access the contents, click the chapter and section titles.
Perl CGl Programming: No experience required.
Processing the Quiz FormBefore you move on to the hard stuff, lets 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 environmentyou can see it in the browser windowPOST puts it into your Perl programs 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. Its more accurate for what you will be doing. POST and STDINBecause 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 courseyou 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:
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);
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.
The Results Are In: Now What?So far, youve 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 dont do much. Surely theres something more you can do with the form information, isnt 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 thats the whole point, really. Its 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 wouldnt 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 PollingNotice 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.
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 visitors information in a more friendly manner, you could easily use the values passed by the form as indexes in an array.
|
![]() |
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. |