Beefy Boxes and Bandwidth Generously Provided by pair Networks Joe
laziness, impatience, and hubris
 
PerlMonks  

Re^7: Unhappy returns

by ysth (Canon)
on Oct 10, 2005 at 14:10 UTC ( [id://498901]=note: print w/replies, xml ) Need Help??

This is an archived low-energy page for bots and other anonmyous visitors. Please sign up if you are a human and want to interact.


in reply to Re^6: Unhappy returns
in thread Unhappy returns

Then where does the return value come from? What expression is it?
And function calls return the value of the last expression evaluated.
I think it is possible to have a mostly consistent understanding based on that "rule", but it involves thinking of void-context expressions as having values. IMO it is a lot simpler (and completely instead of mostly consistent) to think of every statement as having a context and a return value. This issue with for loops is one place where the two models show their difference.

Replies are listed 'Best First'.
Re^8: Unhappy returns
by Aristotle (Chancellor) on Oct 10, 2005 at 15:22 UTC

    It isn't completely consistent.

    #!/usr/bin/perl use strict; use warnings; sub x { local$,=" "; print "$_[ 0 ]:", map( "[$_]", @_[ 1 .. $#_ ] ), +"\n" } x "for", sub { for( 5 .. 7 ) { 1 } }->(); x "while(5)", sub { while( 5 ) { 1; last } }->(); x "while(0)", sub { while( 0 ) { 1 } }->(); x "if(5)", sub { if( 5 ) { 1 } }->(); x "if(0)", sub { if( 0 ) { 1 } }->(); x "do{}while(0)", sub { do { 1 } while 0 }->(); x "do{}until(5)", sub { do { 0 } until 5 }->(); __END__ for: [] while(5): while(0): if(5): [1] if(0): [0] do{}while(0): [1] do{}until(5): [0]

    I admit the value returned from the function for surprised me. It returns an honest-to-goodness false (ie. an empty string) -- not even undef. (I checked.) Interesting.

    Also somewhat surprising, but not nearly as much, is that the functions with while return nothing at all.

    Because I knew about the behaviour of if – when the condition is the last thing evaluated, it is returned, while when the body runs, its last expression's value is returned.

    Which is exactly as expected by the model I described. Which is exactly what perlsub describes:

    The return value of a subroutine is the value of the last expression evaluated by that sub, or the empty list in the case of an empty sub.

    I guess Perl is simply inconsistent here.

    Update: per Re: "last expression" quiz, I added do{} pseudo-loop tests. They return a code reference. It’s probably safe to say that Perl simply has no rule, and that what you get back from a sub depends on implementation details of perl.

    Update: gack, I forgot to invoke the do subs using ->. Fixed, thanks to robin in Re^4: "last expression" quiz. No wonder I got back coderefs. The actual behaviour does not seem very consistent either, though.

    Makeshifts last the longest.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://498901]
help
Sections?
Information?
Find Nodes?
Leftovers?
    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.