Hi
The following can cause a warning if undefined
my $value=$PAR{$name};
What the best solution?
my $value=$PAR{$name }or $value='';
or
my $value=''; if (defined $PAR{$name}) {$value=$PAR{$name}}
or
is there a cleaner solution?
Regards
John
The only uninitialized warning would be if $name were undefined
when you try to use it, ie: $PAR{$name}. It is always legitimate
to assing undef to a scalar variable.
Don't be confused between logical's 'or' and ||. They both do the
same logical test form, but 'or' is used for control flow.
So, my $value2 = 0 or 1; is functionally equivalent to
(my $value2 = 0) or 1; which assigns 0 to $value2 because
^^^^^^^^^^^^^^^
this expression is evaluated and the result of the assignment is always
*TRUE*, and because of that is always meaningless, a non-conditional.
You have to be carefull with || as well. Perl interprets 'undef' logically
equal to 0. So $value = undef || 1; and $value = 0 || 1; both assign
1 to $value.
Some examples follow below ..
-sln
---------
# 'or' can be used like '||' when used for control flow.
use strict;
use warnings;
my $value;
$value = undef || 1;
print $value,"\n";
$value = 0 || 1;
print $value,"\n\n";
# The ||, // and && operators return the last value evaluated
# (unlike C's || and &&, which return 0 or 1).
# --------------------
# For Perl 5.10 :
# $a // $b is exactly equivalent to defined($a) ? $a : $b
$value = defined(0) ? 0 : 3;
print $value,"\n";
$value = defined(undef) ? 0 : 3;
print $value,"\n";
$value = 0 // 3;
print $value,"\n";
$value = undef // 3;
print $value,"\n";
$value = undef;
my $value2 = 0 or 1;
print $value2,"\n";
__END__
Found = in conditional, should be == at gg.pl line 34.
1
1
0
3
0
3
0