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

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

by kyoshu (Curate)
on Jun 24, 2005 at 14:58 UTC ( #469728=note: print w/replies, xml ) Need Help??

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

This node falls below the community's threshold of quality. You may see it by logging in.

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

    No they cannot be used interchangeably in all places, the word versions are specifically guaranteed to have lower precedence than the symbol versions (infact guaranteed to have the lowest precedence of any operators), you might care to try for instance:

    open ONE, "somefilethatdoesnexist" or print "one: $!\n"; open TWO, "somefilethatdoesnexist" || print "two: $!\n";
    As you can see the latter doesn't behave as expected as the '||' binds more tightly and ends up taking the value of the filename - infact the check is optimised away because this is essentially a no-op, as the output from B::Deparse shows:
    print "one: $!\n" unless open ONE, 'somefilethatdoesnexist'; open TWO, 'somefilethatdoesnexist';


Re^2: "or die" versus "|| die"
by rev_1318 (Chaplain) on Jun 24, 2005 at 15:21 UTC
    Yes these is a difference, as jeffa pointed out: precedence. As long as you use parentheses around e.d. open, it doesn't matter, but else it really makes a difference if you say:
    open F, $file or die;
    which will die when the open failes, or:
    open F, $file || die
    which will die if $file is false (undef, empty or 0), but NOT if the open fails!


Re^2: "or die" versus "|| die"
by thor (Priest) on Jun 24, 2005 at 15:14 UTC
    They mean the same thing semantically, but they are not interchangable. The difference is precedence. See perlop.


    Feel the white light, the light within
    Be your own disciple, fan the sparks of will
    For all of us waiting, your kingdom will come

Re^2: "or die" versus "|| die"
by waswas-fng (Curate) on Jun 24, 2005 at 15:09 UTC
    Jeffa's response shows the difference -- because of the way those ops are weighted for ordering, depending on what statements you use them in, they end up meaning different things. In the source that is shown as on the OP's example there is no difference in execution -- but that can't be said in a blanket statement.

Re^2: "or die" versus "|| die"
by polettix (Vicar) on Jun 25, 2005 at 13:50 UTC
    kyoshu, please don't delete your posts this way. It makes the following answers look out-of-context, and deny the possibility to learn to the other readers. Many times one learns from errors, even others' ones.

    It's better to put a pen strike over the sentences you don't agree any more like this, which can be obtained via the strike tag:

    <strike>like this</strike>
    And yes, if you'll ever restore the original content... this post will look out-of-context!

    perl -ple'$_=reverse' <<<ti.xittelop@oivalf

    Don't fool yourself.
Re^2: "or die" versus "|| die"
by VSarkiss (Monsignor) on Jun 24, 2005 at 15:10 UTC

      Beware of violating your own sig.

      For a very thorough explaination of just what the differences are between the two (just an issue with precedence, which has already been covered), and the order of precedence for all perl operators, see the perldoc perlop

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://469728]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (4)
As of 2021-03-08 11:45 GMT
Find Nodes?
    Voting Booth?
    My favorite kind of desktop background is:

    Results (124 votes). Check out past polls.