1. Perl
  2. Operators
  3. here

Perl logical operators

Perl logical operators include:

operator meaning
| | Logical sum
&& Logical AND
!! denial
or OR (lower priority than &&)
and Lower priority than AND &&)
not Negation (lower priority than!)

&& AND (A and B)

Use && to represent the conjunction "A and B".

# Greater than 1 and less than 4
if ($num > 1 && $num < 4) {
  ...
}

|| OR (A or B)

Use | | to represent the conjunction "A or B". True if at least one of the values is true.

# 'dog' or'cat'
if ($pet eq 'dog' || $pet eq 'cat') {
  print "\$pet is either'dog'or'cat'.\n\n";
}

! Negation (not A)

Use ! to represent the negation of "not A".

# $num is not true
if (!$num) {
  ...
}

and AND (A and B) Low priority

You can also use and to represent the conjunction "A and B". The priority is lower than "&&". It is recommended to use "&&" for AND. "And" can be used for conditional branch explained below.

# Greater than 1 and less than 4
if ($num > 1 and $num < 4) {
  ...
}

or OR (A or B) Low priority

You can also use or to represent the conjunction "A or B". The priority is lower than "||". It is recommended to use "||" for the logical sum. "Or" can be used for conditional branch explained below.

# 'dog' or'cat'
if ($pet eq 'dog' or $pet eq 'cat') {
  print "\$pet is either'dog'or'cat'.\n\n";
}

not Negative (not A) Low priority

You can also use not to represent the negation of "not A". The priority is lower than "!". It is recommended to use "!" Instead of "not" for denial.

# $num is not true
if (not $num) {
  ...
}

General usage of logical operators

When using in if statement, unless statement, while statement, for statement, it is Perl to use "&&" "||" "!" It has become a custom. and and not are rarely used.

# Logical operator
if ($var1 && $var2 || $var3) {
  ...
}

"&&" has a higher priority than "||". If you want to change the priority, use parentheses "()".

if ($var1 && ($var2 || $var3)) {
  ...
}

Perl logical operator evaluation features

Perl logical operators stop evaluation when the evaluation is decided. for example

$var1 && $var2

If $var1 is false, it is determined that "$var1 & amp; & amp; $var2" is false, so the evaluation ends there.

$var1 || $var2

If $var1 is true, then "$var1 || $var2" is determined to be true, and the evaluation ends there.

Use

or as a conditional branch

Taking advantage of these properties, or is often used for conditional branch. For example, if open function fails, write as follows.

open my $fh, '<', $file
  or die qq/Can't open file "$file":$!/;

If the open function succeeds, true is returned and the right-hand side of or is not executed. If the open function fails, undef is returned, so die on the right side is executed and the program ends.

As another example, it is also used with the system function. The system function returns 0 on success, so write:

system(@cmd) == 0
  or die "Can't execute command @cmd";

If you want to write the main processing first and write supplementary error processing later, you can use conditional branch with or.

You can use "and" for conditional branch, but "or" is recommended because it is difficult to read.

# Example rewritten with and
system(@cmd)
  and die "Can't execute command @cmd";

Example program

It is an example of logical product, logical sum, and negation.

use strict;
use warnings;

# AND, OR, negation
print "1. Logical product (A and B)\n";
my $num = 3;
if ($num > 1 && $num < 4) {
  print "\$num is greater than 1 and less than 4\n\n";
}

print "2. OR (A or B)\n";
my $pet = 'dog';
if ($pet eq 'dog' || $pet eq 'cat') {
  print "\$pet is either'dog'or'cat'.\n\n";
}

print "3. Negation (not A)\n";
my $num0 = 0;
if (!$num0) {
  print "! \$Num is true\n";
}

Related Informatrion