![]() |
![]() |
![]() |
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
![]() |
![]() |
To access the contents, click the chapter and section titles.
Perl CGl Programming: No experience required.
Adding Guest Book EntriesGathering and filing an entry from the guest book form is a straightforward job in Perl. Lets create a program called addguest.pl. #!/perl/bin/perl require ("d:/pub/scripts/perl-cgi/GuestBook.pm") || die ("Can't find GuestBook header file: $!\n"); # Get the POSTed information from STDIN and put it in $post_info. read (STDIN, $post_info, $ENV {'CONTENT_LENGTH'}); # Split off the fields, which are delimited with '&', # and put them in @InfoArray. @InfoArray = split (/&/, $post_info); # Go through each element in @InfoArray, split off the # "variable=" part, then translate pluses into spaces and # any escaped hex chars back into their real character values. for ($n = 0; @InfoArray[$n]; $n++) { ($dummy, $temp) = split (/=/, @InfoArray[$n]); $temp =∼ tr/+/ /; $temp =∼ s/%([\dA-Fa-f][\dA-Fa-f])/pack ("C", hex ($1))/eg; @InfoArray [$n] = $temp; } # Now well check to see if we have anything to write # to the guest book. We need a first or last name, at # least; otherwise, we'll jump around the routines that # write this stuff to the guest book file. if ((length (@InfoArray[$FirstNameIndex]) != 0) || (length (@InfoArray [$LastNameIndex]) != 0)) { # Tack the current time to the end of the array. $time = time (); # Get the current time. @InfoArray[$NumEntryTime] = $time; # Put it in the array. # Pack the data into a binary structure, open the guest book # file for appending, and write it all out to disk. $GuestEntry = pack ($GuestEntryStruct, @InfoArray); open (GUEST_LOG, ">>$GuestBookPath") || die "Can't open guest book: $!"; print GUEST_LOG $GuestEntry; close (GUEST_LOG); } # End if ((length ) # Finally, we put up a cute little HTML document announcing # that everythings done, with a link to the guest book viewer. &HTML_Header ("All done!"); print "<BODY>\n"; print "<CENTER>\n"; print "<H1>Thanks for taking the time to "; print "sign our guest book </H1>\n"; print "<HR>\n"; print "Click <A HREF=\"/scripts/perl-cgi/guestbook.pl\ ">here</A> "; print "to view the guest book.<BR>\n"; &HTML_Ender (); # End addguest.pl You probably noticed a new require statement at the top of the program. Put this code in another file in the same directory as addguest.pl. # GuestBook.pm # # Header file for the routines to add data to and read it back from # the Web page Guest Book. # require ("d:/pub/scripts/perl-cgi/html.pm") || die ("Can't find header file\n"); # Some useful constants. # Format for pack () $GuestEntryStruct = "a30a30a30a30a30a30a256l"; # Path to guest book file $GuestBookPath = "/pub/http/perl-cgi/ngbook.dat"; # Indexes of elements in the packed structure. $FirstName = 0; $LastName = 1; $City = 2; $State = 3; $Country = 4; $EMail = 5; $Comments = 6; $NumEntryTime = 7; $NumElements = 8; # Number of elements in structure. $RecordSize = 440; # All the sizes added up. # End GuestBook.pm Save this file as guestbook.pm (note the pm). Rename html.pl to give it a .pm extension. Now connect with your Web server and guestbook.html. Fill out the form, as shown in Figure 7.3.
Now submit the form. Youll see a page similar to the one shown in Figure 7.4.
|
![]() |
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. |