views:

410

answers:

4

What is the difference between the C-style operators &&, ||, ... and their Perl human-readable version "and", "or", ... ?

It seems that internet code uses them both :

open (FILE, $file) or die("cannot open $file");
open (FILE, $file) || die("cannot open $file");
+20  A: 

The only difference is their precedence.

open FILE,  $file or die("cannot open $file");   # this works
open FILE,  $file || die("cannot open $file");   # this doesn't work
open FILE, ($file || die("cannot open $file"));  # why it doesn't work
Dave Hinton
+5  A: 

The "&&" and "||" operators have higher precedence than their "and", "or" counterparts.

Inshallah
+5  A: 

From Perl Doc ..

  • OR

This is list operator . On the right side of a list operator, it has very low precedence, such that it controls all comma-separated expressions found there. The only operators with lower precedence are the logical operators "and", "or", and "not", which may be used to evaluate calls to list operators without the need for extra parentheses. Logical or, Defined or, and Exclusive Or

Binary "or" returns the logical disjunction of the two surrounding expressions. It's equivalent to || except for the very low precedence. This makes it useful for control flow

print FH $data   or die "Can't write to FH: $!";

This means that it short-circuits: i.e., the right expression is evaluated only if the left expression is false. Due to its precedence, you should probably avoid using this for assignment, only for control flow.

$a = $b or $c;   # bug: this is wrong
($a = $b) or $c;     # really means this
$a = $b || $c;   # better written this way

However, when it's a list-context assignment and you're trying to use "||" for control flow, you probably need "or" so that the assignment takes higher precedence.

@info = stat($file) || die;     # oops, scalar sense of stat!
@info = stat($file) or die;     # better, now @info gets its due

Then again, you could always use parentheses.

  • ||

If any list operator (print(), etc.) or any unary operator (chdir(), etc.) is followed by a left parenthesis as the next token, the operator and arguments within parentheses are taken to be of highest precedence, just like a normal function call. For example, because named unary operators are higher precedence than ||:

chdir $foo    || die;   # (chdir $foo) || die
chdir($foo)   || die;   # (chdir $foo) || die
chdir ($foo)  || die;   # (chdir $foo) || die
chdir +($foo) || die;   # (chdir $foo) || die
joe
Accepted answer, since it's more complete than Dave Hinton's
Steve Schnepp
+3  A: 

!, &&, ||, and ^ have high precedence such that they are useful in constructing an expression; not, and, or, and xor have low precedence such that they are useful for flow control between what are essentially different expressions.

ysth