- publishing free software manuals
Perl Language Reference Manual
by Larry Wall and others
Paperback (6"x9"), 724 pages
ISBN 9781906966027
RRP £29.95 ($39.95)

Sales of this book support The Perl Foundation! Get a printed copy>>>

7.21 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:

**=    +=    *=    &=    <<=    &&=
       -=    /=    |=    >>=    ||=
       .=    %=    ^=           //=

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];


($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.

ISBN 9781906966027Perl Language Reference ManualSee the print edition