develooper Front page | perl.beginners | Postings from August 2009

Re: ||= operator

Thread Previous | Thread Next
From:
Chas. Owens
Date:
August 15, 2009 10:58
Subject:
Re: ||= operator
Message ID:
58ce48dc0908151058m52b1b287i75e13eaa051b1c3d@mail.gmail.com
On Fri, Aug 14, 2009 at 18:01, Noah Garrett
Wallach<noah-list@enabled.com> wrote:
> Telemachus wrote:
>>
>> On Fri Aug 07 2009 @  2:55, Admin wrote:
>>>
>>> Hi there,
>>>
>>> is there a page that explains the ||= operator and similar operators?
>>> google is not quite finding the special characters in the first 10 hits.
>>
>> Google and punctuation don't mix well, apparently.
>>
>> In any case, try perldoc perlop in a terminal or online:
>> http://perldoc.perl.org/perlop.html
>>
>
> thanks
>
> I searched the docs for ||=
> here is the result
>
> Search results for query "||="
snip

All the information you need is in [perlop][1], it just requires you
to infer the behavior of ||= from the information given.  I am working
on a [reference version of perlop][2] (called perlopref) that will
hopefully make this easier.  The relevant pieces of perlop are

      Assignment Operators

      "=" is the ordinary assignment operator.

      Assignment operators work as in C.  That is,

          $a += 2;

      is equivalent to

          $a = $a + 2;

      although without duplicating any side effects that dereferencing the
      lvalue might trigger, such as from tie().  Other assignment operators
      work similarly.  The following are recognized:

          **=    +=    *=    &=    <<=    &&=
                 −=    /=    |=    >>=    ||=
                 .=    %=    ^=           //=
                       x=

      Although these are grouped by family, they all have the precedence of
      assignment.


This tells you that ||= is like +=, that is $a ||= 2 is the same as $a
= $a || 2.  Logical or is well documented in its section of perlop:

   C−style Logical Or
       Binary "||" performs a short‐circuit logical OR operation.  That is, if
       the left operand is true, the right operand is not even evaluated.
       Scalar or list context propagates down to the right operand if it is
       evaluated.

[1] : http://perldoc.perl.org/perlop.html
[2] : http://github.com/cowens/perlopref

-- 
Chas. Owens
wonkden.net
The most important skill a programmer can have is the ability to read.

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About