Re: "or die" versus "|| die"
by jeffa (Bishop) on Jun 24, 2005 at 10:56 UTC
|
In your example it doesn't matter. One binds more tightly than the other, is the only real difference -- well, that and readablilty. || binds tighter. So, if you want to die after an assignment use or:
my $foo = $bar->fetch('baz') or die "something went wrong\n";
And if you want a default value, use ||
my $foo = $bar->fetch('baz') || 'default';
| [reply] [d/l] [select] |
Re: "or die" versus "|| die"
by kyoshu (Curate) on Jun 24, 2005 at 10:58 UTC
|
| [reply] |
|
|
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';
/J\ | [reply] [d/l] [select] |
|
|
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.
| [reply] |
|
|
| [reply] |
|
|
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
| [reply] |
|
|
They mean the same thing semantically, but they are not interchangable. The difference is precedence. See perlop.
thor
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
| [reply] |
|
|
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!
| [reply] [d/l] [select] |
|
|
<strike>like this</strike>
And yes, if you'll ever restore the original content... this post will look out-of-context!
Flavio
perl -ple'$_=reverse' <<<ti.xittelop@oivalf
Don't fool yourself.
| [reply] [d/l] [select] |
Re: "or die" versus "|| die"
by davidrw (Prior) on Jun 24, 2005 at 11:02 UTC
|
I think in a lot of cases it doesn't matter (especially if die'ing), but there is a difference in precedence. perldoc perlop shows that || is higher than = and some other operators, and or has the lowest pecedence.
I've had cases where || return needed to be changed to or return so that Devel::Cover wouldn't complain about never getting a true value for the other half of the construct. | [reply] [d/l] [select] |
|
|
somesub $this, $that || die;
If you use 'or' here, you'll die upon failure of somesub (assuming it returns false to indicate failure). If you use ||, you'll die if $that is false. Big difference.
| [reply] [d/l] [select] |
|
|
Just because you're invoking die after an || or a or doesn't mean you don't have to think about precedence. Consider the following:
somesub $this, $that || die;
If you use 'or' here, you'll die upon failure of somesub (assuming it returns false to indicate failure). If you use ||, you'll die if $that is false. Big difference.
If you call somesub with parens, the argument list is clearer to novice coders, the notation matches what they learned in grade 6 math class, and in this case, the precedence issue disappears.
In my experience, thinking too hard about precedence is usually a clue that you're being too tricky with your code.
| [reply] |
Re: "or die" versus "|| die"
by trammell (Priest) on Jun 24, 2005 at 11:17 UTC
|
| [reply] |
Re: "or die" versus "|| die"
by ysth (Canon) on Jun 24, 2005 at 11:33 UTC
|
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. | [reply] [d/l] |
|
|
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>
| [reply] [d/l] |
|
|
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 ]
| [reply] [d/l] [select] |
|
|
|
|
Gah, you are right, and I should know better.
| [reply] |
Re: "or die" versus "|| die"
by brian_d_foy (Abbot) on Jun 24, 2005 at 12:05 UTC
|
The perlfaq is and has been maintained by several people, so you see a lot of different styles and conventions. Rather than enforce a particular style, I let the answers show that there is more than one way to do things, although I have also started noted the authors of each answer.
The "best way" is up to you. The "or" version generally works out for people who don't like parentheses (since they don't use the parens' supreme precendence to define the order).
--
brian d foy <brian@stonehenge.com>
| [reply] |
Re: "or die" versus "|| die"
by bart (Canon) on Jun 24, 2005 at 13:17 UTC
|
|| is very old, or is newer. The latter is preferred, the reason why you still see || so often is indeed because or is a more recent addition.
And the difference is indeed just their precedence, or behaves more appropriately for this kind of functionality. | [reply] |
Re: "or die" versus "|| die"
by perlhaq (Scribe) on Jun 24, 2005 at 14:55 UTC
|
Like others have said, you're better off sticking with "or" except for those few cases where the || is necessary for precedence reasons. Familiarize yourself with the operator precendence chart in the "perlop" manual page, print it out and tape it to your monitor if you have to in the beginning (eventually you'll know it instinctively).
The few cases were I use || instead of "or" are constructions like these:
my $var = $cgi->param('foo') || $default{'foo'}; # assign default value if none given
my $var = $cgi->param('foo') || ''; # assign empty value if none given (to avoid warnings due to undef value)
if ($verbose and $bar || $baz) { print "starting job $job_num\n" } | [reply] |
Re: "or die" versus "|| die"
by Anonymous Monk on Jun 24, 2005 at 21:34 UTC
|
| [reply] |
|
|
| [reply] |
Re: "or die" versus "|| die"
by davido (Cardinal) on Jun 25, 2005 at 00:43 UTC
|
| [reply] |
Re: "or die" versus "|| die"
by aditya.singh (Acolyte) on Jun 27, 2005 at 05:20 UTC
|
Both '||' and 'or' are similar. These operators allow you to perform one of the two operations, starting with the first operation on the left-hand side. If that evaluates to false, only then will right side of (||, or) will be evaluated.
In this case, if your open() fails then the program will die. The difference comes when you use them in expressions. They have different precedence and '||' is evaluated before the 'or'. You can check perldoc perlop. | [reply] |