in reply to Re: Surprising Precedence/Context
in thread Surprising Precedence/Context

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

Replies are listed 'Best First'.
Re^3: Surprising Precedence/Context ("and" is "if")
by LanX (Saint) on Jun 02, 2017 at 20:11 UTC
    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!