On page 259 of HOP, MJD is talking of a linked list whose nodes are only constructed when the subroutine, tail is evaluated. The code for tail is similar to the following:
sub tail { my $list = shift; if ( ref( $list->[1] eq 'CODE' ) { return $s->[1]->() ); else { return $s->[1] } }
The code to construct the list, a list of the numbers from m to n, is:
sub upto { my ($m, $n) = @_; return if $m > $n; node( $m, promise { upto($m+1, $n) } ); }
where
sub promise (&) { $_[0] }
and:
sub node { my ($head, $tail) = @_; return [$head, $tail]; }
According to MJD, the tail() function sees the promise and invokes the anonymous promise function, which in turn invokes upto($m+1, $n). When I try this code out on Fedora 9 with perl-5.10.0, I get the error:
'Can't call method "promise" on an undefined value.'
It is apparently parsing the block argument to promise as an indirect method call on the non-existent object { upto($m+1, $n) } If I wrap the block argument to promise in parentheses, I get the errors:
Odd number of elements in anonymous hash at stream.pl line 19. Use of uninitialized value in anonymous hash ({}) at stream.pl line 19 +.
If I then disambiguate the thing in braces as a block with { return upto($m+1, $n) }, undef is returned. And if I replace the braces in the original line with parentheses, like
node( $m, promise ( upto($m+1, $n) ) );
the function is eagerly evaluated, not lazily. So, what do I tell MJD? I think this probably worked before. Is this a problem with newer perls?

In reply to lazy evaluation of sub arg by drbean

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.