in reply to Surprising Precedence/Context

As a side note: Perl comes with with a second set of logical operators for and , or and not', with much higher  precedence  ie '&& , || and ! (compare perlop#Operator-Precedence-and-Associativity and perlop#Logical-Not ff)

You wouldn't need parens then.

One reason and and or are so weak is to allow their use in flow control (in combination with short circuiting)

Cheers Rolf
(addicted to the Perl Programming Language and ☆☆☆☆ :)
Je suis Charlie!

Replies are listed 'Best First'.
Re^2: Surprising Precedence/Context
by williams (Beadle) on Jun 02, 2017 at 19:51 UTC

    That must explain this too,

    #!/usr/bin/perl use strict; use warnings; sub returnsTrueButExpectedFalse {return 1 and 0} die if 1 and 0; #does not die, but die if returnsTrueButExpectedFalse(); #does die here, surprisingly

    B::Deparse shows the return statement as this,

    (return(1) and 0);

    which isn't quite enough to say the return(1) happens before the 0 is evaluated. That must be what's happening though, from your reminder about flow control and and.

    Thanks for the tip about B::Deparse.

    Jim

      like AnoMonk explained, use the -p switch with B::Deparse to see more parens

      D:\Users\lanx>perl -MO=Deparse,-p -e"sub { return 1 and 0 }" sub { ((return 1) and 0); } ; -e syntax OK

      Please note that -x7 or higher will also show the equivalence between if and and (albeit in the opposite direction)

      D:\Users\lanx>perl -MO=Deparse,-p,-x7 -e"sub { 0 if return 1 }" sub { ((return 1) and 0); } ; -e syntax OK

      Cheers Rolf
      (addicted to the Perl Programming Language and ☆☆☆☆ :)
      Je suis Charlie!