How will that work with a for modifier? I've occasionally wanted

$t += $_ for @list;

to return the last value of $t. Okay, okay, I know in Perl 6 there will be other ways to do similar stuff, in particular functional-language like ones that will avoid the need for a temporary variable altogether, but I still wonder how this kind of situation will be handled...

Hmm, well, in my copy of Perl that doesn't return $t, but the notional undef off the end of the list, which again points out the same semantic problem of "what's the last expression, anyway?"

But yes, in Perl 6 you'd just write:

[+] @list
to sum up the values. As for what your snippet would actually return in Perl 6, with the new rules I'd expect it to return the list of intermediate values, much as if you'd used a "triangular reduce":
[\+] @list
But generally if you're in that situation you'd be using the temporary value later anyway, which tends to put the for loop into void context, so it knows it can just throw away all those intermediate values. And arguably if you're using something as subtle as returning the last value of a for loop, you should have at least a return $t after it for clarity, which would also put the loop into void context. I think this approach will turn out more intuitive in the long run, though of course over the short term it runs a bit counter to Perl 5 intuitions. These days when we try to balance the needs of the present with the needs of the future, we tend to put a thumb on the scale in favor of the future. :-)

In reply to Re^5: Value of a statement with modifier (unless) by TimToady
in thread Value of a statement with modifier by Jenda

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.