Link to home
Start Free TrialLog in
Avatar of varuncvarada
varuncvaradaFlag for United States of America

asked on

Guestbook

Hey guys

I have done this perl script and it has some errors when I execute it so can anyone help me?

Script:

#!/usr/bin/perl -w

use 5.004;
use strict;
use CGI qw(:standard);
use Fcntl qw(:flock);

sub bail {
    my $error = "@_";
    print h1("Unexpected Error"), p($error), end_html;
    die $error;
}

my(
    $CHATNAME,
    $MAXSAVE,
    $TITLE,
    $cur,
    @entries,
    $entry,
);

$TITLE = "Simple GuestBook";
$CHATNAME = "guestbook.txt";
$MAXSAVE = 10;

print header, start_html($TITLE), h1($TITLE);

$cur = CGI->new();
if ($cur->param("message")) {
    $cur->param("date", scalar localtime);
    @entries = ($cur);
}

# open the file for read-write (preserving old contents)
open(CHANDLE, "+< $CHATNAME") || bail("Cannot open $CHATNAME because: $!");

# get exclusive lock on the guestbook (LOCK_EX == exclusive lock)
flock(CHANDLE, LOCK_EX) || bail("Cannot flock $CHATNAME because: $!");

while (!eof(CHANDLE) && @entries < $MAXSAVE) {
    $entry = CGI->new(\*CHANDLE);
    push @entries, $entry;
}
seek(CHANDLE, 0, 0) || bail("Cannot rewind $CHATNAME because: $!");
foreach $entry (@entries) {
    $entry->save(\*CHANDLE);
}
trunacate(CHANDLE, tell(CHANDLE)) || bail("Cannot truncate $CHATNAME because:$!");
close(CHANDLE) || bail("Cannot close $CHATNAME because: $!");

print hr, start_form;
print p("Name:", $cur->textfield(
    -NAME => "name"));
print p("Message:", $cur->textfield(
    -NAME => "message",
    -OVERRIDE => 1,
    -SIZE => 50));
print p(submit("Send"), reset("Clear"));
print end_form, hr;

print h2("Prior Messages");
foreach $entry (@entries) {
    printf("%s [%s]: %s",
    $entry->param("date"),
    $entry->param("name"),
    $entry->param("message"));
    print br();
}
print end_html;

Errors:

Bareword "CHANDLE" not allowed while "strict subs" in use at guestbook.pl
Execution of guestbook.pl aborted due to complication error

------------------------------------------------------------------------------------------------------------------
Avatar of nicholassolutions
nicholassolutions
Flag of United States of America image

This is not really a PHP question, so perhaps it should be moved by the moderators. At any rate, take a look at:

http://www.unix.org.ua/orelly/perl/prog3/ch31_19.htm

section 31.19.3 strict 'subs'.

There are essetially two ways to fix your problem. The first would be to add this line right below "use strict;" in your script:

no strict "subs";

The other would be to quote CHANDLE every time it is used. So instead of
open(CHANDLE, "+< $CHATNAME") || bail("Cannot open $CHATNAME because: $!");

use
open('CHANDLE', "+< $CHATNAME") || bail("Cannot open $CHATNAME because: $!");

Hope that helps.

Cheers,
Matt
Avatar of varuncvarada

ASKER

Thanks, I think that did it, but now it says:

Undefined subroutine &main::trunacate called at guestbook.pl

----------------------------------------------------------------------------------------------------------

Can you please help me with this? I don't know if this is a problem, but I just want to know.Thanks.
ASKER CERTIFIED SOLUTION
Avatar of nicholassolutions
nicholassolutions
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
It worked!!! Thanks a bunch!
glad to help ;)