Bryan R Harris wrote:
> 
>>>Can someone explain what:
>>>
>>>$pi ||= 3;
>>>
>>>...means?  I just saw it in Programming Perl (pp 540), but it doesn't
>>>explain it.  Thx!
>>|| is the logical OR operator (see perldoc perlop) which says that if $pi is
>>TRUE then keep the current value of $pi but if $pi is FALSE then assign 3 to
>>$pi.
>>
>>That could also be written as:
>>
>>unless ( $pi ) {
>>    $pi = 3;
>>    }
> 
> 
> Aah, I see now.  Just like the following pairs of commands do equivalent
> things:
> 
> $pi += 3
> $pi = $pi + 3
> 
> $pi ||= 3
> $pi = $pi || 3
> 
> Is there an "&&=" also?

perldoc perlop
[snip]
       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.

       Unlike in C, the scalar assignment operator produces a valid lvalue.
       Modifying an assignment is equivalent to doing the assignment and then
       modifying the variable that was assigned to.  This is useful for
       modifying a copy of something, like this:

           ($tmp = $global) =~ tr [A-Z] [a-z];

       Likewise,

           ($a += 2) *= 3;

       is equivalent to

           $a += 2;
           $a *= 3;

       Similarly, a list assignment in list context produces the list of
       lvalues assigned to, and a list assignment in scalar context returns
       the number of elements produced by the expression on the right hand
       side of the assignment.


> How about "or="?

As you can see from the documentation above, no.

> (I can't think of why I'd need it, but I'm just curious if perl is
> converting "<left> <anything>= <right>" to "<left> = <left> <anything>
> <right>".)

No.  If anything it would convert "$x = $x OP $y" to "$x OP= $y" because the
OP= operators are usually more efficient, and I know that perl converts $x++
to ++$x, if there are no side effects, because ++$x is more efficient.

If you really need to understand why they are more efficient you need an
understanding of assembly language and the C programming language.  :-)



John
-- 
use Perl;
program
fulfillment

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to