On 4/28/05, John W. Krahn <[EMAIL PROTECTED]> wrote:
> Jay Savage wrote:
> > On 4/27/05, John W. Krahn <[EMAIL PROTECTED]> wrote:
> >
> >>Jay Savage wrote:
> >>
> >>>    4> open (V4, "samcmd v4 2>\&1 |" ) || die "unable to open pipe...
> >>>Broken?$!";
> >>>
> >>>Don't do this.  the precedence of || is too high.  your code attempts
> >>>to open a pipe, and if it can't, then it attempts to open "die..." and
> >>>starts throwing exceptions.
> >>
> >>No, that is NOT what happens, it will NEVER attempt to open "die..." with or
> >>without the parentheses.  The ONLY time it will attempt to open "die..." is 
> >>if
> >>there are no parentheses and the expression on the left hand side of the ||
> >>operator evaluates to false.
> >>
> >>open V4, '0' || die $!;
> >>
> >>But even then it will NOT attempt to open "die..." because die() exits the
> >>program!
> >
> > Is stand corrected.  There is no exception; I guess any time I've run
> > into it, I've relied on whatever was opened, and died anyway.  But I
> > don't know what else to call opens behavior, except attempting to open
> > die.  Except in the case of parenthesis, as you noted, the behavior of
> > open || die sure looks like this to me:  open (X, badfile || die).
> > The only difference between the two expressions below is the
> > precedence of the operator.
> >
> >     [EMAIL PROTECTED]:~> perl -e 'open FH, "< BAdFiLe" || die "$!"'
> >     [EMAIL PROTECTED]:~> perl -e 'open FH, "< BAdFiLe" or die "$!"'
> >          No such file or directory at -e line 1.
> >
> > Open may not technically be trying to open an expression and failing,
> > I don't know.  To be honest, I've never taken apart the source to see.
> >  But the appearance is certainly that that's what happens, and the
> > result is so similar as to not matter.  Especially consider the
> > following:
> >
> >     perl -e 'open FH, "< BAdFiLe" || die or die "$!"; print "didnt die\n" '
> >          No such file or directory at -e line 1.
> >
> > Where did the first die go if || didn't attempt to pass it to open?
> >
> > if the reason for the failed open were the attempt to open "BAdFiLe",
> > the first die would execute and the program would exit bore it got to
> > the second.  But clearly that's not what's happening.  The first die
> > is getting slurped up by ||, which is presumably trying to pass it on
> > to open.  When that fails, the second die executes, exiting with $!.
> > at least that's what it looks like to me.
> >
> > So what's really happeneing here?
> 
> perldoc perlop
> 
> [snip]
> 
>       C-style Logical Or
> 
>       Binary "||" performs a short-circuit logical OR operation.  That is, if
>                              ^^^^^^^^^^^^^
>       the left operand is true, the right operand is not even evaluated.
>                                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
> 
> Perhaps this will help illustrate:
> 
> $ perl -MO=Deparse,-p -e'open IN, "BADFILE" || die "ERROR: $!"'
> open(IN, 'BADFILE');
> -e syntax OK
> $ perl -MO=Deparse,-p -e'open IN, "BADFILE" || die or die "ERROR: $!"'
> (open(IN, 'BADFILE') or die("ERROR: $!"));
> -e syntax OK
> 
> Because the string "BADFILE" is true, which is determined at compile time, it
> is as if "|| die" did not exist at all!
> 
> Of course if the file name is in a variable then it must be evaluated at run 
> time:
> 
> $ perl -MO=Deparse,-p -e'open IN, $ARGV[0] || die "ERROR: $!"' BADFILE
> open(IN, ($ARGV[0] || die("ERROR: $!")));
> -e syntax OK
> $ perl -MO=Deparse,-p -e'open IN, $ARGV[0] || die or die "ERROR: $!"' BADFILE
> (open(IN, ($ARGV[0] || die)) or die("ERROR: $!"));
> -e syntax OK
> 
> Where "|| die" will only be evaluated if the variable is false (undef, 0, '0'
> or '').
> 
> 

Logical "or" is also a short circuit operator, also from perlop:

    As more readable alternatives to "&&" and "||" when used for control
    flow, Perl provides "and" and "or" operators (see below). The
    short-circuit behavior is identical. The precedence of "and" and "or" is
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    much lower, however, so that you can safely use them after a list
    operator without the need for parentheses:

In other words, the only differnce between "||" and "or" is
precedence.  And precedence is what creates this:

[EMAIL PROTECTED]:~> perl -MO=Deparse,-p -e'$i=<STDIN>;open IN, $i ||
die or die "ERROR: $!"'
($i = <STDIN>);
(open(IN, ($i || die)) or die("ERROR: $!"));
-e syntax OK

I will admit that my response to OP didn't take into account that, in
his particular case, the argument to open was a literal and "||" was
being evaluated at compile time.  But the fact remains that it is
simply and demonstrably not true that open will never attempt to open
die, or that the attempt to open die will call die and exit the
program.  And I would have to say that the case where || is evaluated
at runtime is by far the more common case.

Thank you, though for the lesson on the differences between compile
time and runtime execution (and the reminder about how useful Deparse
can be).

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to