- Assignment Operators
- Increment and Decrement Operators
- String Concatenation and Repetition
- Operator Precedence and Associativity
- Using Patterns to Match Digits
- An Example: Simple Statistics
- Input and Output
- Another Example: Stocks
- A Note About Using Functions
- Going Deeper
- Summary
- Q&A
- Workshop
- Answers
Increment and Decrement Operators
The ++ and -- operators are used with a variable to increment or decrement that variable by 1 (that is, to add or subtract 1). And as with C, both operators can be used either in prefix fashion (before the variable, ++$x) or in postfix (after the variable, $x++). Depending on the usage, the variable will be incremented or decremented before or after it's used.
If your reaction to the previous paragraph is "Huh?", here's a wordier explanation. The ++ and -- operators are used with scalar variables to increment or decrement the value of that variable by 1, sort of an even shorter shorthand to the += or -= operators. In addition, both operators can be used before the variable referencecalled prefix notation, like this:
++$x;
Or, in postfix notation (after the variable), like this:
$x++;
The difference is subtle and determines when, in the process of Perl's evaluation of an expression, that the variable actually gets incremented. If you used these operators as I did in those previous two examplesalone, by themselvesthen there is no difference. The variable gets incremented and Perl moves on. But, if you use these operators on the right side of another variable assignment, then whether you use prefix or postfix notation can be significant. For example, let's look at this snippet of Perl code:
$a = 1; $b = 1; $a = ++$b;
At the end of these statements, both $a and $b will be 2. Why? The prefix notation means that $b will be incremented before its value is assigned to $a. So, the order of evaluation in this expression is that $b is incremented to 2 first, and then that value is assigned to $a.
Now let's look at postfix:
$a = 1; $b = 1; $a = $b++;
In this case, $b still ends up getting incremented; its value at the end of these three statements is 2. But $a's value stays at 1. In postfix notation, the value of $b is used before it's incremented. $b evaluates to 1, that value is assigned to $a, and then $b is incremented to 2.
NOTE
To be totally, rigorously correct, my ordering of how things happen here is off. For a variable assignment, everything on the right side of the = operator always gets evaluated before the assignment occurs, so in reality, $a doesn't get changed until the very last step. What actually happens is that the original value of $b is remembered by Perl, so that when Perl gets around to assigning a value to $a, it can use that actual value. But unless you're working with really complex expressions, you might as well think of it as happening before the increment.
CAUTION
Even though using assignment operators and increment operators in the same statement can be convenient, you should probably avoid it because it can cause confusion.