http://www.perlmonks.org?node_id=1031650


in reply to Re^2: Looking for discussions of "block after or" syntax error
in thread Looking for discussions of "block after or" syntax error

Yes, I agree with you, Rolf, it is true that there might be a problem of understanding, but the construct is so practical that that I think it is worth the effort. Besides, it is easy to force precedence with parens:

open my $FH, "<", $in or (print "cannot open file $in\n" and exit 3);

so that, in fact, replacing curlies by parens in the OP's code might really be all that is needed to make it work if I did not miss anything (I haven't tried it in real context).

Besides, as far as I understand it (but I may be wrong on that, I haven't investigated very far), it seems to me that it is not really a issue of precedence between the operators, but a simpler question of left to right Boolean evaluation with the right part of the expression simply not being evaluated if not needed (i.e. if the left part is true with an "or" operator, or if the left part is false with an "and" operator).

Consider these two one-liners (I have an xml.pl file in my current directory, but no xml2.pl):

$ perl -e 'open my $FH, "<", "xml.pl" and print "Opened!" or print "ca +nnot open file\n" and print 3' Opened! $ perl -e 'open my $FH, "<", "xml2.pl" and print "Opened!" or print "c +annot open file\n" and print 3' cannot open file 3

This seems to work fine in both cases. I am of course not suggesting that such bizarre code should really be used for anything useful, but I am just pointing out that this would not work, I think, if we were to rely on the fact that the "or" operator has a lower precedence than the "and" operator.

Replies are listed 'Best First'.
Re^4: Looking for discussions of "block after or" syntax error
by LanX (Saint) on May 01, 2013 at 21:28 UTC

    > Besides, as far as I understand it (but I may be wrong on that, I haven't investigated very far), it seems to me that it is not really a issue of precedence between the operators, but a simpler question of left to right

    Well, you are not "knowing" you are "guessing"...

    ... and that's not a good base for designing code.

    What's worse, I also guessed that and and or have the same precedence ...

    ... but now take a look what B::Deparse says:

    DB<102> dp $a and $b or $c { $c unless $a and $b; } DB<103> dp $a or $b and $c { $b and $c unless $a; }

    too many surprises for my taste. =)

    edit

    from perlop

    Operator Precedence and Associativity ... nonassoc list operators (rightward) right not left and left or xor

    Cheers Rolf

    ( addicted to the Perl Programming Language)

      Well, you are not "knowing" you are "guessing"... ... and that's not a good base for designing code.

      This is unfair, Rolf. I am certainly not claiming to be an expert on the subject, this is why I am saying I may be wrong. But, please, don't say that I am just guessing, I have done a quite a little bit more work than just guessing. I am just admitting that I might be completely wrong, if such is the case, please show me why, and I will be happy to recognize it.

      What's worse, I also guessed that and and or have the same precedence ...

      Well, I knew they did not have the same precedence. But my point is that this is probably essentially irrelevant.

      ... but now take a look what B::Deparse says: ...

      $a and $b or $c

      What I am saying: true, i.e. $a and $b, if they are both true, otherwise, $c. Exactly equivalent to $c unless $a and $b;

      $a or $b and $c

      True if $a is true. Otherwise, true only if $b and $c are both true. Again, it seems to me that this is exactly equivalent to $b and $c unless $a;

      In brief, the two deparse examples that you have shown confirm exactly and precisely by left-to-right Boolean interpretation with short-circuit that I made.

      Again, I am being very careful on this, I may be completely wrong. But, *please*, don't use this uncertainty to claim that I am just guessing.

        ehm ... sorry if you conceive this as an unfair attack.

        Cheers Rolf

        ( addicted to the Perl Programming Language)