http://qs321.pair.com?node_id=537549

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

So in the process of getting enlightened about a syntax misunderstanding, I ran across this oddity:

First, look at this code and ask yourself what you think it should produce:

sub ctx { print "$_[0] "; \&ctx; } ctx(1)->(2,ctx(3),ctx(4))->(5,ctx(6))->(7,ctx(8)); print "\n";

No peaking; take a guess first

That code produces

8 6 3 4 1 2 5 7

Now, I'm pretty sure we all agree that 7 has to be last, and obviously 2 must be printed later than both 3 and 4, and also 5 must be printed later than 6, but what about the rest?

It appears that this order is the logical result of perl evaluating &{func }(args ) by evaluating args first, (left to right), and then evaluating func.

This strikes me as an odd choice - it goes against the left-to-right evaluation order that perl follows elsewhere. (well, except for if and other control constructs at the end of a statement) Do any monks familiar with language implementation in general have any insight into why things are done that way?

Note that an interesting consequence of this is that the following both die with the message "TWO":

perl -e '&{die("ONE")}(die("TWO"))' perl -e 'die("ONE")->(die("TWO"))'
--
@/=map{[/./g]}qw/.h_nJ Xapou cets krht ele_ r_ra/; map{y/X_/\n /;print}map{pop@$_}@/for@/