Solved

Guestbook

Posted on 2004-09-26
5
1,113 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
Browsers only know CSS so your awesome SASS code needs to be translated into normal CSS. Here I'll try to explain what you should aim for in order to take full advantage of SASS.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

839 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