![]() |
![]() |
![]() |
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
![]() |
![]() |
To access the contents, click the chapter and section titles.
Perl CGl Programming: No experience required.
Dissecting the Display ProgramIts exhilarating to get something to work, isnt it? Now comes the sticky part: What have you done? Actually, there isnt much in guestbook.pl that you havent 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 reads LENGTH parameter uses $GuestEntrySize instead of a hard-coded number; this is another example of the wisdom of placing constantthat is, non-variablevalues 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 its 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.
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 cant do much with them. Perl provides localtime to break the huge time values down into something you can use in your programs. localtimes specification looks like this: ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = localtime (time); where
localtimes 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 dont print times such as 11:6:5.
|
![]() |
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. |