Last Updated:

Assignment operations | Perl

Assigning a value to a variable to a literal value, or assigning one variable to the value of another variable, is the most commonly performed action in a program written in any programming language. In some languages, this action is determined using an operator, and in others, by an operation. The difference is that in languages where assignment is an operation, it can be used in expressions as an integral part of it, since any operation calculates a certain value, whereas the operator only produces an action. In Perl, assignment is an operation that returns the correct lvalue. What it is, we will explain literally in the following paragraphs.

The assignment operation =, of which the reader is already somewhat familiar, is a binary operation whose right operand can be any correct expression, whereas the left operand must define the memory area where the assignment operation places the calculated value of the right operand. In this case, they say that the left operand must be the correct lvalue (from the English left value - left value). And what can we use in the program to mark the memory area? That's right, variables. Therefore, you can use a variable of any type or an element of any array as the left operand of the assignment operation. (There are other objects in Perl that can be used as the left operand of an assignment operation, but more on that in due course.) The following simple assignment operation is:

$а = $b+3;

... calculates the value of the right operand and assigns it to the variable $a, i.e. stores it in the memory area allocated for the variable $a. The return value of this operation will be the address of the memory area of the variable $a (the correct lvalue), or more simply, the name of the scalar variable $a, which can again be used as the left operand of the assignment operation. Thus, in Perl, the following assignment operation is performed:

($а = $b) = 3;

... is syntactically correct and as a result of its calculation, the variable $a will be assigned a value of 3, so the result of calculating the assignment operation $a = $b will be assigning the variable $a to the value of the variable $b, and the return value can be considered the variable $a, which in the next operation is assigned the value of 3. The reader will ask, "Why bother with such complexity when the same result can be obtained by a simple $a = 3 assignment operation?" Indeed, the observation is fair. But in this example, we showed how you can use an assignment operation as the correct lvalue. We will show more interesting examples when we define composite assignment operations borrowed from the C language.

Perl syntax rules allow you to assign the same value to multiple variables in a single expression:

$varl = $var2 = $varl[0] =34;

Very often, when implementing computational algorithms, it is necessary to carry out a variety of calculations using the value of some variable and assign the result to the same variable. For example, increment the value of the variable $a by z and assign the result to the same variable $a. This action can be implemented by the following assignment operation:

$а = $а+3;

However, the Perl language offers a more efficient way to solve such problems by providing the programmer with a binary compound assignment operation +=, which adds to the value of the left operand representing the correct lvalue the value of the right operand and assigns the result to the variable represented by the left operand. Thus, the composite assignment operator:

$а += 3; I Result: $а = $а+3

... is equivalent to the previous simple assignment operator. The only difference is that its implementation is more efficient than the implementation of simple assignment, since in a composite assignment operator the variable $a is calculated once, whereas in a simple one it has to be calculated twice. (Calculating a variable is the calculation of the address of the memory area it represents and retrieving the value stored in that memory area.)

For all binary operations of the Perl language, there are corresponding composite assignment operations. All of them, along with examples of their use, are collected in Table. 4.2.

Table 4.2. Compound assignment operations.

OperationExampleSimple assignment equivalent
**=$a **= 3;$a = $a ** 3;
+=$a += 3;$a = $a + 3;
- =$a -= 3;$a = $a -3;
.=$a.= "a";$a = $a. "a";
*=$a *= 3;$a = $a * 3;
/=$a /= 3;$a = $a / 3;
%=$a %= 3;$a = $a % 3;
x=$a x = 3;$a = $a x 3;
&=$a &= $b;$a = $a & $b;
|=$a |= 3;$a = $a | 3;
^=$a ^= 3;$a = $a ^ 3;
<<=$a <<= 3;$a = $a " 3;
>> =$a >>= 3;$a = $a " 3;
|| =$a || = $b == 0;$a = $a || $b == 0;

The return value of each of the composite assignment operations, as in the case of simple assignment, is the left Operand variable (the correct lvalue), so they can be used in any operand of other assignment operations (Example 4.11).

$b = 1; 
$a = ($B +=3);# Result: $a = $b = 4
$a += ($B += 3);# Result: $a = $a+$b+3
(($a += 2) **= 2) -= 1;# Result: $a = ($a+2)**2-1

If you use an assignment operation (simple or composite) as the left operand of another assignment operation, you must enclose the assignment operation in parentheses. Otherwise, a syntax error may be generated or the expression will not be interpreted as intended. If there are multiple assignment operations in a single expression without parentheses, the perl interpreter begins parsing it on the right . For example, if the last expression of example 4.11 is written without parentheses

$а += 2 **= 2 -= 1;

... then when it is parsed, the interpreter will first highlight the assignment operation:

2 -= 1;

... and will report an error because its syntax is incorrect (the left operand is not a variable or an array element).