Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

'or' vs '', '&&' vs 'and'

by Anonymous Monk
on Apr 01, 2002 at 17:44 UTC ( #155804=perlquestion: print w/ replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

When testing the success of failure of an open command, is it better to use '||' or 'or':
my $file = 'somefile.txt'; open (THEFILE, ">$file") || die "Could not create $file - $!\n";
vs
my $file = 'somefile.txt'; open (THEFILE, ">$file") or die "Could not create $file - $!\n";
What about '&&' vs 'and' when testing the success or failure when using system:
my $command = "/bin/cp fileone filetwo"; system ($command) and die "Fooey, $command - $!\n";
vs
my $command = "/bin/cp fileone filetwo"; system ($command) && die "Fooey, $command - $!\n";

Comment on 'or' vs '', '&&' vs 'and'
Select or Download Code
(Ovid) Re: 'or' vs '', '&&' vs 'and'
by Ovid (Cardinal) on Apr 01, 2002 at 17:48 UTC

    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.

      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
        

Re: 'or' vs '', '&&' vs 'and'
by rdfield (Priest) on Apr 01, 2002 at 17:50 UTC
    Way to go starting a religious war :) Personally I use 'and' and 'or' for readability. Purists will no doubt point out the slight difference in precedence between the two forms, but for all practical purposes (AFAIK) there's no real difference.

    rdfield

      Purists will no doubt point out the slight difference in precedence between the two forms, but for all practical purposes (AFAIK) there's no real difference.

      Anyone who is serious about programming will appreciate that "slight differences" can have significant consequences. Painting such people as "purists" says something sad about HFYK (how far you know).

      Actually I agree with dws. Getting into a habit of using either I think is probably a bad idea. I used the low precedence ones only until I got bitten by them, then I switched to the highe precedence once and got bitten again. Now I tend to try to be as careful and deliberate as possible, but I still occasionally screw it up.

      Little mental mantra: Parens are a symbol so use the symbols. No parens? Use the words.

      Yves / DeMerphq
      ---
      Writing a good benchmark isnt as easy as it might look.

Re: 'or' vs '', '&&' vs 'and'
by mrbbking (Hermit) on Apr 01, 2002 at 18:35 UTC
    "Better" is (usually)? a matter of opinion.

    For the examples you gave, I like to use 'and' and 'or' instead of '&&' and '||', and I use parentheses.

    The parens allow me to forget which set has higher precedence.

    And I like the 'and' and 'or' because they are more natural.

Re: 'or' vs '', '&&' vs 'and'
by vek (Prior) on Apr 01, 2002 at 18:46 UTC
    Way to go starting a religious war :) - rdfield++.

    Not wishing to get involved in said religious war I'll not state my preference :-). All I will say is that you need to be careful if you decide not to spell out the keywords - make sure you don't forget those parens otherwise you won't catch failures:
    # a subtle bug... open THEFILE, ">/somedir/somefile" || die "Could not create /somedir/somefile - $!\n"
    If you do that, you'll never know something bad happened.
Re: 'or' vs '', '&&' vs 'and'
by Parham (Friar) on Apr 01, 2002 at 19:11 UTC
    i was taught to use "or" and "and" instead of || and && and was never told why. Later on i discovered what the rest of the monks have already stated. With "or" and "and" you have the advantage of including or not including parentheses whereas with "||" and "&&" they must be included.
Re: 'or' vs '', '&&' vs 'and'
by rbc (Curate) on Apr 01, 2002 at 19:12 UTC
    For opening files I like to do this ...
    if( !open( FH, "<$file") ) { die "Cannot open $file :$!\n"; }
    ... just reminds me of C.
      I much prefer the 'unless' version of this as for me (who needs new glasses) I can sometimes miss the ! so I prefer.
      unless(open( FH, "<$file") ) { # Error code, logging, bits, bobs (etc etc) die "Cannot open $file :$!\n"; }

      As a matter of personal taste I think this is more natural (to me).. unless this do this ;P
Re: 'or' vs '', '&&' vs 'and'
by brianarn (Chaplain) on Apr 01, 2002 at 19:21 UTC
    The difference is the precedence, which can be handy not just for opening files. I still prefer to use parens when I open cause I like the look of it better, but then use an or as well.

    You can also mix them to make some checks easier, seeing as || has a higher precedence than and, saving some parentheses.
    if ($a || $b and $c) if (($a or $b) and $c) if(($a||$b)&&$c)
    All of these lines are equal.
    The parens add readability, but hey, maybe you like the visual of only one set of parens. :) TMTOWTDI

    ~Brian
Re: 'or' vs '', '&&' vs 'and'
by NodeReaper (Curate) on Apr 01, 2002 at 19:50 UTC

    Reason: (dws) delete, swiftly.

    For more information on this node visit: this


      Reason: (dws) delete -- more anonymous trollage

      For more information on this node visit: this

Re: 'or' vs '', '&&' vs 'and'
by Popcorn Dave (Abbot) on Apr 01, 2002 at 20:47 UTC
    Okay, the way I was taught was to do it was: my $file = 'somefile.txt'; die "Could not open file" unless open (THEFILE, ">$file"); Is this now preferable to the or/|| or it just a matter of personal preference? How much of the religious war flames does this fan? :)
Re: 'or' vs '', '&&' vs 'and'
by Juerd (Abbot) on Apr 01, 2002 at 20:55 UTC

    A lot of people have already mentioned the precedence, in particular with open. An example implementing both would be:

    open FILE, $file || 'default.txt' or die "Can't open: $!";
    If you don't know how something will be parsed, use B::Deparse, and have it add a lot of parenthesis using -p:
    perl -MO=Deparse,-p -e'open FILE, $file || "default.txt" or die "Canno +t open: $!";'
    Which outputs (without color):
    
    (open(FILE, ($file || 'default.txt')) or die("Cannot open: $!"));
    -e syntax OK
    
    Hope this helps understanding.

    U28geW91IGNhbiBhbGwgcm90MTMgY
    W5kIHBhY2soKS4gQnV0IGRvIHlvdS
    ByZWNvZ25pc2UgQmFzZTY0IHdoZW4
    geW91IHNlZSBpdD8gIC0tIEp1ZXJk
    

Re: 'or' vs '', '&&' vs 'and'
by seanbo (Chaplain) on Apr 01, 2002 at 21:08 UTC
    For readability, I prefer the following:
    open (THEFILE, ">$file") or die "Could not create $file - $!\n";
    The parens are not needed, but I think it helps to keep things clean. Readable code is always helpful!!

    perl -e 'print reverse qw/o b n a e s/;'

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://155804]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (4)
As of 2014-07-12 23:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (242 votes), past polls