http://qs321.pair.com?node_id=337428


in reply to Re: Short Circuit Operator and Hash Assignment
in thread Short Circuit Operator and Hash Assignment

The 1 && part is being optimized out of the statement.
this seems a rather dangerous and naive optimization to me, given that also this:
$x = 1 && $y = 2; $x = 0 || $y = 2;
gets "optimized" as:
$x = $y = 2; $x = $y = 2;
perhaps the p5p people should do something about it.

cheers,
Aldo

King of Laziness, Wizard of Impatience, Lord of Hubris

Replies are listed 'Best First'.
Re: Short Circuit Operator and Hash Assignment
by jonadab (Parson) on Mar 17, 2004 at 18:24 UTC
    this seems a rather dangerous and naive optimization to me

    The optimization is a red herring; it's not changing the result in any way. If the left operand of the && operator is true in boolean context, then it returns the value of its right operand, whatever that may be. Expecting it to return the left operand is wrong. (What you really were expecting is a different parse, based on different operator precedence. (See my other post downthread.))


    ;$;=sub{$/};@;=map{my($a,$b)=($_,$;);$;=sub{$a.$b->()}} split//,".rekcah lreP rehtona tsuJ";$\=$;[-1]->();print
Re: Re: Re: Short Circuit Operator and Hash Assignment
by hardburn (Abbot) on Mar 17, 2004 at 17:33 UTC

    You're hitting an edge case. The optimization had a different situation in mind:

    if( 1 && foo() ) { . . . } elsif( 0 || bar() ) { . . . }

    This optimization is hardly unique to Perl. I believe both C and Java compilers will optimize away constants in exactly the same way.

    ----
    : () { :|:& };:

    Note: All code is untested, unless otherwise stated