apomatix has asked for the wisdom of the Perl Monks concerning the following question:

Is there any difference between
$v ||= $w; $x &&= $y;
and
$v |= $w; $x &= $y;
? If not, is there some other situation in which the double and single operators differ in their action?

Replies are listed 'Best First'.
Re: logical-assignment operators
by ikegami (Patriarch) on Mar 27, 2009 at 18:51 UTC

    The inputs for which they are different are far more numerous than the inputs for which they are similar. perlop

Re: logical-assignment operators
by eff_i_g (Curate) on Mar 27, 2009 at 18:52 UTC
    &&= and ||= are logical; &= and |= are bitwise. See perlop.
Re: logical-assignment operators
by kyle (Abbot) on Mar 27, 2009 at 18:57 UTC

    As others have said, the first pair are conditions and the second pair are bitwise operations, and you can learn all about them in perlop. For a little more illustration, I'll show Perl that does the same thing without the operators you're asking about.

    Conditions:

    if ( ! $v ) { $v = $w } # $v ||= $w; if ( $x ) { $x = $y } # $x &&= $y;

    Bitwise operations:

    $v = $v | $w; # $v |= $w $x = $x & $y; # $x &= $y
Re: logical-assignment operators
by zwon (Abbot) on Mar 27, 2009 at 18:55 UTC

    Yes there is some difference.

    use strict; use warnings; my $v = 1; my $w = 2; my $x = 1; my $y = 2; $v ||= $w; $x &&= $y; print "$v, $x\n"; $v = 1; $w = 2; $x = 1; $y = 2; $v |= $w; $x &= $y; print "$v, $x\n"; __END__ 1, 2 3, 0
Re: logical-assignment operators
by lostjimmy (Chaplain) on Mar 27, 2009 at 19:03 UTC

    When you have an operator paired with an =, that can usually be translated to an easier to understand version. Your examples could be written as:

    $v ||= $w --> $v = $v || $w $x &&= &y --> $x = $x && $y $v |= $w --> $v = $v | $w $x &= $y --> $x = $x & $y

    The first set of examples are logical and, and logical or. The or example checks if $v is true, if it is, $v is set to itself, OR it is set to $w. The and example is a little more confusing and I'm not really sure why you would want to use it. If $x is 0, it remains 0, otherwise it is set to $y.

    The second set of examples is bitwise and, and bitwise or.

    You can look up more information about these operators in perlop

      Here is yet another way to use ||=.
      $cache{$a} ||= expensive_sub($a);

      Here we have an expensive operation on $a, and we want to save the result so that if $a is seen again, we already have the answer from expensive_sub().

      Having recently run into a really good use for &&= this comment caught my eye. How about:

      $value &&= 'prefix-' . uc $value;

      translated as "$value contains a string, uppercase it and prepend the prefix."

      In my particular circumstance, '0' was not going to happen but either undef or the empty string were possible. Those cases were filtered out in later processing.

      G. Wade
      Here is one example of where ||= is useful... If this regex doesn't match then $license_class is undefined. Instead of some kind of "if" code that does something "if (!defined ...)", this just sets the default case "BAD_CLASS" if that happens.
      my $license_class = ($raw_html =~ m|Class:</td><td class="di">(\w+?) |)[0]; $license_class ||= "BAD_CLASS";