Beefy Boxes and Bandwidth Generously Provided by pair Networks vroom
Just another Perl shrine
 
PerlMonks  

Re: Re: Short Circuit Operator and Hash Assignment

by dada (Chaplain)
on Mar 17, 2004 at 12:25 UTC ( [id://337428]=note: print w/replies, xml ) Need Help??

This is an archived low-energy page for bots and other anonmyous visitors. Please sign up if you are a human and want to interact.


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: Re: Re: Short Circuit Operator and Hash Assignment
by hardburn (Abbot) on Mar 17, 2004 at 12: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

Re: Short Circuit Operator and Hash Assignment
by jonadab (Parson) on Mar 17, 2004 at 13: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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://337428]
help
Sections?
Information?
Find Nodes?
Leftovers?
    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.