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@/

In reply to surprising backwards evaluation order by fizbin

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.