in reply to Re: $i=$i++
in thread $i=$i++

If it were evaluated right to left, then the answer would negative four

No, it's a per-operator thing, as + and - are different operators, and their precedence defines they should be evaluated left-to-right. This is done by implicit parentheses:

2;0 juerd@ouranos:~$ perl -MO=Deparse,-p -e'sub one { 1 } sub two { 2 +} sub three { 3 } print one() - three() + two()' sub one { 1; } sub two { 2; } sub three { 3; } print(((one() - three()) + two()));
(I had to use these subs, because Perl optimizes 1-2+3 to a literal 0 :)

So we have these calculation:
  1. 1 - 3 = -2
  2. answer ( = -2 ) + 2 = 0
But which are evaluated first? the 1 or the 3? The -2 or the 2? It becomes clear when you test the operators one by one (or so I thought):
2;0 juerd@ouranos:~$ perl -le'sub foo { print "foo"; return 1 } sub ba +r { print "bar"; return 2 } print foo + bar' bar foo 1 2;0 juerd@ouranos:~$ perl -le'sub foo { print "foo"; return 1 } sub ba +r { print "bar"; return 2 } print foo && bar' foo bar 2
So I decided to test a lot of operator this way, but found out that everything was evaluated left-to-right, but not if I left out the parens. Why did my first test print bar before foo then? Because I didn't print foo() + bar() but did print foo( +bar() ).

So that still doesn't solve our mistery...

- Yes, I reinvent wheels.
- Spam: Visit eurotraQ.