Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
A coworker passed on the following bit of code, which, as far as I can tell, does nothing:
$x ||= 0;
$x = -1; $x ||= 0; print $x;
-1
$x = 0; $x ||= 0; print $x;
0
$x = 2; $x ||= 0; print $x;
2
$x = 3; $x ||= 0; print $x;
3
$x = 123132; $x ||= 0; print $x;
123132
Any ideas on how or why something like Or, Or, Equals Zero would be used?
Re: Or, Or, Equals Zero, $x ||= 0
by eric256 (Parson) on Nov 27, 2007 at 23:08 UTC
|
$x = undef; $x ||= 0; print $x;
That is the same as $x = $x || 0 (which is read $x or 0) and is used for defaulting values.
| [reply] [d/l] [select] |
Re: Or, Or, Equals Zero, $x ||= 0
by graff (Chancellor) on Nov 28, 2007 at 02:09 UTC
|
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). | [reply] [d/l] [select] |
|
$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.
| [reply] [d/l] [select] |
|
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.
| [reply] [d/l] [select] |
Re: Or, Or, Equals Zero, $x ||= 0
by FunkyMonk (Chancellor) on Nov 27, 2007 at 23:24 UTC
|
$x ||= 42 is read as "if $x is false (empty string, 0 or undef) store 42 into $x". This construct has always had problems when used to assign a default value (what if you want $x to be zero) that will soon be solved by the "defined-or" operator, //, arriving soon in perl 5.10.0.
| [reply] [d/l] [select] |
Re: Or, Or, Equals Zero, $x ||= 0
by markkawika (Monk) on Nov 28, 2007 at 00:33 UTC
|
$x ||= 0; translates to $x = $x || 0;. You might use this to assign a default value to a variable when you didn't want to test whether or not the variable is defined. $x stays the same if $x is defined and is not equal to zero. If it is equal to zero, it falls through the "or" (||) and sets it to zero.
So the only time this changes the value of $x is when $x is undefined. That's a short-hand way of writing:
$x = 0 if (!defined $x);
| [reply] [d/l] [select] |
|
|