3.3. Autoincrement and AutodecrementThe ++ and -- operators work as in C. That is, when placed before a variable, they increment or decrement the variable before returning the value, and when placed after, they increment or decrement the variable after returning the value. For example, $a++ increments the value of scalar variable $a, returning the value before it performs the increment. Similarly, --$b{(/(\w+)/)[0]} decrements the element of the hash %b indexed by the first "word" in the default search variable ($_) and returns the value after the decrement.[2]
The autoincrement operator has a little extra built-in magic. If you increment a variable that is numeric, or that has ever been used in a numeric context, you get a normal increment. If, however, the variable has only been used in string contexts since it was set, has a value that is not the null string, and matches the pattern /^[a-zA-Z]*[0-9]*$/, the increment is done as a string, preserving each character within its range, with carry: As of this writing, magical autoincrement has not been extended to Unicode letters and digits, but it might be in the future.print ++($foo = '99'); # prints '100' print ++($foo = 'a0'); # prints 'b1' print ++($foo = 'Az'); # prints 'Ba' print ++($foo = 'zz'); # prints 'aaa' The autodecrement operator, however, is not magical, and we have no plans to make it so. Copyright © 2002 O'Reilly & Associates. All rights reserved. |
|