Assignment Operators

Yesterday, we discussed the basic assignment operator, =, which assigns a value to a variable. One common use of assignment is an operation to change the value of a variable based on the current value of that variable, such as:

$inc = $inc + 100;

This does exactly what you'd expect; it gets the value of $inc, adds 100 to it, and then stores the result back into $inc. This sort of operation is so common that there is a shorthand assignment operator to do just that. The variable reference goes on the left side, and the amount to change it on the right, like this:

$inc += 100;

Perl supports shorthand assignments for each of the arithmetic operators, for string operators I haven't described yet, and even for && and ||.

Get Sams Teach Yourself Perl in 21 Days, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.