13

This is probably a minor point, but I was wondering why Perl's logical operators (&&, ||, and !) take precedence over the easily understandable "English" logical operators (and, or and not). Is there any advantage of using the former set and any disadvantage of using the latter set in a script?

2
  • Because Larry Wall liked it that way?
    – mvp
    Mar 4, 2013 at 1:51
  • 1
    If they were identical in preference, it would not be necessary to keep both versions - only one would be enough to keep. Since Larry is a linguist and liked to use plain English words across his new language, he introduced these (alongside with unless and others). And of course, he assigned meaning to these words that he liked better (and still different from C-style operators)
    – mvp
    Mar 4, 2013 at 2:44

4 Answers 4

24

The original &&, || and ! operators are high precedence to match the C language.

The newer (but still old) and, or and not operators were added to simplify some common constructs. For example, compare:

open my $fh, '<', $filename || die "A horrible death!";
open my $fh, '<', $filename or die "A horrible death!";

The first of these is incorrect; the high priority || binds with $filename and die which is not what you want. The second is correct; the low priority or means that the missing parentheses do not lead to ambiguity.

1
  • 7
    +1 because this is the actual reason Larry Wall gives in Programming Perl. Mar 4, 2013 at 3:01
14

If || and or had the same precedence, then

return some_func $test1 || $test2;

would mean

return some_func($test1) || $test2;

instead of

return some_func($test1 || $test2);

or

some_func $test1 or die;

would mean

some_func($test1 or die);

instead of

some_func($test1) or die;

Neither of those changes are desirable.

And while one could debate or is more easily understood than ||, it's harder to read. It's easier to read code when the operators don't look like their operands.

0
-1

Logical Precedence and Clarity

While I have no idea whether this was the reason that got them in the language, they're incredibly useful for writing clear code,

## Some sample data...
my ($foo, $bar, $baz) = (0,1,1);

## Different results..
say ( $foo && $bar || $baz );
say ( $foo and $bar || $baz );

We can use this in code, even with \n. No noisy parens needed.

## When would you use this...
if (
  $cache->is_outdated
  and $db_master->has_connection || $db_slave->has_connection
) {
  $cache->refresh
}

This would otherwise have to be,

  $cache->is_outdated
  && ( $db_master->has_connection || $db_slave->has_connection )

But, perl doesn't like all that line-noise that other languages force upon its users.

-3

converting comment to an answer:

If these operators were identical in preference, it would not be necessary to keep both versions - having only one version would be enough.

But Larry Wall is a linguist, and he really liked to use plain English words across his new language. So, he introduced these English-style operators (alongside with unless and others).

To keep C-style operators and their classic meaning, he needed to make new keywords not redundant. Because of that he assigned slightly different meaning to these operators that he liked better. So that difference turned out to be operator precedence.

4
  • 1
    That isn't true: it is nothing to do with Larry's predilection for English words. and and or were included in addition to && and || purely to offer identical operators with a lower binding priority. They are particularly useful in Perl because function calls don't require parentheses around the parameters.
    – Borodin
    Mar 4, 2013 at 3:04
  • Isn't that what I said? If they were identical in priority, and and or would not exist today. And English-style constructs certainly do offer more flexibility, I am not disputing that either
    – mvp
    Mar 4, 2013 at 3:06
  • 2
    No it's not what you said. "[Larry Wall] really liked to use plain English words across his new language. So, he introduced these English-style operators...He needed to make new keywords not redundant [so] he assigned slightly different meaning to these operators that he liked better." is fairy tale nonsense.
    – Borodin
    Mar 4, 2013 at 3:23
  • The only person who can tell this for certain is Larry Wall himself
    – mvp
    Mar 4, 2013 at 3:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.