Solved

Guestbook

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Article by: Matthew
I am a very big proponent of technology compliance standards and strive to meet such criteria in all of my work. That includes my site, which is 100% XHTML 1.0 compliant as determined by the World Wide Web Consortium. https://www.matthewstevenkel…
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.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

863 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

27 Experts available now in Live!

Get 1:1 Help Now