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


Displaying the Guest Book

Listing the entries in your guest book file is less challenging than getting them into the file to begin with. You just do everything in reverse.

Still, there are some perplexing details you’ll need to deal with. The steps are straightforward:

1.  Set up variables to hold the information you’ll read from the file. Obviously, you need to know how the variables are structured, but you took care of that in addguest.pl.
2.  Open the guest book file for reading.
3.  Read one record. You need to know its length, but you’ve taken care of that, too.
4.  Format the data to your liking in HTML.
5.  print it out to the Web server.
6.  If there are any more records in the file, go back to Step 3.
7.  If not, close the file.

You’re finished.

Displaying the Guest Book with Perl

Save the following Perl code as guestbook.pl in a directory accessible to your CGI pipeline.

   #!perl/bin/perl

   # guestbook.pl
   #
   # Reads records from the guest book file specified in
   # guestbook.pm, formats them in HTML and sends them
   # to a Web page.

   # Get the header file; scream loudly and exit if it can't be
   # found. Otherwise, define the title string.

       require ("d:/pub/scripts/perl-cgi/GuestBook.pm") ||
           die ("Can't find GuestBook header file: $!\n");

       $Title = "Perl-CGI Guest Book Entries";

   # Attempt to open the guest book file.  Again, this is
   # a fatal error if it doesn't succeed.

       open (GUEST_LOG, $GuestBookPath) ||
            die "Can't open guest book: $!";

   # Set up the HTML document.

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

   # Read records and display them in a while loop. The test
   # at the top of the while block fails when all the records
   # have been read.

       while (read (GUEST_LOG, $buffer, $GuestEntrySize))
          {

       # Use unpack to load the record into an array of fields based
       # on the same template we used with pack to format them for
       # the file.

           @InfoArray = unpack ($GuestEntryStruct, $buffer);

       # Loop through the elements of the array and remove any NULL
       # padding from the strings, in case this is being run on a browser
       # that prints spaces for NULLs.

           for ($n = 0; $n < ($NumElements - 1); $n++)
               {
               $InfoArray[$n] =∼ s/\0//g;
               }

       # Load separate variables with the elements in @InfoArray.

           ($FirstName, $LastName, $City, $State, $Country, $Email,
                $Comments, $NumAccessTime) = @InfoArray;
           print "<STRONG>Name:</STRONG><BR>\n";
           printf ("%s %s<BR>\n", $FirstName, $LastName);
           print "<STRONG>E-mail address:</STRONG><BR>\n";
           print $Email, "<BR>\n";
           print "<STRONG>From:</STRONG><BR>\n";
           print $City, " ", $State, " ", $Country, "<BR>\n";
           print "<STRONG>On:</STRONG><BR>\n";

    # Set up a string time description after running the 4-byte
    # time value through localtime ().

        ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst)
            = localtime ($NumAccessTime);

        print "$WeekDay[$wday], $Month[$mon] $mday, ", $year + 1900;
        print " at $hour:";
        if ($min < 10)
            {
            print "0";
            }
        print "$min:";
        if ($sec < 10)
            {
            print "0";
            }
        print "$sec <BR>\n";
        print "<STRONG>Comments:</STRONG><BR>\n";
        print $Comments, "<BR>\n";
        print "<HR>\n";
        }

     close (GUEST_LOG);
     &HTML_Footer ();

   #                  End GuestBook.pl

Now add the following two arrays to the bottom of guestbook.pm:

   # Days of the week.

       @WeekDay = ("Sunday", "Monday", "Tuesday",
                        "Wednesday", "Thursday", "Friday",
                        "Saturday");

   # Months of the year.

    @Month = ("January", "February", "March", "April",
                     "May", "June", "July", "August",
                     "September", "October", "November", "December");

   #                    End GuestBook.pm

Start up your Web browser and connect with guestbook.html. Fill in the form a few times with bogus or real entries. You should see something similar to what is illustrated in Figure 7.5.


Figure 7.5:  Fill out the form again.


TIP:  If you click your browser’s Back button after submitting the form, you will go back to the form page. Click Clear Form to get a blank one up and do it again.

Finally, click the link on your Thank-you page that invokes the display script (that’s guestbook.pl). You’ll see something similar to the illustration in Figure 7.6.


Figure 7.6:  Running out the guest book entries


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.