http://www.perlmonks.org?node_id=653450


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).