Perl Equality Operators


These are also called relational operators in Perl. Assume variable $a holds 10 and variable $b holds 20 then, let's check the following numeric equality operators available in Perl −

Sr.No.Operator & Description
1== (equal to)
Checks if the value of two operands are equal or not, if yes then condition becomes true.
Example− ($a == $b) is not true.
2!= (not equal to)
Checks if the value of two operands are equal or not, if values are not equal then the condition becomes true.
Example− ($a != $b) is true.
3<=>
Checks if the value of two operands are equal or not, and returns -1, 0, or 1 depending on whether the left argument is numerically less than, equal to, or greater than the right argument.
Example− ($a <=> $b) returns -1.
4> (greater than)
Checks if the value of the left operand is greater than the value of right operand, if yes then condition becomes true.
Example− ($a > $b) is not true.
5< (less than)
Checks if the value of the left operand is less than the value of right operand, if yes then condition becomes true.
Example− ($a < $b) is true.
6>= (greater than or equal to)
Checks if the value of the left operand is greater than or equal to the value of right operand, if yes then condition becomes true.
Example− ($a >= $b) is not true.
7<= (less than or equal to)
Checks if the value of the left operand is less than or equal to the value of right operand, if yes then condition becomes true.
Example− ($a <= $b) is true.

Below is a list of equity operators. Assume variable $a holds "abc" and variable $b holds "xyz" then, let's check the following string, equality operators −

Sr.No.Operator & Description
1lt
Returns true if the left argument is stringwise less than the right argument.
Example−($a lt $b) is true.
2gt
Returns true if the left argument is stringwise greater than the right argument.
Example−($a gt $b) is false.
3le
Returns true if the left argument is stringwise less than or equal to the right argument.
Example− ($a le $b) is true.
4ge
Returns true if the left argument is stringwise greater than or equal to the right argument.
Example−($a ge $b) is false.
5eq
Returns true if the left argument is stringwise equal to the right argument.
Example−($a eq $b) is false.
6ne
Returns true if the left argument is stringwise not equal to the right argument.
Example− ($a ne $b) is true.
7cmp
Returns -1, 0, or 1 depending on whether the left argument is stringwise less than, equal to, or greater than the right argument.
Example−($a cmp $b) is -1.

Updated on: 29-Nov-2019

277 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements