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:
(I had to use these subs, because Perl optimizes 1-2+3 to a literal 0 :)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()));
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() ).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
- Yes, I reinvent wheels.
- Spam: Visit eurotraQ.
In reply to Re: Re: $i=$i++
by Juerd
in thread $i=$i++
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |