in the seventies, the programming language theory people were really big on non-determinism. when it comes to things like short circuit evaluation and stuff like that, they liked to say: "based on compiler implementation, your answer is guaranteed to be non-deterministic..." which meant: "there ain't no way to know..." That lesson has stuck with me, and whenever I find myself in a situation that might lead itself to non-determinisim (in other words, any situation with a vauge answer) i make sure that I can validate the order of operations. In your case: my $val1=&sub1;
my $val2=&sub2;
my $total=$val1+$val2;
i don't know if perl guarantees an order of operations, but do yourself a favor, and do a favor to whomever has to look at your code some day down the line, minimize non-determinism. | [reply] [Watch: Dir/Any] [d/l] |
While not a definitive answer, there is some help in perldoc that may be of use to you:
perldoc perlop
has a whole section devoted to operator precedence. For your particular example, the answer is 'yes'. But it can get a little funky, and not do what you might intuitively expect, in other cases.
--Chris
e-mail jcwren | [reply] [Watch: Dir/Any] [d/l] |
I can answer this with much zeal and vigor. (Or I just wanted to use those two words.)
Use the B::Terse module, I made the following check:
jeffp@friday [10:44am] parsers #264> perl -MO=Terse
sub a { print 1 }
sub b { print 2 }
$c = a() + b();
- syntax OK
LISTOP (0x13b940) pp_leave
OP (0x130ef8) pp_enter
COP (0x13b970) pp_nextstate
BINOP (0x142ce0) pp_sassign
BINOP (0x142d00) pp_add [3]
UNOP (0x142c20) pp_entersub [1]
UNOP (0x13b9d0) pp_null [141]
OP (0x130f58) pp_pushmark
UNOP (0x142c40) pp_null [17]
GVOP (0x142c60) pp_gv GV (0x12dbac) *a
UNOP (0x142d20) pp_entersub [2]
UNOP (0x13ba90) pp_null [141]
OP (0x130f70) pp_pushmark
UNOP (0x142d40) pp_null [17]
GVOP (0x142d60) pp_gv GV (0x14bbc4) *b
UNOP (0x12ea00) pp_null [15]
GVOP (0xbed00) pp_gvsv GV (0xc6304) *c
Yes, Perl will call a() before it calls b(). That's why you can safely say: ($a,$b) = (shift, shift) to get the first two values from @_, IN THE RIGHT ORDER. (But you'd use splice(), really.) | [reply] [Watch: Dir/Any] [d/l] |
That's why you can safely say: ($a,$b) = (shift, shift) to get the first two values from @_, IN THE RIGHT ORDER.
the comma operator is one that definitely evaluates left to right. it does this even in C/C++ (in addition to &&, and, ||, or and ?:). i know that those evaluate left to right. but are all operators guaranteed to evaluate left to right. just because your computer does it that way does not necesarily mean that all will. granted Perl has less computer/software dependant items like C and C++, but i'd like to know for sure what the case is. preferably some mention in documentation or an answer from someone who's read the source code or a link to a tutorial or something.
| [reply] [Watch: Dir/Any] [d/l] [select] |
Check the perlop documentation.
| [reply] [Watch: Dir/Any] |