in reply to 'or' vs '', '&&' vs 'and'

When you spell out those kewords ("||" as "or", "&&" as "and"), you are using a keyword with a lower precedence, thus allowing you to skip parentheses:

open (THEFILE, ">$file") || die "Could not create $file - $!\n"; # versus: open THEFILE, ">$file" or die "Could not create $file - $!\n";

I like spelling them out as they make reading the expression more natural (because it's a word instead of a symbol) and they allow you to avoid a lot of excessive parentheses.

Cheers,
Ovid

Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.

Replies are listed 'Best First'.
Re: (Ovid) Re: 'or' vs '', '&&' vs 'and'
by admiralh (Sexton) on Apr 01, 2002 at 20:03 UTC
    Just remember that sometimes "excessive" parentheses can enhance the readability of your code. Especially when you have to "enhance" your own code 6 months (or more!) after you originally wrote it :-)

    That said, for these situations I always use 'and' and 'or'.

      Just remember that sometimes "excessive" parentheses can enhance the readability of your code.

      Parentheses that enhance readability are never excessive.

      U28geW91IGNhbiBhbGwgcm90MTMgY
      W5kIHBhY2soKS4gQnV0IGRvIHlvdS
      ByZWNvZ25pc2UgQmFzZTY0IHdoZW4
      geW91IHNlZSBpdD8gIC0tIEp1ZXJk