PERL Built-in Operators

There are many Perl operators but here are a few of the most common ones:

Arithmetic Operators

    +   addition
- subtraction
* multiplication
/ division

Numeric Comparison Operators

    ==  equality
!= inequality
< less than
> greater than
<= less than or equal
>= greater than or equal

String Comparison Operators

    eq  equality
ne inequality
lt less than
gt greater than
le less than or equal
ge greater than or equal
(Why do we have separate numeric and string comparisons? Because we don’t have special variable types, and Perl needs to know whether to sort numerically (where 99 is less than 100) or alphabetically (where 100 comes before 99).

Boolean Logic Operators

    &&  and
|| or
! not
(and , or and not aren’t just in the above table as descriptions of the operators — they’re also supported as operators in their own right. They’re more readable than the C-style operators, but have different precedence to && and friend.

Miscellaneous Operators

    =   assignment
. string concatenation
x string multiplication
.. range operator (creates a list of numbers)
Many operators can be combined with a = as follows:
    $a += 1;        # same as $a = $a + 1
$a -= 1; # same as $a = $a - 1
$a .= "n"; # same as $a = $a . "n";

Operator Precedence and Associativity

Perl operators have the following associativity and precedence, listed from highest precedence to lowest. Operators borrowed from C keep the same precedence relationship with each other, even where C’s precedence is slightly screwy. (This makes learning Perl easier for C folks.) With very few exceptions, these all operate on scalar v
alues only, not array values.
    left terms and list operators (leftward)
left ->
nonassoc ++ --
right **
right ! ~ and unary + and -
left =~ !~
left * / % x
left + - .
left << >>
nonassoc named unary operators
nonassoc < > <= >= lt gt le ge
nonassoc == != <=> eq ne cmp
left &
left | ^
left &&
left ||
nonassoc .. ...
right ?:
right = += -= *= etc.
left , =>
nonassoc list operators (rightward)
right not
left and
left or xor

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s