in reply to complaint: always testing defined()

This doesn't work in your example, where you are specifically testing for a false value, but in other cases, you can do:
if ( ($params->{root} || '') eq 'foo' )
The other thing to consider is why you have so many possible undefs; could you be initializing things, providing default values, using NOT NULL columns, etc. to avoid the possibility of undefs in some cases?

Replies are listed 'Best First'.
Re^2: complaint: always testing defined()
by moritz (Cardinal) on Nov 02, 2007 at 11:01 UTC
    But please note that you can't use this to compare to '0', or any other value that evaluates to false in a boolean context, so you should only use it to compare it with constants.

    perl -wle 'if( ("0" || "") eq "0"){print "yes"} else { print "no" }' no

      It seems // (binary defined-or operator) isn't popular enough for some reason (available at least in 5.8.8) ...

      my $p; $p //= 2; my $q = 0; $q //= 2; print $p , $q; # 20

      A few minutes later... Well, above isn't available by default in 5.8.8 but via separate patch. It seems the patch isn't popular enough for some reason.

      :)

Re^2: complaint: always testing defined()
by RaptorRex (Acolyte) on Nov 02, 2007 at 04:50 UTC
    Good point. It's form input. I should just provide defaults. I knew there was something obvious I was missing this morning :)

    Thanks.