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 Code

You’ve 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 let’s 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 you’ll begin following now for Perl header files that can be used over and over by many programs. It’s 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. You’ll need these variables to list the guest book entries, too. Rather than duplicate them in two programs, it’s 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&");

What’s this die function and why does it follow a logical OR symbol?

This is another of Perl’s 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 encountered—errors 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 doesn’t work, call die and terminate the program.” In other words, if require can’t find the file that is its argument, control goes to die, the message is printed, and the program exits.


TIP:  The Perl global variable $! always contains the value of the last system error. die understands this. If you put $! in its message string, it will print a semi-explanatory message all by itself. (Sometimes you may not find them too helpful!)


NOTE:  die is probably used most often with open; if the file can’t be opened, die prints a message explaining why and exits. die’s less fanatical little brother is warn, which operates the same way with the same argument, but doesn’t terminate the program.

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 employee’s 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. Here’s how pack works to make it possible.


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.