Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: "or die" versus "|| die"

by ysth (Canon)
on Jun 24, 2005 at 15:33 UTC ( #469742=note: print w/replies, xml ) Need Help??


in reply to "or die" versus "|| die"

It's too easy to make precedence mistakes if you use the || variety for this kind of flow control. Witness later in that same faq: http://perldoc.perl.org/perlfaq5.html#How-do-I-randomly-update-a-binary-file%3f the error in the line:
read(FH, $record, $RECSIZE) == $RECSIZE || die "can't read record $r +ecno: $!";
Rule of thumb: use || when you are actually using the result of the operation, otherwise use or.

Replies are listed 'Best First'.
Re^2: "or die" versus "|| die"
by brian_d_foy (Abbot) on Jun 24, 2005 at 15:59 UTC

    Which error? The numeric comparison operator has a higher precedence. If the record size that perl read is the expected record size, the LHS evaluates to one and the short circuit terminates.

    The better rule of thumb is to use or when you want everything else to happen first.

    If you know about problems in the perlfaq, however, you can send them to perlfaq-workers @ perl.org and we'll fix them up.

    --
    brian d foy <brian@stonehenge.com>
      In terms of your "rule of thumb," I like to say that || goes between expressions, and or goes between statements. It's kinda like how , goes between elements, but ; goes between statements. I'm not sure if my phrasing is any more clear to the uninitiated than talking in greek, though.

      --
      [ e d @ h a l l e y . c c ]

        That's not a good rule though because it doesn't actually provide useful guidance and it makes people think about the operators all wrong. It has nothing to do with expressions or statements. It's all about operators and in which order perl evaluates them. That's the information you need to pass on in your rule so that people can figure out what to do.

        --
        brian d foy <brian@stonehenge.com>
      Gah, you are right, and I should know better.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://469742]
help
Chatterbox?
[choroba]: Why don't they use tire chains?
[perldigious]: First snow, probably not prepared right away choroba.
[stevieb]: it's Quebec... they do weird stuff there :) In the mountains, it is *mandatory* for trucks to use tire chains. Many passenger vehicles do as well (but it's not mandatory for them)
[perldigious]: That stuff can hit fast... like what we just got where I'm at. Saw a few people who had slid OUT of the roundabout I have to drive through to get to work, not used to the ice yet, take awhile to get the hang of it again and make vehicle adjustments.
[stevieb]: man, when the roads are glare ice, I don't even bother going to work or out... unless I absolutely have to, or was already out in the first place
[choroba]: Tire chains are mandatory here in mountains, and the only unprepared each year are gritters
[stevieb]: I have a 3 day winter survival kit in my vehicle in the event I get snowed in in the mountains (which has happened before due to avalanches closing the roads (and once in the summer due to a massive forest fire that trapped us
[perldigious]: a handful of people in my work area did not make it, but I live pretty close and it's flat the whole way, so I didn't have any trouble. The roundabout is the worst thing I have to negotiate.
[thezip]: G'day all!
[perldigious]: Well, that and dodging the people who forget how ice works right away. :-)

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (12)
As of 2016-12-06 16:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    On a regular basis, I'm most likely to spy upon:













    Results (112 votes). Check out past polls.