next up previous contents
Next: Comparison Operators Up: Scalar Data Previous: Quoting Operators   Contents


Operators for Numeric Scalar Values

Perl supports all the numeric operators you would expect: + for addition,- for subtraction, * for multiplication and / for division. In addition, perl also provides ** for exponentiation, and % for modulus (remainder). These numeric operators are all known as binary operators, because they require exactly two operands, one on each side of the operator's symbols. As always, variables and constant values can be freely combined:
     print 3 + 5;           # prints 8
     $x = 3;
     print $x + 5;          # prints 8
All of perl's operations on numeric values are performed using double precision arithmetic, even if the results are displayed as integers. When a string value is used in an expression involving numeric operators, perl will attempt to convert it to a number.
     $value = "21";
     $answer = $value / 3;
In the above example, $answer will have the value 7; perl will not print any errors or warnings regarding the conversion. When perl converts a string to a number, it stops reading the string once it can't interpret it as a number any longer; if it can't interpret the string as a number at all, it silently converts it to a zero. So the string value ``4x'' would be treated as the number 4 in a numeric context, but the string ``x4'' would be treated as a zero. If you want to be made aware of perl's failure to successfully convert a string to a number, make sure you use the -w flag when you invoke perl. (See Section 1.5.1 for more details.)

In addition to the basic operators described above, perl provides a number of operators, also available in C, for simplifying some very commonly performed tasks. None of these operators provide any additional functionality, but they still can be very useful. The first class of such operators are known as assignment operators. These operators consist of one of the binary operators mentioned earlier in this section, followed by an equal sign (=). These operators are used in place of the usual assignment operator, and first perform the selected binary operation, and then re-assign the resulting value to the variable on the left-hand side of the assignment operator. The following examples show the equivalence of assignment operators with the usual assignment and binary operators.

  $x += 5;           # equivalent to $x = $x + 5
  $value /= 2;       # equivalent to $value = $value / 2
There are two reasons to use the assignment operators over the combination of a regular assignment and the binary operation. First, using the assignment operators eliminates the possibility of a typographical error, which would erroneously store the result of the operation in some other variable. Since perl will not complain if a variable has not been set to a value (it will simply treat it as a zero), such errors can be difficult to track. In addition, when reading your program, the assignment operators make it clear that a variable is being modified, and what the nature of the modification is. Without the assignment operator, more code needs to be read to understand what the program is doing.

Perl also offers increment and decrement operators, which basically are an abbreviation of adding one to a variable, and storing that value back into the original variable. Incrementing is represented by two plus signs (++), and decrementing is represented by two minus signs (--). There are two flavors of these operators. If the plus or minus signs appear before the variable being operated on, the value is incremented before it is used, and if they appear after the variable, the value is used and then incremented. In the simplest case, when the variable and operator appear by themselves, it doesn't make any difference. For example

     $x++;
and
     ++$x;
both have exactly the same effect; they add one to the value of the variable $x, and store the result back into $x. The differences show when you combine an incremented (or decremented) variable with other operators and variables.
 $x = 3;
 $y = 3;
 print  7 + $x++;         # prints 10 and $x now equals 4
 print  7 + ++$x;         # prints 11 and $x now equals 4
in the first case, the value of $x is added to 7, and then $x is incremented; in the second case, $x is incremented, and then added to 7.

Finally, there is the range operator, which is represented by two periods (..). The range operator allows you to specify two numeric values, and perl will produce an array containing all the integers between and including the two values. The value to the left of the double periods must be less than the value to the right. The range operator differs from the other scalar operators in that it generates an array instead of another scalar. It's very useful when extracting slices from arrays. (See Section 4.5).


next up previous contents
Next: Comparison Operators Up: Scalar Data Previous: Quoting Operators   Contents
Phil Spector 2002-10-18