What do the &= and &&= operators do?

X

Xevo

I can not find any information on what the "and equal" operators &=
and &&= do. I looked in the 'Programming Perl' book, searched
perldoc.com, and tried to google it, but to no avail. Can anyone fill
me in on how to use these strange operators?
 
P

Paul Lalli

Xevo said:
I can not find any information on what the "and equal" operators &=
and &&= do. I looked in the 'Programming Perl' book, searched
perldoc.com, and tried to google it, but to no avail. Can anyone fill
me in on how to use these strange operators?

From perldoc perlop:
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=

All operators of the form OP= have the same expansion form. So $x &= $y
is equivalent to $x = $x & $y, which means to take the bitwise and of
the two values $x and $y, and put the result back in $x. Similarly, $x
&&= $y is equivalent to $x = $x && $y, which means to take the logical
conjunction of the values $x and $y, and put the result back in $x.

Paul Lalli
 
B

Ben Morrow

Quoth (e-mail address removed) (Xevo):
I can not find any information on what the "and equal" operators &=
and &&= do. I looked in the 'Programming Perl' book, searched
perldoc.com, and tried to google it, but to no avail. Can anyone fill
me in on how to use these strange operators?

Exactly what they say on the tin:

$a &= $b;

is equivalent to

$a = $a & $b;

except that $a is only evaluated once. Ditto &&=. Why you would want to
do that is left as an exercise for the reader :).

(actually, &= could be useful for clearing bits in a bit-vector:

$flags |= FLAG_BIT; # set the bit
$flags &= ~FLAG_BIT; # clear it

but one doesn't do that sort of thing much in Perl.)

Ben
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top