Solved

Guestbook

Posted on 2004-09-26
5
1,032 Views
Last Modified: 2010-07-27
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

------------------------------------------------------------------------------------------------------------------
0
Comment
Question by:varuncvarada
  • 3
  • 2
5 Comments
 
LVL 15

Expert Comment

by:nicholassolutions
ID: 12156367
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
0
 
LVL 1

Author Comment

by:varuncvarada
ID: 12245256
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.
0
 
LVL 15

Accepted Solution

by:
nicholassolutions earned 100 total points
ID: 12245583
probably this is the problem:

trunacate(CHANDLE, tell(CHANDLE)) || bail("Cannot truncate $CHATNAME because:$!");

this should be

truncate(CHANDLE, tell(CHANDLE)) || bail("Cannot truncate $CHATNAME because:$!");
0
 
LVL 1

Author Comment

by:varuncvarada
ID: 12264710
It worked!!! Thanks a bunch!
0
 
LVL 15

Expert Comment

by:nicholassolutions
ID: 12265522
glad to help ;)
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
JavaScript has plenty of pieces of code people often just copy/paste from somewhere but never quite fully understand. Self-Executing functions are just one good example that I'll try to demystify here.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now