in reply to Re: Or, Or, Equals Zero, $x ||= 0
in thread Or, Or, Equals Zero, $x ||= 0

Works for strings too: $x ||= '': will avoid warnings about using an undefined value in a print, string concatenation, string comparison, regular expression, and so on.

That also turns '0' into '' and so would probably be better written as:

$x= '' if ! defined $x;

Or you can use //= if you are on the bleading edge and eschew backward compatability. Note that ||= is probably the best choice for the numeric case as treating '' as a number can also cause a warning.

- tye        

Replies are listed 'Best First'.
Re^3: Or, Or, Equals Zero, $x ||= 0 ('')
by Erez (Priest) on Nov 29, 2007 at 11:01 UTC
    If it's on a new code, and no animals are harmed, I don't see a problem with using 5.10 syntax. It's a question of what the people around you like to read. For instance, I'd write it
    $x= '' unless defined $x;
    But its a question of whatever suits.

    Software speaks in tongues of man; I debug, therefore I code.