SoFunction
Updated on 2025-04-07

The difference between or in perl and ||

Let’s see what the following code will output?

Copy the codeThe code is as follows:

my $a = 0;
$a = $a or 1;
print $a, "\n";
$a = $a || 1;
print $a, "\n";

Output:
0
1
Why? Because the priority of these three gradually decreases from left to right.
So, writing this way can:
chomp(my $filename = shift( @ARGV ) || <STDIN>);

This won't work:
chomp(my $filename = shift( @ARGV ) or <STDIN>);
The following error will be prompted:
Can't modify logical or (||) in chomp