![]() |
![]() |
![]() |
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
![]() |
![]() |
To access the contents, click the chapter and section titles.
Perl CGl Programming: No experience required.
Dissecting the CodeYouve gone through so many new concepts in the last two files that your head may be treading water by now. You have a working guest book entry form and CGI back-end, so lets slow down a little and take the program apart. The required file at the top (guestbook.pm) itself contains the require that brings in the familiar subroutines of html.pl, which itself is html.pm now. The pm extension for the file is a convention youll begin following now for Perl header files that can be used over and over by many programs. Its the extension used for Perl packages, a concept you will learn about in Skill 12. The scalar variables defined in guestbook.pm are not used only in addguest.pl. Youll need these variables to list the guest book entries, too. Rather than duplicate them in two programs, its easier to include all of them in one file. Note the require line: require ("d:/pub/scripts/perl-cgi/html.pm") || die ("Can't find header file\n&"); Whats this die function and why does it follow a logical OR symbol? This is another of Perls many handy shortcuts. die amounts to a bail out now! instruction, allowing you to print an explanatory message along with it. It is used when errors are encounterederrors so bad that the program cannot continue. Many Perl functions can be tied to die with the logical OR symbol. Used in this fashion, it means do this; if it doesnt work, call die and terminate the program. In other words, if require cant find the file that is its argument, control goes to die, the message is printed, and the program exits.
The constants defined in the rest of guestbook.pm are similar to the index values you set up in Skill 6 for quiz.pl. There is one unfamiliar exception: The $GuestEntryStruct appears to be a string of nonsense, but its comment line says it is a format for pack (). What could that be? Reading and Writing Structured Data pack and its Perl complement unpack are ubiquitous in programs that read and write binary data to and from disk files. They are necessary because Perl would like to believe that everything it deals with is in the form of a string. Oh, it can convert pretty easily between numbers and strings on an individual basis, but it falls apart when things get too complicated. In the real world, programmers run into situations all the time in which they have to manipulate pieces of information that are structured in records, each of which has a fixed length and each member of which has its own fixed length. For example, if you were putting an employee-information database together, you would fashion each employee entry with a certain size for each bit of information: The name would be 32 characters, the address might be 64, the employees age and salary would be two numeric fields, each taking 4-byte integers, etc. The entire entry would be a record with a length equal to the combined maximum sizes of each field. Each record would fit into the same amount of space in a disk file; each record could be called into another program and manipulated, then written back to the disk. The file into which all these records go is called a database. You have created a small one, in fact, with addguest.pl. Heres how pack works to make it possible.
|
![]() |
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. |