##
#!/usr/bin/perl -w
use strict;
use CGI qw(:all);
use Fcntl qw(:flock);
# Location of the guestbook log file. Change this to suit your needs
my $gbdata="http://www.perl03.georgeself.com/cgi-bin/forms.txt";
# Any file name will do for semaphore.
my $semaphore_file="/tmp/helpdesk.sem";
# Function to lock (waits indefinitely)
sub get_lock {
open(SEM, ">$semaphore_file")
|| die "Cannot create semaphore: $!";
flock(SEM, LOCK_EX) || die "Cannot lock: $!";
}
# Function to unlock
sub release_lock {
close(SEM);
}
# This function saves a passed-in help desk HTML form to a file
sub save {
get_lock();
open(GB, ">>$gbdata") || die "Cannot open $gbdata: $!";
print GB "name: ", param('name'), "\n";
print GB "type: ", param('probtype'), "\n";
print GB "problem: ", param('problem'), "\n";
print GB "time: ", scalar(localtime), "\n";
close(GB);
release_lock();
}
# This function displays the contents of the help desk log file as HTML,
# with minimal formatting.
sub display {
open(GB, $gbdata) || die "Cannot open $gbdata: $!";
while(){
print "$_"; # The name
my($type,$prob);
$type=; # Assumes that each entry is
$prob=; # exactly 3-lines long...
my $time=;
print "$type";
print "$prob
";
print $time;
}
close(GB);
}
print header;
# The parameter 'submit' is only passed if this CGI program was
# executed by pressing the 'submit' button in the form in listing 22.7
if (defined param('submit')) {
save;
display;
} else {
display;
}