Perl Operators   «Prev  Next»
Lesson 3Perl Operator Precedence
ObjectiveOrder in which Perl expressions are evaluated.

Perl Operator Precedence

In expressions where more than one operator is in use, some parts of the expression will be evaluated before other parts.
The rules that dictate this are based on a concept called operator precedence. The operators which have higher precedence are evaluated before the operators with lower precedence.
For example, consider this expression:
$x = 5 * 3 + 4;

Perl multiplication Operator

Take a look at the following code:
$x = 5 * 3 + 4;

Since the precedence of the multiplication operator (*) is higher than that of the addition operator ( + ), the multiplication is evaluated first, so the result is (5 * 3) + 4, or 19.
If this is not what you meant, you can always use parenthesis, like this:
$x = 5 * (3 + 4);


Numeric Operators

Perl provides the typical ordinary addition, subtraction, multiplication, and division operators, and so on. For example:
2 + 3 # 2 plus 3, or 5
5.1 - 2.4 # 5.1 minus 2.4, or 2.7
3 * 12 # 3 times 12 = 36
14 / 2 # 14 divided by 2, or 7
#

10.2 / 0.3 # 10.2 divided by 0.3, or 34
10 / 3 # always floating-point divide, so 3.3333333...
Perl also supports a modulus operator (%). The value of the expression 10 % 3 is the remainder when 10 is divided by 3, which is 1. Both values are first reduced to their integer values, so 10.5 % 3.2 is computed as 10 % 3.* Additionally, Perl provides the FORTRAN-like exponentiation operator, which many have yearned for in Pascal and C. The operator is represented by the double asterisk, such as 2**3, which is two to the third power, or eight. In addition, there are other numeric operators.
Will the value of$x be 19 (15 + 4) or
35 (5 * 7)?
Use this list of operators, in order of precedence, for reference throughout this module. The rest of this module is not in the order of precedence. The order was chosen to put related operators together, and to give you the operators first that will help you understand the later ones.



Perl Operators in Order of Precedence

The higher precedence operators are toward the top, and the lower precedence operators are toward the bottom. Refer to this list when you need to know the relative precedence of different operators.
->
++ --
**
! ~ \ and unary + and -
=~ !~
* / % x
+ - .
<< >>
< > <=>= lt gt le ge
==!=<=> eq ne cmp
&
| ^
&&
||
..
?:
=+=-=*= etc.
,=>
not
and
or xor

Perl Complete Reference
Other operators that Perl provides, as well as operator associativity and precedence. The operators are
  1. The arithmetic operators **, %, and - (unary negation)
  2. The other integer- and string-comparison operators
  3. The logical operators
  4. The bit-manipulation operators
  5. The assignment operators
  6. Autoincrement and autodecrement
  7. Concatenating and repeating strings
  8. The comma and conditional operators

Using the Arithmetic Operators

The arithmetic operators that you have seen so far-the +, -, *, and / operators-work the way you expect them to: They perform the operations of addition, subtraction, multiplication, and division. Perl also supports three other arithmetic operations:
  1. Exponentiation
  2. The modulo or remainder operation
  3. Unary negation
Although these operators aren't as intuitively obvious as the ones you've already seen, they are quite easy to use.
  • Exponentiation
    The exponentiation operator, **, provides a convenient way to multiply a number by itself repeatedly. For example, here is a simple Perl statement that uses the exponentiation operator:
    $x = 2 ** 4;
    

    The expression 2 ** 4 means "take four copies of two and multiply them." This statement assigns 16 to the scalar variable $x. Note that the following statements are equivalent, but the first statement is much more concise:
    $x = 2 ** 7;
    $x = 2 * 2 * 2 * 2 * 2 * 2 * 2;
    

    When an exponentiation operator is employed, the base value (the value to the left of the **) is the number to be repeatedly multiplied. The number to the right, called the exponent, is the number of times the multiplication is to be performed. Here are some other simple examples of the exponentiation operator:
    $x = 9 ** 2; # 9 squared, or 81
    $x = 2 ** 3; # 2 * 2 * 2, or 8
    $x = 43 ** 1; # this is just 43
    

    The ** operator also works on the values stored in variables:
    $x = $y ** 2;
    

    Here, the value stored in $y is multiplied by itself, and the result is stored in $x. $y is not changed by this operation.
    $x = 2 ** $y;
    
    In this case, the value stored in $y becomes the exponent, and $x is assigned 2 multiplied by itself $y times. You can use the exponent operator with non-integer or negative exponents:
    2 ** -5 # this is the fraction 1/32
    5 ** 2.5 # this is 25 * the square root of 5
    
In the next lesson you will learn about the basic math operators.

SEMrush Software