What is the difference between Perl's ( or, and ) and ( ||, && ) short-circuit operators?
Asked Answered
K

5

34

Which of these subroutines is not like the other?

sub or1 {
    my ($a,$b) = @_;
    return $a || $b;
}

sub or2 {
    my ($a,$b) = @_;
    $a || $b;
}

sub or3 {
    my ($a,$b) = @_;
    return $a or $b;
}

sub or4 {
    my ($a,$b) = @_;
    $a or $b;
}

I came to Perl 5 from C and Perl 4 and always used || until I saw more scripts using or and I liked the way it looked. But as the above quiz shows, it's not without its pitfalls for the unwary. For people who use both constructs or who use a lot of or, what rules of thumb do you use to decide which construct to use and make sure the code is doing what you think it is doing?

Kennedy answered 3/10, 2009 at 1:43 Comment(2)
I'll take Japan-US relations for $200, Trebek.Methodius
Possible duplicate of What is the difference between "||" and "or" in Perl?Nauseous
T
42

Due to the low precedence of the 'or' operator, or3 parses as follows:

sub or3 {
    my ($a,$b) = @_;
    (return $a) or $b;
}

The usual advice is to only use the 'or' operator for control flow:

@info = stat($file) or die;

For more discussion, see the perl manual: http://perldoc.perl.org/perlop.html#Logical-or-and-Exclusive-Or

Twodimensional answered 3/10, 2009 at 1:55 Comment(1)
'or' operator has precedence even lower than assignment operator, so beware.Bargainbasement
O
18

What rules of thumb do you use to decide which construct to use and make sure the code is doing what you think it is doing

The operator precedence rules.

|| binds tightly, or binds weakly. There is no "rule of thumb".

If you must have a rule of thumb, how about "only use or when there is no lvalue":

or:

open my $fh, '>', 'file' or die "Failed to open file: $!"

||:

my $greeting = greet() || $fallback || 'OH HAI';

I agree with MJD about avoiding parens; if you don't know the rules, look them up... but don't write (open(my $fh, '>', 'file')) or (die("Failed to open file: $!")) "just to be sure", please.

Ormsby answered 3/10, 2009 at 3:35 Comment(0)
O
8

In Perl 5, "or" and "and" have lower precedence than "||" and "&&". Check out this PerlMonks thread for more info:

http://www.perlmonks.org/?node_id=155804

Oxcart answered 3/10, 2009 at 1:53 Comment(0)
A
2

Both versions are short-circuiting in Perl, but the 'textual' forms ('and' and 'or') have a lower precedence than their C-style equivalents.

http://www.sdsc.edu/~moreland/courses/IntroPerl/docs/manual/pod/perlop.html#Logical_And

Algesia answered 3/10, 2009 at 1:53 Comment(0)
T
1

My guess is that or3 is different.

I'm not really a Perl guy, but it looks like 1, 2, and 4 all explicitly return booleans. I'm guessing 3 has side effects, such as returning $a or something crazy like that.

looks down

Hey, I was right.

Thurible answered 3/10, 2009 at 1:46 Comment(3)
Correct, but you don't say why.Riehl
@Ether: I didn't know why. :PThurible
You've won the guessing game!Ormsby

© 2022 - 2024 — McMap. All rights reserved.