What are the logical operators and, or, & not in Perl?

A logical operator is a symbol or word that connects two or more expressions so that the value of the created expression is solely determined by the value of the original expressions and the operator’s meaning.

Below are the logical operators available in Perl:

  • and
  • or
  • not

Description

The table below explains each of the logical operators in Perl.

Operator Symbol

Operator Name

Example

Description

  • &&
  • and

Logical And

Operand-A && Operand-B

Returns True when both operand-A and operand-B are True; otherwise, it returns False.

  • ||
  • or

Logical Or

Operand-A || Operand-B

Returns True when either operand-A or operand-B is True; otherwise, it returns False.

  • not
  • !

Logical Not

not Operand-A

Returns the operand's logical state in reverse.

The figure below is the visual representation of the logical operators in Perl.

Visual representation of Logical Operators in Perl

Note:

  • True equates to 1.
  • False equates to 0.

Code

The following code demonstrates how to use these logical operators in Perl.

$A=10, $B=20, $C=30;
#And Operator
#Both True
if ($A<$B && $B<$C){
print "The value of (A<B && B<C): true \n";
} else {
print "The value of (A<B && B<C): false \n";
}
#First operand: True Second Operand: False
if ($A<$B and $B>$C){
print "The value of (A<B and B>C): true \n";
} else {
print "The value of (A<B and B>C): false \n";
}
#Both False
if ($A>$B && $B>$C){
print "The value of (A>B && B>C): true \n";
} else {
print "The value of (A>B && B>C): false \n";
}
#Or Operator
#Both True
if ($A<$B || $B<$C){
print "The value of (A<B || B<C): true \n";
} else {
print "The value of (A<B || B<C): false \n";
}
#First operand: True Second Operand: False
if ($A<$B or $B>$C){
print "The value of (A<B or B>C): true \n";
} else {
print "The value of (A<B or B>C): false \n";
}
#Both False
if ($A>$B || $B>$C) {
print "The value of (A>B || B>C): true \n";
} else {
print "The value of (A>B || B>C): false \n";
}
#Not operator
if (not($A<$B))
{
print "The value of not(A<B): true \n";
} else {
print "The value of not(A<B): false \n";
}

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved