Operators are symbols which tell the compiler to perform certain operations on variables. You can take, for example, (*) which is an operator used for multiplying the values of two variables.
There are different types of operators in Perl. Let's take a look at some of the most important operators with a few examples for each.
Arithmetic Operators are the type of operators which take numerical values (either literals or variables) as their operands and return a single numerical value.
Let's assume 'a' takes value 5 and 'b' takes value 2.
|-||Subtracts second operand from the first||a-b=3|
|*||Multiplies both operands||a*b=10|
|/||Divides numerator by denominator.||a/b=2.5|
|%||Modulus Operator returns the remainder of after an integer division.||a%b=1|
|**||Exponent Operator performs exponential (power) calculation on operators.||a**b=25|
$a = 5; $b = 2; print $a+$b,"\n"; print $a-$b,"\n"; print $a*$b,"\n"; print $a/$b,"\n"; print $a%$b,"\n"; print $a**$b,"\n";
Operator Precedence and Associativity
In Maths, you might have learned about BODMAS rule, but that rule is not applied here. If we have written more than one operation in one line, then which operation should be done first is governed by the following rules :-
Expressions inside brackets '()' are evaluated first. After that, this table is followed:
Note that there are a number of operators in this table you haven't learned about yet.
Operators at the top have higher precedence than the lower ones.
Just have a look at the table. You will get these things from the examples given after this table.
|! ~ \ + -||right|
|* / % x||left|
|+ - .||left|
|< > <= >= lt gt le ge||none|
|== != <=> eq ne cmp||none|
|= += -= *= /=, etc.||right|
Now, suppose you have an expression $k = 8/4+3-5*6+8 to solve
Highest priority operators are '*' and '/', as they are on the top of the table.
Since both operators have the same precedence, so we will follow associativity and it is left. So, we will solve from left to right.
Solving, '*' and '/' first from left to right.
$k = 2+3-30+8
Now solving, '+' and '-' from left to right (Its associativity is right).
$k = -17
Now solving '=' from right to left
k is -17.
$k = 8/4+3-5*6+8; print $k,"\n";
Let's see one more example.
$a = 1; $a = $a+1; print $a,"\n"; $a = $a*5; print $a,"\n";
It is very simple. '+' operator has higher precedence than '=' operator. So, '$a+1' will be evaluated first and it will be 2. Then '=' operator will be evaluated giving '$a = 2;'. So, now 'a' is 2.
Similarly, $a*5 will be first evaluated as '5*2', then '=' operator will be evaluated ($a = 10;) and 'a' will become 10.
Assignment Operators are used to assign values of the operand on the right to the operand on the left. The most common assignment operator is =.
If we write $a = 10; means that we are assigning a value '10' to the variable 'a'.
There are more assignment operators which are listed below.
|=||Assigns value of the right operand to the left operand||$C = $A + $B|
|+=||Adds the value of right operand to the left operand and assigns the final value to the left operand||$C += $A is same as $C = $C + $A|
|-=||Subtracts the value of right operand to the left operand and assigns the final value to the left operand||$C -= $A is same as $C = $C - $A|
|*=||Multiplies the value of right operand to the left operand and assigns the final value to the left operand||$C *= $A is same as $C = $C * $A|
|/=||Divides the value of left operand from the right operand and assigns the final value to the left operand||$C /= $A is same as $C = $C / $A|
|%=||takes modulus using two operands and assigns the result to the left operand||$C %= $A is same as $C = $C % $A|
|**=||performs exponential operation and assigns the result to the left operand||$C **= $A is same as $C = $C**$A|
Let's take an example:
$a = 25; $b = 8; $a += $b; print $a,"\n"; $a -= $b; print $a,"\n"; $a *= $b; print $a,"\n"; $a /= $b; print $a,"\n"; $a %= $b; print $a,"\n"; $a **= $b; print $a,"\n";
Initially 'a' is 25 and 'b' is 8.
$a += $b; - It means that $a = $a+$b. Now, a is a+b i.e. 33. ( $a = $a+$b )
$a -= $b - Now, a is 33. So, $a -= $b ( $a = $a-$b ) is 25. Thus, the new 'a' is 25. The same is for the next cases.
Relational Operators check the relationship between two operands.
Following is the list of relational operators in Perl for numeric values.
Assume the value of 'a' to be 8 and that of 'b' to be 4.
|==||Equal to||($a == $b) is false|
|!=||Not equal to||($a != $b) is true|
|<=>||Compares two operands, returns -1, 0, or 1 depending on whether the left argument is numerically less than, equal to, or greater than the right argument.||($a <=> $b) will return 1|
|>||Greater than||($a > $b) is true|
|<||Less than||($a < $b) is false|
|>=||Greater than or equal to||($a >= $b) is true|
|<=||Less than or equal to||($a <= $b) is false|
Following is the list of relational operators in Perl for string values.
We use six comparison operators to compare strings, to check if one string is alphabetically bigger, smaller or equal to another. Here are the string operators and numerical operators they correspond to:
|String Operator||Numerical Operator|
Difference between = and ==
Although = and == seem to be same, but they are quite different from each other. = is the assignment operator while == is the equality operator. = is used to assign values whereas == is used for comparing values.
Take two examples.
$x = 5;
$x == 5;
By writing $x = 5, we assign a value of 5 to 'x', whereas by writing $x == 5, we check if the value of x is 5 or not.
Assume that the value of 'a' is 4.
|.||concatenates two strings||"ab"."cd" is "abcd"|
|..||range operator||(2..4) will give (2, 3, 4)|
|++||Increment operator||$a++ will give 5|
|--||Decrement operator||$a-- will give 3|
|x||Repetition operator x||"a"x3 will give "aaa"|
|->||Used for dereferencing a method or a variable from an object or a class name||You will study later|
You will not learn all the operators mentioned above in this chapter. Let's see an example:
$a = 25; print $a++,"\n"; print $a--,"\n"; print "a"x3,"\n";