Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re^2: Evaluation Order again.

by Tanktalus (Canon)
on May 30, 2016 at 19:11 UTC ( [id://1164526]=note: print w/replies, xml ) Need Help??


in reply to Re: Evaluation Order again.
in thread Evaluation Order again.

If I'm reading your statement correctly, and given a func that looks like this:

sub func { print $_, ' ' for @_; print "\n"; $_[2]; }
And given $i starting at, say, 3, what would you suppose the output to look like? The way I read your your ordering is that I should expect "3 4 6" (once we've added the missing $ to the second parameter). However, that's not what I get. I get "4 4 6" consistently across perls going back to 5.8.8 (which is as far back as I've got here). So either my interpretation of your statement is wrong or the documentation doesn't match the implementation. I'm not sure which.

Replies are listed 'Best First'.
Re^3: Evaluation Order again.
by ikegami (Patriarch) on May 30, 2016 at 19:22 UTC

    Your incorrect expectation has nothing to do with operand evaluation order; it's that you didn't take into account that Perl always passes by reference.

    The following demonstrates that the operand evaluation order used is the one that was documented.

    use Data::Alias qw( alias ); sub func { print $_, ' ' for @_; print "\n"; $_[2]; } { my $i = 3; func( $i, ++$i, $i+2 ); } { my $i = 3; local @_; alias push @_, $i; alias push @_, ++$i; alias push @_, $i+2; &func; }
    4 4 6 4 4 6

      Ah, of course. That distinction becomes important so rarely for me that it's easy to forget. So, to remove the referencing, and make it show up "as expected", I just add zero to each item, and I get what I expect:

      use strict; use warnings; sub func { print $_, ' ' for @_; print "\n"; $_[2]; } my $i = 3; my $rv = func(0+$i, 0+ ++$i, 0+$i+2);
      And now I get my "3 4 6" - and everything makes sense again. See? I knew I did something wrong ;)

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (6)
As of 2024-04-23 21:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found