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

Re: ||= operator

Thread Previous | Thread Next
From:
Noah Garrett Wallach
Date:
August 15, 2009 01:25
Subject:
Re: ||= operator
Message ID:
4A85DF5B.8010207@enabled.com
Chas. Owens wrote:
> On Mon, Aug 10, 2009 at 11:22, Bryan R
> Harris<Bryan_R_Harris@raytheon.com> wrote:
> snip
>> I think it's saying that just like:
>>
>>  $a += 2;
>>
>> is the same as:
>>
>>  $a = $a + 2;
>>
>> similarly:
>>
>>  $a ||= 3;
>>
>> is the same as:
>>
>>  $a = $a || 3;
>>
>> ... implying if $a is false, $a gets set to 3, otherwise it stays at
>> whatever it was.
> snip
> 
> That is exactly what it is saying.  The problem Shawn is pointing out
> is that it requires you to
> 
> 1. read about what += does
> 2. infer the behavior of ||= from that
> 3. then look up what || does
> 
> All of that is fine if you are reading perlop from start to finish,
> but if you are trying to use it as a reference guide (by searching for
> what you want to know about) it can lead to confusion and a belief
> that it is not documented.  Perhaps we need a perlopref or perlopquick
> like this:
> 
> =head2 C<||>
> 
> 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.
> 
> =head2 C<<< X >>= Y >>>
> 
> This is equivalent to C<<<X = X >> Y>>> see,  C<<< >> >>> and C<=> for
> more information.
> 
> =head2 C<X ||= Y>
> 
> This is equivalent to C<X = X || Y>, see C<||> and C<=> for more information.
> 
> =head2 C<X .= Y>
> 
> This is equivalent to C<X = X . Y>, see C<.> and C<=> for more information.
> 
> 
> Sounds like yet another side project for me.
> 

Thanks Chas :)


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