Re: "or die" versus "|| die"
by jeffa (Bishop) on Jun 24, 2005 at 14: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] [Watch: Dir/Any] [d/l] [select] |
Re: "or die" versus "|| die"
by trammell (Priest) on Jun 24, 2005 at 15:17 UTC
|
| [reply] [Watch: Dir/Any] |
Re: "or die" versus "|| die"
by brian_d_foy (Abbot) on Jun 24, 2005 at 16: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] [Watch: Dir/Any] |
Re: "or die" versus "|| die"
by davidrw (Prior) on Jun 24, 2005 at 15: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] [Watch: Dir/Any] [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] [Watch: Dir/Any] [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] [Watch: Dir/Any] |
Re: "or die" versus "|| die"
by perlhaq (Scribe) on Jun 24, 2005 at 18: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] [Watch: Dir/Any] |
Re: "or die" versus "|| die"
by davido (Cardinal) on Jun 25, 2005 at 04:43 UTC
|
| [reply] [Watch: Dir/Any] |
Re: "or die" versus "|| die"
by bart (Canon) on Jun 24, 2005 at 17: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] [Watch: Dir/Any] |
Re: "or die" versus "|| die"
by ysth (Canon) on Jun 24, 2005 at 15: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] [Watch: Dir/Any] [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] [Watch: Dir/Any] [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] [Watch: Dir/Any] [d/l] [select] |
|
|
Gah, you are right, and I should know better.
| [reply] [Watch: Dir/Any] |
Re: "or die" versus "|| die"
by Anonymous Monk on Jun 25, 2005 at 01:34 UTC
|
| [reply] [Watch: Dir/Any] |
|
| [reply] [Watch: Dir/Any] |
Re: "or die" versus "|| die"
by aditya.singh (Acolyte) on Jun 27, 2005 at 09: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] [Watch: Dir/Any] |
A reply falls below the community's threshold of quality. You may see it by logging in. |