Perl Operators   «Prev 

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