Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re^2: "last expression" quiz

by robin (Chaplain)
on Oct 21, 2005 at 10:51 UTC ( #501957=note: print w/ replies, xml ) Need Help??


in reply to Re: "last expression" quiz
in thread "last expression" quiz

If the last thing that happened was an iteration of the do {} while $cond or do {} until $cond pseudo-loop, you get back a code reference (whether the condition turned out true of false).

That sounds really strange, but sadly I can't reproduce it. I tried

perl -e 'print sub {my $x=5; do {print "."} while $x--}->()'
but the sub just seems to return 0. Can you give an example? (Also, what version of Perl are you using?)


Comment on Re^2: "last expression" quiz
Download Code
Re^3: "last expression" quiz
by Aristotle (Chancellor) on Oct 23, 2005 at 14:43 UTC

    As the node you’re replying to already says: check Re^8: Unhappy returns for code.

    $ perl -v
    
    This is perl, v5.8.7 built for i486-linux

    Makeshifts last the longest.

      There is a bug in your code. You've missed out the ->() in the last two cases, so it's printing the sub rather than the result of calling the sub.

        Gack. Thanks for the catch. That was stupid. Fixed, node has been updated.

        The new behaviour is a tad more consistent, but still not consistent with the documentation. What gets returned is the last expression from the body of the do, while the last one evaluated is clearly the while/until condition. (Arguably, the real behaviour is more useful than the strictly doc-adherent one would be.)

        Makeshifts last the longest.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (10)
As of 2014-09-18 08:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (109 votes), past polls