Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re^3: "last expression" quiz

by sauoq (Abbot)
on Oct 20, 2005 at 04:44 UTC ( [id://501537]=note: print w/replies, xml ) Need Help??


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

But statements (things like if, for, while) don't have values and return pretty much garbage?

Well, if works in that it returns the result of the last expression evaluated. It seems that's what looping constructs should do too. Wouldn't it make sense for this sub x { $_ for 10 } to return 10?

-sauoq
"My two cents aren't worth a dime.";

Replies are listed 'Best First'.
Re^4: "last expression" quiz
by Anonymous Monk on Oct 20, 2005 at 14:33 UTC
    Well, if works in that it returns the result of the last expression evaluated. It seems that's what looping constructs should do too. Wouldn't it make sense for this sub x { $_ for 10 } to return 10?
    Not really. If you're asking my opinion I'd say it should be an error if the sub is called in anything other than a void context. Note the following are also compilation errors...
    my $what = (if(1){print "true"}else{print "false"}); sub foo { return (for (1..3) { print $_ }); }
    You're not arguing that they shouldn't be, are you?
      You're not arguing that they shouldn't be, are you?

      No, I'm certainly not. I'm perfectly happy that those are errors. I don't know if it follows that sub x {$_ for 10} should be an error but I can see the argument for it. I see usefulness on both sides. Causing an error would be useful in preventing some god awful code while returning the result of the last expression evaluated would be useful in some god awful code. Usually, when there is a choice like that, Perl supports the latter but prints a warning under -w ... :-)

      -sauoq
      "My two cents aren't worth a dime.";
      
        Usually, when there is a choice like that, Perl supports the latter but prints a warning under -w
        I'll second that notion. The interpreter should be fixed to implement this idea and the documentation should be changed to something like...
        If the last thing (Note 1) in a subroutine is an expression, then the return value of the subroutine is the value of that expression. Otherwise if the last thing in a subroutine is a statement (if, for, while, etc.) and the subroutine is called in non-void context, then issue a warning. Alternatively, a return statement may be used to exit the subroutine, optionally specifying the returned value, which will be evaluated in the appropriate context (list, scalar, or void) depending on the context of the subroutine call...
        Note 1: We should another word besides "thing" but I'm not sure what that is.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (6)
As of 2024-03-28 20:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found