Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Or, Or, Equals Zero, $x ||= 0

by Anonymous Monk
on Nov 27, 2007 at 22:56 UTC ( #653404=perlquestion: print w/ replies, xml ) Need Help??
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?

Comment on Or, Or, Equals Zero, $x ||= 0
Select or Download Code
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.


    ___________
    Eric Hodges
Re: Or, Or, Equals Zero, $x ||= 0
by FunkyMonk (Canon) 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.

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

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

      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        

        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.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://653404]
Approved by kvale
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (3)
As of 2014-12-27 09:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (176 votes), past polls