Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: Re: Re: An obscure side effect?

by sgifford (Prior)
on Aug 04, 2003 at 06:21 UTC ( [id://280572]=note: print w/replies, xml ) Need Help??


in reply to Re: Re: An obscure side effect?
in thread An obscure side effect?

Perhaps there's some way to do this, but in general parentheses control the order of evaluation for math operators, not the order of execution for assignments or argument evaluation. For example, how would you parenthesize to make
$a[$i++] = $i++;
evaluate the left-hand side before the right, producing $a[0] = 1? Or to make
$j = $i++ - $i++;
evaluate the second $i before the first, producing 1?

How would you suggest parenthesizing in swab to guarantee the order of execution for this?

Replies are listed 'Best First'.
Re: Re: Re: Re: An obscure side effect?
by dws (Chancellor) on Aug 04, 2003 at 07:36 UTC
    [I]n general parentheses control the order of evaluation for math operators, not the order of execution for assignments...

    Unless assignment is just another way to build expressions. See perlop, and note that the assignment operator has associativity and precedence.

    Postincrement/postdecrement are tricky beasts. In C (at least in K&R C), their timing is left to the whim of the implementer. The swab() example involves a side-effect (substr() as lvalue), but the timing of the side-effect is well-defined.

      Postincrement and postdecrement also have associativity and precedence (although their associativity is "nonassoc", since they're unary), but their order of execution is still undefined when used multiple times in the same statement.

      My point is that I don't believe that associativity or precedence guarantee order of execution. For example, addition and multiplication have well-known associativity and precedence, but in the expression

      $a = b()*c() + d()
      I don't believe the order in which a, b, or c will be executed is guaranteed, since, ignoring side-effects, they can be evaluated in many different orders to produce correct results. At least, I haven't been able to find such a guarantee in Perl's documentation; if you can point me to one with some degree of precision I'd be happy to change my mind.

        Off the top of my head, the boolean operators are guaranteed to evaluate their arguments left-to-right, required for short-circuit behaviour. The comma operator is also guaranteed to evaluate arguments left-to-right. And the ternary is guaranteed to evaluate the condition before the other argument it picks, because the one it doesn't pick is guaranteed not to be evaluated.

        In other words (besides the comma), all operators which exhibit some form of short circuiting operator (and in Perl, are therefor also useful for flow control).

        Makeshifts last the longest.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (8)
As of 2024-04-23 07:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found