Perl Operators   «Prev  Next»
Lesson 7 Boolean and relational operators
ObjectiveWrite a program that uses conditional operators to process a yes/no question.

Perl Boolean and Relational Operators

The following section discusses more about the boolean and relational operators used in Perl.
Different Perl Operators
Operator categoryUsed for
1) Numeric Relational OperatorsNumerical comparisons
2) String relational operators String Comparisons
3) Numerical equality operators Testing Numerical equality
4) String equality operators Testing String Equality
5) Boolean Operators Boolean Algebra
6) Shortcut operators Boolean Shortcuts
7) Ternary operator If-then-else


1) Numeric Relational Operators

OperatorDescription
>greater than
<less than
>=greater than or equal to
<=less than or equal To

The numerical relational operators are used to test numerical relationships. For example,
if ($x > $y) { 
some code here 
}
else { 
some other code here 
}

These operators test the relative value of numeric data.
It is an error to use these operators with strings.


2) String Relational Operators

OperatorDescription
gt greater than
lt less than
ge greater than or equal to
le less than or equal to

Strings are evaluated based on their ASCII value (so a is greater than A) and spaces are significant. These operators test the relative value of string data. It is an error to use these operators with numbers. The string relational operators are used to test relationships between strings. For example,
if ($s1 gt $s2) { 
  some code here
}
else { 
  some other code here
}


3) Perl Numeric equality Operators

Operator Description
== equal to
!= not equal to

The numeric equality operators are used to test equality between numbers. For example,
while($x == 42) { 
some code here 
}

These operators work with numeric data.
It is an error to use these operators with strings.

Assignment Operators
Perl offers a wide variety of assignment operators, including many shortcut operators to handle common tasks. Table 3-7 lists these operators. The lvalue is the left hand side of the operator and the rvalue is the expression on the right.
Table3-7: Perl Assignment Operators
Operator Equivalent Expression
= Assign rvalue to lvalue
+= lvalue = lvalue + rvalue
-= lvalue = lvalue - rvalue
*= lvalue = lvalue * rvalue
/= lvalue = lvalue / rvalue
||= lvalue = rvalue if !lvalue
//= lvalue = rvalue if !defined lvalue
&&= lvalue = lvalue && rvalue
|= lvalue = lvalue | rvalue
&= lvalue = lvalue & rvalue
**= lvalue = lvalue ** rvalue
x= lvalue = lvalue x rvalue
<<= lvalue = lvalue << rvalue
>>= lvalue = lvalue >> rvalue
^= lvalue = lvalue ^ rvalue


4) String Equality Operators

Operator Description
eqequal to
nenot equal to

The string equality operators are used to test equality between strings. For example,
while($s ne 'error') 
{ some code here }

These operators work with string data. It is an error to use these operators with numbers.


5) Boolean Operators in Perl

These conditional operators perform the common boolean algebraic operations: and, or, and not.
OperatorDescription
&&boolean and
||boolean or
!boolean not

Here are several examples:
if($this && $that) { 
some code here
}
unless($this || !$the_other) { 
some code here
}

Some older code would occasionally use these operators for shortcuts, but Perl 5 now has low precedence operators specifically for that purpose.
The && and || operators have the special property of returning the last value evaluated, instead of 1 for truth. That allows the following to work:
$x = shift || "default";

If the shift operator doesn't return a value, the "default" value will be assigned to $x instead.




6) Perl Shortcut Operators

Operator Description
and boolean and (low precedence)
or boolean or (low precedence)
not boolean not (low precedence)

The shortcut operators are identical to the Boolean algebra operators, except that they are of the lowest possible precedence.
Shortcut operators are designed for simple conditional execution of code. This purpose used to be served by the Boolean algebraic operators, but these shortcut operators have low-enough precedence to make them work properly without parenthesis.
Here is an example:
$x = shift or $state = "first";

If you tried to do this with the || operator, like this:
$x = shift || $state = "first";

you would find that you needed parenthesis around the ($x = shift) part of the expression to make the assignment bind first.
Since the or operator has lower precedence than the = assignment operator, the entire expression $x = shift is evaluated before the condition is checked, giving the expected behavior.



7) Perl Ternary Operators

In computer programming, the following notation ?: is a ternary operator that is part of the syntax for basic conditional expressions. It is commonly referred to as the conditional operator or ternary if.
Operator Description
? : if then else

The ternary operator uses three expressions to do its work:
$x = $a ? $b : $c

This operator evaluates from left to right:
  1. $a is evaluated.
  2. If $a is nonzero, $b is evaluated and returned.
  3. Otherwise, $c is evaluated and returned.

So, given this:
$a = 1; $b = 2; $c = 3;
$x = $a ? $b : $c;

The value of $b (2) is assigned to $x. If $a were zero or undefined, the value of $c would be assigned to $x. Note that if $a evaluates to true (non-zero), then $b will be evaluated, but $c will not. If $a evaluates to false (zero), then $c will be evaluated but $b will not. Thus you should not write code using the ternary operator that depends on both $b and $c being evaluated.



Understanding Boolean Operators

You use Boolean operators to determine if a value or expression is true or false. Because Perl lets you assign strings and numbers to variables, the Boolean operators are separated into string and numeric versions. You learn the string versions first. you see the if/else statement now just so you can understand how they work. The if statement takes an expression in parentheses and, if it evaluates as true, executes the code in the block following it. If an else block follows the if block, the else block executes only if the if expression evaluates as false. For example:
my ( $num1, $num2 ) = ( 7, 5 );
if ( $num1 < $num2 ) {
 print "$num1 is less than $num2\n";
}
else {
 print "$num1 is not less than $num2\n";
}

That code prints 7 is not less than 5. The < Boolean operator is the Boolean less than operator and returns true if the left operand is less than the right operand. Now that you have this small example out of the way, the following sections discuss the boolean operators.
Note: In order to complete this lesson's exercise, you must be familiar with these operators.

Conditional Operators - Exercise

Click the Exercise link below to practice writing conditional code.
Conditional Operators - Exercise

SEMrush Software