in reply to Or, Or, Equals Zero, $x ||= 0
The reason for using that expression is probably that the original script author was (wisely) including a "-w" flag on the shebang line, or put "use warnings;" near the top of the script. In those cases, the $x ||= 0; will prevent perl from issuing a warning about "use of undefined value in numeric comparison" or "use of undefined value in print".
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.
It's not that undefined values are inherently bad -- sometimes you want a variable to be undef, and if you really want to use undef values in a print or a comparison to some actual value, you can modify the "use warnings" pragma to say which warnings it should ignore. But such "special needs" cases are pretty rare (and probably avoidable).
|Replies are listed 'Best First'.|
Re^2: Or, Or, Equals Zero, $x ||= 0 ('')
by tye (Sage) on Nov 28, 2007 at 05:56 UTC
by Erez (Priest) on Nov 29, 2007 at 11:01 UTC
In Section Seekers of Perl Wisdom