For Programmers: Free Programming Magazines  


Home > Archive > PERL CGI Beginners > October 2006 > Warnings for variable only in nested conditional test but not in parent?









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Warnings for variable only in nested conditional test but not in parent?
evillen@gmail.com

2006-10-04, 6:55 pm

In my program I have the following two test conditions, the second "if"
is nested into the first.

if($records->[$idx_r][$idx_f]){print $q->param($choice), ":
<b>$records->[$idx_r][$idx_f]</b>", "<br>";
#if($q->param($choice) =~ /(First|Mechanical|Logic check date)/){print
$q->p()};
}

When I comment out the second "if" conditional the program runs without
warnings, however if I enable the second "if" I get hundreds of the
following in my apache error log:

[Wed Oct 04 17:32:04 2006] [error] [client 192.168.14.35] [Wed Oct 4
17:32:01 2006] development.cgi: Use of uninitialized value in join or
string at (eval 26) line 15.

I can't understand why I only get the warnings in the second test when
the same variable $q->param($choice) is referenced in both tests?
Shouldn't I also get the warnings when only using the first "if" test?

I hope this is clear

Thanks
NJH

Paul Lalli

2006-10-05, 7:55 am

evillen@gmail.com wrote:
> In my program I have the following two test conditions, the second "if"
> is nested into the first.
>
> if($records->[$idx_r][$idx_f]){print $q->param($choice), ":
> <b>$records->[$idx_r][$idx_f]</b>", "<br>";
> #if($q->param($choice) =~ /(First|Mechanical|Logic check date)/){print
> $q->p()};
> }
>
> When I comment out the second "if" conditional the program runs without
> warnings, however if I enable the second "if" I get hundreds of the
> following in my apache error log:
>
> [Wed Oct 04 17:32:04 2006] [error] [client 192.168.14.35] [Wed Oct 4
> 17:32:01 2006] development.cgi: Use of uninitialized value in join or
> string at (eval 26) line 15.
>
> I can't understand why I only get the warnings in the second test when
> the same variable $q->param($choice) is referenced in both tests?
> Shouldn't I also get the warnings when only using the first "if" test?


Your avoidance to whitespace is killing my eyes and your ability to
debug.... ;-) Rewrite your block as:

if ($records->[$idx_r][$idx_f]){
print $q->param($choice);
print ":<b>$records->[$idx_r][$idx_f]</b>";
print "<br>";
if ($q->param($choice) =~ /(First|Mechanical|Logic check date)/){
print $q->p();
}
}

Then see on what line you actually get the warnings.

Also, you're not giving us the whole story - the error message is
referring to a 26th eval that this code is apparently contained in.
Please pare your problem down to the smallest possible script that
still exhibits the error. Doing that will most likely lead you to the
solution. If it doesn't, however, post that shortest possible script,
and we can help you figure it out.

Paul Lalli

evillen@gmail.com

2006-10-05, 6:56 pm


Paul Lalli wrote:
> evillen@gmail.com wrote:
>
> Your avoidance to whitespace is killing my eyes and your ability to
> debug.... ;-) Rewrite your block as:
>
> if ($records->[$idx_r][$idx_f]){
> print $q->param($choice);
> print ":<b>$records->[$idx_r][$idx_f]</b>";
> print "<br>";
> if ($q->param($choice) =~ /(First|Mechanical|Logic check date)/){
> print $q->p();
> }
> }
>
> Then see on what line you actually get the warnings.
>
> Also, you're not giving us the whole story - the error message is
> referring to a 26th eval that this code is apparently contained in.
> Please pare your problem down to the smallest possible script that
> still exhibits the error. Doing that will most likely lead you to the
> solution. If it doesn't, however, post that shortest possible script,
> and we can help you figure it out.
>
> Paul Lalli


Thanks Paul - you make some good points.

FYI I now have the answer to the problem:

If $choice is not defined, param() returns an undefined value of
an empty list depending on context. It might also return these
results if $choice is defined, but the result of param() is not.
'print' always assumes list context. Printing an empty list does
not raise a warning.


use strict;
use warnings;

use CGI;

my $q = CGI->new();

# list context.
print '"', $q->param(undef), '"';

# list context.
print '"', $q->param('foo'), '"';

__END__


use strict;
use warnings;

use CGI;

my $q = CGI->new();

# scalar context.
print '"', scalar $q->param(undef), '"';

# scalar context.
print '"', scalar $q->param('foo'), '"';

__END__

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com