On Fri, 2002-06-14 at 15:50, todd r wade wrote:
>
>
> Chas Owens wrote:
>
>
> ><snip>
> >> Alternately, you could say:
> >>
> >> $track ||= '';
> >>
> >> right before the next time you use foo after the split. I dont know
> why this
> >> works offhand, I just remember reading in the docs somewhere that
> its exempt
> >> from warnings. But for me, that is crossing over into the land of
> ugly =0).
> ></snip>
> >
> >$track ||= '';
> >
> >is equivalent to
> >
> >$track = $track || '';
> >
> >which is equivalent to
> >
> >if ($track) {
> > $track = $track;
> >} else {
> > $track = '';
> >}
> >
>
> When I said, "I dont know why this works...", I meant I dont remember
> why ||= is exempt from warnings. I know what it does.
>
> Your examples are neither equivalent nor dangerous in the context of
> this thread. We are trying to avoid "undefined value..." warnings by
> the use of a variable after a split.
>
> Im sure your second example is not exempt from the warning ,where
> $track ||= ''; is exempt.
>
> also, The original OPs conditional following the split uses the eq
> operator with $track as one of its arguments, so the zero issue does
> not apply.
>
> This was my second example, and my first suggestion to use:
>
> if ( defined($track) and ( $track eq "foo") ) { ....
>
> would be the most prudent IMHO.
>
> Todd W
The reason it works is because of the third case. The value undef is
equivalent to false in logical situations. The proof is below. I agree
that
if ( defined($track) and ( $track eq "foo") ) {
.
.
.
}
is the safest method since it correctly handles the case where $track ==
0 (which would be the first track in some numbering systems). As I
said, Perl 6 will solve this problem with a new operator.
<output>
Testing for warnings
shortest way
Use of uninitialized value in print at ./t.pl line 12.
test
short way
Use of uninitialized value in print at ./t.pl line 22.
test
Longest way
Use of uninitialized value in print at ./t.pl line 32.
test
Testing for overwrite of 0
shortest way
test
short way
test
Longest way
test
</output>
<example>
#!/usr/bin/perl
use warnings;
use strict;
print "Testing for warnings\n\n";
print "shortest way\n";
my $var1;
print $var1;
$var1 ||= "test\n\n";
print $var1;
print "short way\n";
my $var2;
print $var2;
$var2 = $var2 || "test\n\n";
print $var2;
print "Longest way\n";
my $var3;
print $var3;
if ($var3) {
$var3 = $var3;
} else {
$var3 = "test\n\n";
}
print $var3;
print "Testing for overwrite of 0\n\n";
print "shortest way\n";
my $var4 = 0;
$var4 ||= "test\n\n";
print $var4;
print "short way\n";
my $var5 = 0;
$var5 = $var5 || "test\n\n";
print $var5;
print "Longest way\n";
my $var6 = 0;
if ($var6) {
$var6 = $var6;
} else {
$var6 = "test\n\n";
}
print $var6;
</example>
--
Today is Setting Orange the 19th day of Confusion in the YOLD 3168
Frink!
Missile Address: 33:48:3.521N 84:23:34.786W
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]