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


Dissecting the Display Program

It’s exhilarating to get something to work, isn’t it? Now comes the sticky part: What have you done?

Actually, there isn’t much in guestbook.pl that you haven’t already done. The Perl read function that gets the records from the file is set up in a while loop that terminates when read reaches the end of the file.

   # 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))
           {

Notice that read’s LENGTH parameter uses $GuestEntrySize instead of a hard-coded number; this is another example of the wisdom of placing constant—that is, non-variable—values in one place, where they can be changed easily.

Each record read from the file is unpacked into an array, as you learned previously.

for Loops, NULLs

The code block that follows the call to unpack introduces the Perl for statement. for is similar to foreach in that it sets up a loop. But where foreach is ideal for pulling elements out of arrays until they are empty, for provides a great deal more control over many more data structures than foreach.

for uses a counter variable, $n in this example, and a terminating condition to decide when it’s finished. The for specification looks like this:

   for (starting count; terminating condition; do something with count)
       {
       Do this code block;
       }

for is especially good for stepping through subscripted arrays, as you do in the for block in guestbook.pl.

The purpose in this example is to go through each of the string members of @InfoArray and strip out the trailing NULL characters that were used to pad them when they went through pack.


NOTE:  It is not strictly necessary to strip the NULLs out of formatted strings. However, some Web browsers regard NULLs as spaces and print them out in an HTML document. It is safer—and quite easy, as you will see—to simply get rid of them.

The for starts the counter, $n, at 0. It will terminate when $n equals $NumElements - 1, which is all of the elements in the array except for the last one (the long-integer value denoting the time the guest book entry was made). Each time through the loop, $n++ is executed, which increments it.

The elements go through a regular expression substitution in the body of the loop.

   $InfoArray[$n] =∼ s/\0//g;

The substitution operator in this case searches globally (note the g at the end) for all occurrences of \0, the NULL character, and replaces them with nothing, which effectively removes the NULLs from the string.

Keeping Time

The last bit of new stuff in guestbook.pl is the Perl function localtime. Four-byte integers denoting billions of seconds from some epoch may be quite meaningful to the computer as a way of telling time, but humans can’t do much with them. Perl provides localtime to break the huge time values down into something you can use in your programs.

localtime’s specification looks like this:

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

where

  $sec is the number of seconds past the minute
  $min is the number of minutes past the hour
  $hour is the hours past midnight
  $mday is the day of the month, starting from 0, not 1
  $mon is the numeric month, again starting from 0
  $year is the number of years from 1900, such as 97 for 1997
  $wday is the numeric day of the week, starting from 0
  $yday is the numeric day of the year
  $isdst is a flag indicating whether it’s daylight savings time
  time is the 4-byte integer

localtime’s return values are the impetus for the two arrays you added to guestbook.pm. You can use the numeric values from localtime as subscripts into these arrays to pull out the real names of days of the week and months as in guestbook.pl:

   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";

The conditionals testing $min and $sec for values less than 10 are intended to pad them out with 0s so you don’t print times such as 11:6:5.


TIP:  Here’s a question for turn-of-the-century buffs: What will happen to this code at midnight on January 1, 2000? Answer: Because $year is added to 1900, the date will suddenly plunge a century into the past. Remember this!


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.