Regarding the for loop issues (and this may apply to other loop controls as well), it seems that the loop must stop on a false boolean expression. If that's the last expression in the sub, then that should be returned.

Note that under the hood, all of the loop constructs can be mapped to a single generic construct (which I suspect is what actually happens). Suppose we have this:

sub x { for my $q (1..10) { print "$q\n"; } }
While you may think the last thing evaluated is print "$q\n", the for actually has to check that there are no more elements in the list, perhaps making the above code equivalent to:
sub x { my @_forlist = (1..10); if (@_forlist) { do { my $q = pop @_forlist; print "$q\n"; } until ( not @_forlist ); } }
So the last expression evaluated is not @_forlist. Which is not explicit in the code!

Most of the errant behaviors documented in this thread seem to ignore this implicit expression. Perhaps the documentation could emphasize that "last expression" doesn't mean "last explicit expression".

BTW, does someone have the skills to investigate the optree for examples like these?

-QM
--
Quantum Mechanics: The dreams stuff is made of


In reply to Re: "last expression" quiz by QM
in thread "last expression" quiz by fxn

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.