Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Guestbook

Posted on 2004-09-26
5
Medium Priority
?
1,217 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 400 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

Tech or Treat! - Giveaway

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

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

It's sometimes a bit tricky to use date functions in Oracle BPEL. I'll explain quickly how you can add N days to the current date. In a BPEL process this can be useful, and you can adapt it to fit your needs. First of all, let's see how to add 1 …
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.
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…

596 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