Perl Operators

Operators play a very crucial role in any programming language. As other programming language, the basic operators for perl remains the same, Addition, Subtraction, Multiplication and Division.

Seems simple, but things gets little complicated when there are several operators in a single statement. Here comes "precedence" and "associativity" to the rescue.

Operators in precedence with associativity:

1. **        Associativity: right
2. *,/,%    Associativity: left
3. +,-        Associativity: left

Let us try to understand precedence first with an example statement below:
    => 4+4/2-2+3%5
    => 4+2-2+3%5
    => 4+2-2+3
    => 6-2+3
    => 7

Associativity, means the operation direction to be followed when operators of same precedence are in a statement. From the above example, when we reached line "4+2-2+3", perl found that operators of same precedence was in the line, hence it followed the associativity rule, which was from left to right to evaluate the expression.

So far, we dealt with the Numerical Operators with which we perform/calculate mathematical expressions. Now let us focus on COMPARISON operators where we would compare numbers and/or strings.

Operation                          Num version         String version
less than                                                      <                                    lt
less than or equal                                     <=                                 le
greater than                                                >                                    gt
greater than or equal                               >=                                 ge
equal to                                                        ==                                  eq
not equal to                                                  !=                                  ne
compare                                                       <=>                             cmp

These COMPARISON operators evaluates to either a True or False, but here is the catch...
Everything in perl is true, except:
a. The empty string "" and "0" or any expression that evaluates to these values
b. Any numeric expression that evaluates to numeric 0
c. Any value that is not defined.

String Operator comparison:
While comparing strings, we use
- eq and ne operators

Three-value compare statements:
    - If $a set to 4, 3 <=> $a return -1
    - if $a set to 4, 4 <=> $a return 0
    - if $a set to 5, 5 <=> $a return 1

    - if $b set to Perl, 'Basicperl' cmp $b return -1 , Because, in ASCII value of first letter(B) in Basicperl is less than first letter(P) in Perl.
    - if $b set to Perl, 'Perl' cmp $b return 0, as Both the ASCII vales matches
    - if $b set to Perl, 'Pert' cmp $b return 1, because, ASCII values till the 3rd letter was same, but on the 4th letter, ASCII of t is greater than l

With that said, lets summarize the whole post with this small perl script:


use strict;
use warnings;

# Example to show precedence and associativity
print 2**2+4/2-1+3%5;
print "\n";

# Example to show that a null string is false
my $test = "";
print "baam\n";
print "Ouch\n";

# string comparisons

print "ad" lt "ab";
my $b = "Perl";
print 'Basicperl' cmp  $b;
print "\n";
print "Pert" cmp $b;
print "\n";

Thats all for this post, hope it was helpful..

Feel Free To Leave A Comment
If Our Article has Helped You, Support Us By Making A Small Contribution, Thank You!