Guestbook Preliminaries

Here’s a guided tour of what’s going on at the top of the guestbook script. First, we specify the name of the file where we will save the guestbook entries:

my $data_file = '/w1/l/lies/begperl/data/guestbook.txt';

(Your own guestbook file will probably reside somewhere else.) We’ll be discussing appropriate permissions for this file toward the end of this chapter.

Next, we specify how many guestbook entries we want to save in that guestbook file:

my $max_entries = 50; # how many guestbook entries to save?
                      # set to '0' (zero) for infinite entries...

This is designed to prevent the guestbook from using up all our disk space. Even if we don’t think our guestbook is likely to be that popular, we can’t overlook the possibility that someone will attempt to use it maliciously. If $max_entries is set to a nonzero value, the script won’t let the guestbook have more entries than that value; once that number is reached, the oldest entry will be deleted every time a new entry is added. If you really want to you can set $max_entries to zero (0) in order to disable this feature, but don’t say I didn’t warn you.

Next we pull in the CGI module, using the same line we saw back in Chapter 3’s form-to-email-gateway example. We also pull in another module, Fcntl , which is going to help us do our file locking:

use CGI qw(:standard);
use Fcntl qw(:DEFAULT :flock);

Next, we print out the top of our HTML page:

print header, <<"EOF"; <HTML> <HEAD> <TITLE>My guestbook</TITLE> </HEAD> ...

Get Perl for Web Site Management now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.