in reply to When warnings get in the way

The workaround seems fine -- it's straightforward and legible. You could probably make sure that $foo is never undef, too, so you don't have to worry about it. Something like:
my $foo = getFoo() || ''; if ($foo =~ /bar/ ){ ... }
But this has limitations (like if getFoo() returns 0).

side note #1 -- might just be your example, but treating foo as a boolean instead of string matching for "true" might work better, and avoids the warning. something like (again, depends on what getFoo() can return):
my $foo = getFoo() ? 1 : 0; if ( $foo ){ ... }

side note #2 -- I'm sure someone will mention it if it's true, but i think perl6 has some new operators that can handle things like that (but i might be mis-remembering).
Update: I was thinking of (found it here) this:
$a // $b; # short for: defined($a) ?? $a :: $b $pi //= 3; # so could do (of course perl6 only): if( $foo // '' eq "true" ){ ... } # or my $foo = getFoo(); $foo //= ''; if( $foo eq "true" ){ ... }

Replies are listed 'Best First'.
Re^2: When warnings get in the way
by ysth (Canon) on Apr 22, 2005 at 17:14 UTC