Perl Style: Embrace && and || for Control and Values

  • Perl’s && and || operators short circuit like C’s, but return different values: they return the first thing that resolves them.
  • This is most often used with ||:

        ++$count{ $shell || "/bin/sh" };
        $a = $b || 'DEFAULT';
        $x ||= 'DEFAULT';
  • Sometimes it can be done with && also, usually providing the false value is “ not 0. (False tests in Perl return ” not 0!).

        $nulled_href = $href . ($add_nulls && "\0");

Forward to Learn Precedence
Back to Parallelism
Up to index

Copyright © 1998, Tom Christiansen All rights reserved.



Something wrong with this article? Help us out by opening an issue or pull request on GitHub