Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (4)
As of 2024-04-20 04:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found