Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

order of evaluation of operands

by jlistf (Monk)
on Jul 21, 2000 at 01:19 UTC ( [id://23490]=perlquestion: print w/replies, xml ) Need Help??

jlistf has asked for the wisdom of the Perl Monks concerning the following question:

this is directed to people who can actually understand the source code.
does Perl guarantee a certain order of operations of operands. for example, if i type:
$var = &sub1 + &sub2;
do i have a guarantee that sub1 will be called before sub2?

thanks

Replies are listed 'Best First'.
Re: order of evaluation of operands
by eduardo (Curate) on Jul 21, 2000 at 02:29 UTC
    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.
(jcwren) RE: order of evaluation of operands
by jcwren (Prior) on Jul 21, 2000 at 01:23 UTC
    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
Re: order of evaluation of operands
by japhy (Canon) on Jul 21, 2000 at 19:04 UTC
    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.)
      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.
        Check the perlop documentation.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (7)
As of 2024-03-28 20:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found