Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: Looking for discussions of "block after or" syntax error

by Laurent_R (Parson)
on May 01, 2013 at 20:11 UTC ( #1031637=note: print w/ replies, xml ) Need Help??


in reply to Looking for discussions of "block after or" syntax error

A possibly more perlish way:

my $in = "somefile.txt"; open my $FH, "<", $in or print "cannot open file $in\n" and exit 3;

EDIT: I had not paid attention to your title when I wrote the above, I now see that your question is really related to the block construct after the or keyword. My answer above may thus be slightly off-topic. It still gives a workable solution to the immediate problem and probably a better syntax to open a file.


Comment on Re: Looking for discussions of "block after or" syntax error
Download Code
Re^2: Looking for discussions of "block after or" syntax error
by LanX (Canon) on May 01, 2013 at 20:26 UTC
    Maybe a matter of taste, but do you think the precedence rules of or and and are that obvious, that this code is immediately understood?

    Cheers Rolf

    ( addicted to the Perl Programming Language)

      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.

        > 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)

      Especially considering the Perlish idiom for this situation is die which inherently combines print and exit.

      As Occam said: Entia non sunt multiplicanda praeter necessitatem.

Re^2: Looking for discussions of "block after or" syntax error
by davido (Archbishop) on May 01, 2013 at 21:04 UTC

    print returns false on failure, in which case the 'and' would short-circuit and exit would never get called.

    Granted, it's pretty unusual for print to fail -- so rare that we rarely test for success in our prints, and usually don't think in terms of "this could fail", but when you absolutely positively want to exit, don't include a logical test on print's success.


    Dave

      Agreed. I did not think about that. And I usually don't do such a print anyway but rather a die statement (although, going to the end of this logic, what do you do if die fails? Do you 'die "i failed" or die "die failed"'?

      Usually, the only reason I want to know that opening a file failed if to get an immediate diagnostic of the reason for the program failure. If the print statement fails, well, I am likely to have more severe problems than just file opening.

        die doesn't have a useful return value (to my knowledge), and would never be placed on the lefthand side of a logical short circuit.


        Dave

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (6)
As of 2014-09-20 20:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (161 votes), past polls