Hi Uri (and all), On Fri, 14 Mar 2014 13:40:02 -0400 Uri Guttman <u...@stemsystems.com> wrote:
> On 03/14/2014 01:03 PM, Ralph Grove wrote: > > I'm trying to determine how Perl evaluates operands for expressions. I > > expected the following code to output 3, 4, and 5, as it would if > > executed as a C++ or Java program. The actual output that I get > > (v5.16.2), however, is 4, 4, and 5. This leads me to believe that > > operand evaluation is either non-deterministic and compiler-dependent, > > or it's simply broken. > > > > The Perl documentation discusses operator evaluation, but doesn't > > directly address operand evaluation order. Does anyone know of a good > > resource for this information? > > perl doesn't guarantee any evaluation order. when doing side effects > like this don't expect any particular result unless you control things > explicitly. > > lists of args passed to subs and ops are evaluated left to right. that > can be useful if you really want to control evaluation order. > > > > > Thanks, > > Ralph > > > > > > > > sub doit { > > return ++$g ; > > } > > > > $g = 0; > > my $sum = $g + doit() + doit(); > > use List::Util qw( sum ) ; > > my $sum = sum( $g, doit(), doit() ) ; > > that will do what you think it should do. > > since + officially doesn't care about left to right ordering, perl does > what it thinks will be efficient. it can't tell doit() is modifying $g. > > but even better DON'T code that way!! even if order was specified that > is a very dangerous action at a distance piece of code. > > uri Thank you for replying before me, with what I had to say (that the code was too clever for its own good) and more. Like the old aphorism goes: “Always write your code, as if the maintenance is a violent psychopath who knows where you live.”. Avoid overly clever code, that may try to do more in less code. Shabbath Shalom and happy upcoming https://en.wikipedia.org/wiki/Purim everone (or just have a great weekend). Regards, Shlomi Fish -- ----------------------------------------------------------------- Shlomi Fish http://www.shlomifish.org/ My Public Domain Photos - http://www.flickr.com/photos/shlomif/ Whitespace in Python is not a problem: just lay out all the whitespace first, then add the code around it. — sizz on Freenode’s #perl Please reply to list if it's a mailing list post - http://shlom.in/reply . -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/