ok im on chap19 (cgi programming) of learning perl 2nd edition.
I am just not understanding how the referances to a class work.
i understand the following example:
@array1=qw( 1 2 3 4);
$aref = \@array1;
@array2=qw(5 6 7 8);
$aref=\@array2;
$# of $aref is now 1
print $aref[1][2]; # this will print "7"
but when the example says..
$cur = CGI->new(); # this is where i get lost. what value is $cur holding? its a scalar value i know this. (i think) but what is new()?
and why is the module CGI referanceing it and saveing its values to $cur.
here is the example CGI script i dont understand the methods useage . this lesson is in makeing a cgi guestbook.
#! /usr/bin/perl -w
use 5.004;
use strict;
use CGI qw(:standard);
use Fcntl qw(:flock);
sub bail {
my $error = "@_";
print h1("Enexpected Error"), p($error), end_html;
die $error;
}
my (
$CHATNAME,
$MAXSAVE,
$TITLE,
$cur,
@entries,
$entry
);
$TITLE = "Simple Guestbook";
$CHATNAME= "chatfile";
$MAXSAVE = 10;
print header, start_html($TITLE), h1($TITLE);
$cur = CGI->new();
if ($cur->param("message")) {
$cur->param("date", scalar localtime);
@entries=($cur);
}
open(CHANDLE, "+< $CHATNAME") || bail("cannot open $CHATNAME: $!");
flock(CHANDLE, LOCK_EX) || bail("cannot flock $CHATNAME: $!");
while (!eof(CHANDLE) && @entries < $MAXSAVE) {
$entry = CGI->new(\*CHANDLE);
push @entries, $entry;
}
seek(CHANDLE, 0, 0) || bail("cannot rewind $CHATNAME: $!");
foreach $entry (@entries) {
$entry->save(\*CHANDLE);
}
truncate(CHANDLE, tell(CHANDLE)) || bail ("cannot truncate $CHATNAME: $!");
close(CHANDLE) || bail ("cannot close $CHATNAME: $!");
print hr, start_form;
print p("Name:", $cur->textfield(
-NAME => "message"));
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;