Re: Newbie "undefined value"

From: Mike Flannigan (mikeflan_at_earthlink.net)
Date: 10/09/03


Date: Thu, 09 Oct 2003 15:25:47 GMT


Nadine wrote:

> Hello,
>
> I get a "cannot use undefined value as symbol ref at line 45." I have
> used strict and -w. I am using CARP. From the command line, my syntax
> checks out as OK. From that same command line, if I pass in parameters,
> then I get the "undefined value." I thought "strict" would catch all
> undefined variables. I am using Perl 5 and Apache.
>
> I had a short script that printed to the browser. Now I am changing it
> to print to a file and using parts of Stein's guestbook script. Here is
> the resulting script.
>
> #!/usr/bin/perl -w
> use strict;
> use warnings;
> use CGI qw(:standard Carp qw(fatalsToBrowser)); # import shortcuts
> use Fcntl qw(:flock); # imports LOCK_EX, LOCK_SH, LOCK_NB
>
> my(
> $DATAFILE,
> $fh, # File Handle
> $TIMEOUT,
> $path_name,
> $path,
> $description,
> $lock_type,
> );
>
> # give path and name of data file
> $DATAFILE="Eval.dat";
>
> my $cgi = CGI->new();
> my $value = $cgi->param( 'keyword' ) || '';
>
> sub write_datafile {
> my $fh = filelock($DATAFILE,1);
> unless ($fh) {
> print strong('Sorry, an error occurred: unable to open file.'),p();
> Delete('action');
> print a({-href=>self_url},'Try again');
> return undef;
> }
> }
>
> # List field names in order.
> foreach my $item ( qw(orgclass relevance majorReq course) ) {
> my $value = $cgi->param( $item ) || 0;
> print $fh "$value . |\n";
> }
>
> # Generate web page saying thank you
>
> print $cgi->header,
> $cgi->start_html('Thank You'),
> $cgi->h2('Thank You'),
> $cgi->p("Thank you for your feedback."),
> $cgi->p("Return to ", a({-href=>'http://www-rohan.sdsu.edu/~ssrllab/'},
> "SSRL Lab home page")),
> $cgi->hr({-width=>"100%"}),
> $cgi->end_html;
>
> unlock($fh);
> 1;
>
> sub filelock {
> my $path = shift;
> my $for_writing = shift;
>
> my ($lock_type,$path_name,$description);
>
> $lock_type = LOCK_EX;
> $path_name = ">>$path";
> $description = 'writing';
> }
>
> local($main::msg,$main::oldsig);
> my $handler = sub { $main::msg='timed out';
> $SIG{ALRM}=$main::oldsig; };
> ($main::oldsig,$SIG{ALRM}) = ($SIG{ALRM},$handler);
> alarm($TIMEOUT);
>
> open ($fh,$path_name) or
> warn("Couldn't open $path for $description: $!"), return undef;
>
> # now try to lock it
> unless (flock ($fh,$lock_type)) {
> warn("Couldn't get lock for $description (" . ($main::msg ||
> "$!") . ")");
> alarm(0);
> close $fh;
> return undef;
> }
>
> alarm(0);
> return $fh;
>
> sub unlock {
> my $fh = shift;
> flock($fh,LOCK_UN);
> close $fh;
> }

Wow, that's some pretty advanced code for a Newbie.
I guess everything is relative.

Mike