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

Re: ||= operator

Thread Previous | Thread Next
Chas. Owens
August 10, 2009 10:52
Re: ||= operator
Message ID:
On Mon, Aug 10, 2009 at 11:22, Bryan R
Harris<> wrote:
> 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.

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

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

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

Thread Previous | Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About