Operators

Assignment operators

Full

Plain = and the OP= family that combine an operation with assignment.

= assigns. For almost every infix operator there is a matching OP= form that applies the operator to the current value and stores the result — $x += 3 is $x = $x + 3.

Arithmetic assignment #

my $x = 5;
$x += 3;
$x **= 2;
say $x;
Output
64

5 + 3 is 8, then 8 ** 2 is 64.

String assignment #

~= appends (the assignment form of the ~ concatenation operator).

my $s = "a";
$s ~= "b";
say $s;
Output
ab

Logical assignment #

//=, ||=, and &&= assign only when the current value is undefined / false / true — the "set a default" idiom.

my $y;
$y //= 10;
say $y;
my $z = 0;
$z ||= 5;
say $z;
Output
10
5

Notes #