Re: Re: Re: Want a Hashref. Getting a List in Scalar Context.
by diotalevi (Canon) on May 04, 2004 at 20:46 UTC
|
All things being equal, it's good to use return; it's an extra opcode that does nothing when it's at the end of the function but it adds to the readability. | [reply] |
|
Additional redundancy may improve readability for newbies, but that way lay the elimination of all implicit features, and then you've got Python.
"Final expression is return value" is an easy and basic meme for Perl, going all the way back to Perl 1. The unnecessary use of return deserves, not recommendation, but quiet disdain.
-- Chip Salzenberg, Free-Floating Agent of Chaos
| [reply] [d/l] |
|
Adding an explicit return is just like adding "unneeded" parentheses: a way of expressing things more explicitly (and maybe more clearly) if the author wants. Let people write with their own style; I see no need for disdain or for fear that explicit returns will be mandatory in the future. I agree that freedom should remain one of the attractive features of Perl; people who want to have only one way of doing things have other languages to choose from.
| [reply] |
|
I dunno, I'm not a newbie and I like seeing it. Its strange to write return() in some places but not others. I suppose I could think of it like writing scalar() some places but I haven't.
| [reply] |
|
|
I think your disdain may be misplaced. Whenever writing perl code it is always important to consider the audience for the code you are writing; the compiler is one member (and a very important member) of that audience, but so are you as the author, and often so are other people that may need to maintain it or desire to understand it in the future.
I frequently use return at the end of a function or method. I frequently omit return at the end of a function or method. Which I choose to do in any particular situation is primarily down to whether I feel the return adds clarity.
It is rare that I write code that is so resource-critical an extra opcode would make a difference; when I do, of course, I don't use return.
Hugo
| [reply] [d/l] [select] |
|
Actually, this "Final expression is return value"
is IMO a mistake of perl. The final expression should
only mean a return value iff it's not followed by a semicolon.
That can only be done in free-form languages (where
a newline doesn't mean end of the command,
like perl or C but unlike awk or ruby or python).
That way, you wouldn't have to add return (or undef) statements
just to make a function void.
Yes, there's already one such language: Mathematica treats
semicolon as an operator that just returns its second argument
(like scalar comma in perl, comma operator in C,
begin in scheme).
It's a function like any operator in Mathematica
(well, any but one, @@@),
it's called CompoundExpression.
When it is used without second argument (that is, when you
just put a semicolon after an expression) the second
argument is implicitly Null.
Update: fixed a typo.
| [reply] [d/l] |
|
|
|
Re: Re: Re: Want a Hashref. Getting a List in Scalar Context.
by dave_the_m (Monsignor) on May 04, 2004 at 22:45 UTC
|
All things being equal, it's good to avoid return; it's an extra opcode that does nothing when it's at the end of the function.
On the other hand it's executed instead of the leavesub op, so its just as efficient. | [reply] |
|
| [reply] |
Re: Re: Re: Want a Hashref. Getting a List in Scalar Context.
by cebrown (Pilgrim) on May 04, 2004 at 21:16 UTC
|
But doesn't "+" become an opcode too? If so, why not err on the side of maintainability? | [reply] |
|
| [reply] [d/l] |