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

#### Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

•  Subject
• Author
• Posted on
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?

## Re: What do the &= and &&= operators do?

> 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

## Re: What do the &= and &&= operators do?

Quoth x3v0-usenet@yahoo.com (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

--
I must not fear. Fear is the mind-killer. I will face my fear and
I will let it pass through me. When the fear is gone there will be
nothing. Only I will remain.
ben@morrow.me.uk                                          Frank Herbert, 'Dune'