Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: Short Circuit Operator and Hash Assignment

by hardburn (Abbot)
on Mar 17, 2004 at 16:29 UTC ( [id://337390]=note: print w/replies, xml ) Need Help??


in reply to Short Circuit Operator and Hash Assignment

After running that line through B::Deparse:

$idx{$val} ||= push(@num, $val);

The 1 && part is being optimized out of the statement.

Note, too, that the exact operator precedance in Perl5 is poorly defined, and there are many edge cases that don't work out quite as you'd expect.

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

Note: All code is untested, unless otherwise stated

Replies are listed 'Best First'.
Re: Re: Short Circuit Operator and Hash Assignment
by TimToady (Parson) on Mar 17, 2004 at 18:59 UTC
    Note, too, that the exact operator precedance in Perl5 is poorly defined, and there are many edge cases that don't work out quite as you'd expect.
    Gah, you make it sound as though Perl rolls dice to determine operator precedence. The exact operator precedence in Perl is quite well-defined, and there are no random number generators involved. But people fail to understand it in two different ways. First, they simply don't bother to learn the precedence table, which was the problem in this case (and is the usual problem). I think people get lazy because operator precedence usually DWIMs, so they figure it will always DWIM, and then are surprised when it doesn't.

    The other way that people fail to understand operator precedence is that they don't know when it doesn't apply. It's important to understand that a yacc grammar uses operator precedence only to break ties between ambiguous rules in the grammar. If the grammar specifies that something can only be parsed in a particular way, operator precedence never comes into it. You may think that the $ on the front of a variable is a unary operator, but it's not, because what comes after it is not allowed to be a general expression. The grammar restricts the following item to be an identifier, a simple scalar variable, or a block, and these are all recognized by the grammar without recourse to operator precedence. If you try to treat $ as a unary operator, you will certainly be surprised when it doesn't work that way. But it's not an "edge case" of operator precedence. It's simply getting nowhere near the operator precedence table in the first place.

      I was thinking of "edge cases" where there is a tie in the precedence table, such as in Auto-increment frenzy. Which seems to be a recurring theme on the Fun With Perl mailing list.

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

      Note: All code is untested, unless otherwise stated

        Then it would seem that you either didn't read or didn't understand the thread in question, since in Re: Auto-increment frenzy Abigail-II correctly points out that the order of evaluation in that case has nothing whatsoever to do with operator precedence. Operator precedence has absolutely nothing to say about the order in which the two terms to a binary operator are evaluated. Terms ain't operators. But even if you happen to know or guess that the left term is usually evaluated before the right one, you still can't assume that the post-increment happens right when the old value is returned. Legally it can happen any time up until the next statement. But again, this is a run-time issue, not a parsing issue. Just because operator precedence places some constraints on the order of evaluation doesn't mean that we should use the former term to mean the latter. Okay, maybe you did understand the thread--I can see that maybe you're just using the term differently, but if so, what you have to understand is that "operator precedence" is a technical term in computer science, and to use it generically like that will make computer scientists think you're spouting nonsense.

        So yes, there are certainly "edge cases" in the order of evaluation when it comes to side effects. Every imperative computer language has them. (Which is one reason people fall in love with functional languages.) So in the documentation for an imperative language, you'll almost always find something that says something like: "Relying on the order of evaluation for side effects is erroneous. Just don't do that. Use a separate statement if you want to guarantee order of evaluation."

Re: Re: Short Circuit Operator and Hash Assignment
by dada (Chaplain) on Mar 17, 2004 at 17:25 UTC
    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

      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

      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

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (4)
As of 2024-04-20 15:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found