[Novalug] a perl question

rosejw@cox.net rosejw@cox.net
Fri Jan 5 14:17:58 EST 2007


Usually get those errors when not checking to see if a hash element is actually defined before trying to use it.

in otther words,

$myhash{'value"} = "na" if ( not defined $myhash{'value'} );

Getting in the habit of doing this, prevents a lot of the not initialized errors that I have gotten over the years.


---- Igor Birman <igor_birman@yahoo.com> wrote: 
> I get these errors when I run the script from the command line, but that's because there is no CONTENT_LENGTH and no FORM.  Are you getting the errors when running with a real form or just when testing with no input?
> 
> By the way, my favorite perl guide is perl in 20 pages at:
> 
> http://backpan.perl.org/authors/id/Q/QU/QUONG/perlin20.html
> 
> Igor
> 
> Megan Larko <larkoc@iges.org> wrote: Igor Birman wrote:
> 
> Thank you.   That got rid of some of the messages.   I still have the same error message for every 
> instance of $FORM{'$myvar'}.   I put the my %FORM = ""' and the my @pairs = (); (my stupidity, I 
> could not remember the % or () syntax---Thanks again Igor).  Now supposedly everything is 
> initialized but writing it out generates one error warning per instance $FOMR{'$myvar'}.
> 
> OUTPUT:
> ./news.data.test.cgi
> Odd number of elements in hash assignment at ./news.data.test.cgi line 35.
> Content-type: text/html
> 
> NASA NEWS Investigators Data Table
> Use of uninitialized value in read at ./news.data.test.cgi line 44.
> Use of uninitialized value in string eq at ./news.data.test.cgi line 56.
> Use of uninitialized value in string eq at ./news.data.test.cgi line 57.
> 
> 
> Your form submission to NEWS Data has been sent.
> Use of uninitialized value in concatenation (.) or string at ./news.data.test.cgi line 66.
> Use of uninitialized value in concatenation (.) or string at ./news.data.test.cgi line 67.
> Use of uninitialized value in concatenation (.) or string at ./news.data.test.cgi line 68.
> Use of uninitialized value in concatenation (.) or string at ./news.data.test.cgi line 74.
> Use of uninitialized value in concatenation (.) or string at ./news.data.test.cgi line 75.
> 
> 
> 
> Your dataset description update to the NASA Energy and Watercycle site has been received.
>  
> Please visit the NEWS data
> table to view the current descriptions (and holdings)
> The current time is: Fri Jan  5 12:42:10 2007 
> 
> Return to NEWS Page
> 
> 
> 
> 
> Use of uninitialized value in concatenation (.) or string at ./news.data.test.cgi line 98.
> Use of uninitialized value in concatenation (.) or string at ./news.data.test.cgi line 98.
> Use of uninitialized value in concatenation (.) or string at ./news.data.test.cgi line 98.
> Use of uninitialized value in concatenation (.) or string at ./news.data.test.cgi line 98.
> Use of uninitialized value in concatenation (.) or string at ./news.data.test.cgi line 98.
> Use of uninitialized value in concatenation (.) or string at ./news.data.test.cgi line 98.
> Use of uninitialized value in concatenation (.) or string at ./news.data.test.cgi line 98.
> Use of uninitialized value in concatenation (.) or string at ./news.data.test.cgi line 98.
> Use of uninitialized value in concatenation (.) or string at ./news.data.test.cgi line 98.
> Use of uninitialized value in concatenation (.) or string at ./news.data.test.cgi line 98.
> Use of uninitialized value in concatenation (.) or string at ./news.data.test.cgi line 98.
> Use of uninitialized value in concatenation (.) or string at ./news.data.test.cgi line 98.
> Use of uninitialized value in concatenation (.) or string at ./news.data.test.cgi line 98.
> 
> > Megan,
> > 
> > You need to initialize the FORM as a hash type and pairs as an array.  
> > Do this:
> > 
> > my %FORM = "";
> > my @pairs = ();
> > 
> > Igor
> > 
> #!/usr/bin/perl -w  -T
> use strict;
> ###  Unbuffer the output ###
>     use FileHandle;
>     STDOUT->autoflush(1);
>     my $mytime = scalar(localtime());
>     my $server = $ENV{'SERVER_NAME'};
>     my $host = $ENV{'REMOTE_HOST'};
>     my $page = $ENV{'SCRIPT_NAME'};
>     my $mailprog = '/usr/sbin/sendmail';
> #Meg    my $mailprog = '/usr/bin/mail';
> #Meg    my $recipient = 'larkoc@cola.iges.org';
>     my $recipient = 'larkoc';
> # Initialize vars
> my $buffer = "";
> my $pair   = "";
> my $dir1   = "";
> my $name   ="";
> my $value  ="";
> my $ContactEmail   ="";
> my $datatype   ="";
> my $dataname   ="";
> my $dataDesc   ="";
> my $dataX   ="";
> my $dataY   ="";
> my $dataCtr ="";
> my $dataStartLat   ="";
> my $dataStartLon   ="";
> my $dataEndLat   ="";
> my $dataEndLon   ="";
> my $dataMeet   ="";
> my $dataFormat   ="";
> my $dataURL   ="";
> my $mybrowser   ="";
> my %FORM  = "";
> my @pairs = ();
> 
> print "Content-type: text/html\n\n";
> # Flush stdout buffer
> $| = 1;
> NASA NEWS Investigators Data Tableprint "";
> print "\n";
> 
> read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
> 
> @pairs = split(/&/, $buffer);
> 
> foreach $pair (@pairs)
> {
>   ($name, $value) = split(/=/, $pair);
>   $value =~ tr/+/ /;
>   $value=~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
> 
>  $FORM{$name} = $value;
> }
>  if ($FORM{'ContactEmail'} eq "") {$FORM{'ContactEmail'} = '';}
>  if ($FORM{'name'} eq "") {$FORM{'name'} = '';}
> 
> print "
> \n";
> print "Your form submission to NEWS Data has been sent.\n";
> 
> open (MAIL, "|$mailprog $recipient ") || die "cant open $mailprog!\n";
> print MAIL "Subject: NEWS Data Test\n";
> print MAIL "This is a Web page from $FORM{'name'}\n";
> print MAIL "\n";
> print MAIL "The dataset name: $FORM{'dataname'}\n";
> print MAIL "Type: $FORM{'datatype'}.\n\n";
> print MAIL "Special Instructions: $FORM{'dataDesc'}\n";
> print MAIL "\n";
> print MAIL "---------------------------------------------------------------------------\n";
> print MAIL "Statistics from: $FORM{'name'} at $FORM{'ContactEmail'}\n";
> print MAIL "\n";
> print MAIL "The time sent was $mytime \n";
> print MAIL "The browser used to access my Web page: $ENV{'HTTP_USER_AGENT'}\n";
> print MAIL "The server used to process my Web page: $ENV{'SERVER_NAME'}\n";
> print MAIL "---------------------------------------------------------------------------\n";
> print MAIL "\n";
> close (MAIL);
> 
> print "\n";
> print "
> \n";
> print "Your dataset description update to the NASA Energy and Watercycle site has been received.
>   Please visit the NEWS data
> table to view the current descriptions (and holdings)\n";
> print "The current time is: $mytime 
> \n";
> print "Return to NEWS Page\n";
> print "
> \n";
> print "\n";
> print "\n";
> 
> # Write updated information to file "survey.response"
> if ($mybrowser =~ /MSIE/) {
>         $dir1="/var/www/response/IE";
> } else {
>         $dir1="/var/www/response";
> }  # Ends browser if block
> if (open(RESPONSE, ">$dir1/survey.response")) {
>    flock RESPONSE, 2; #lock file to assure only single-access
>    print RESPONSE "$FORM{'name'} XX $FORM{'ContactEmail'} XX $FORM{'datatype'} XX $FORM{'dataname'} XX $FORM{'dataX'} XX $FORM{'dataY'} XX $FORM{'dataCtr'} XX $FORM{'dataStartLat'} XX $FORM{'dataStartLon'} XX $FORM{'dataMeet'} XX $FORM{'dataEndLat'} XX $FORM{'dataEndLon'} XX $FORM{'dataFormat'} XX $FORM{'dataURL'} XX $FORM{'dataDesc'} \n";
> } else {     print "Can't open $dir1/response\n";
> }  #Ends fi open and releases flock
> close (RESPONSE);
> 
> # Replace old response file with new response file
> # Run 4 times to remove all spaces from system data program; replace some with underscore
> $mytime =~ s/\s+//;
> $mytime =~ s/\s+//;
> $mytime =~ s/\s+/_/;
> $mytime =~ s/\s+/_/;
> #print "mytime  $mytime \n\n";
> rename ("$dir1/survey.response","$dir1/survey.response.$mytime") || print
> "Cannot rename response\n";
> 
> ### Now chdir response and run pagegen.
> chdir $dir1;
> # NOTE: system forks then execs; exec just executes
> system  "ls  > infile.list";
> system  "./pagegen.pl";
> 
> 




More information about the Novalug mailing list