Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: "last expression" quiz

by QM (Parson)
on Oct 21, 2005 at 14:03 UTC ( [id://501999]=note: print w/replies, xml ) Need Help??


in reply to "last expression" quiz

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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://501999]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (5)
As of 2024-04-19 15:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found