Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: Re: Want a Hashref. Getting a List in Scalar Context.

by chip (Curate)
on May 04, 2004 at 20:37 UTC ( #350549=note: print w/ replies, xml ) Need Help??


in reply to Re: Want a Hashref. Getting a List in Scalar Context.
in thread Want a Hashref. Getting a List in Scalar Context.

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.

    -- Chip Salzenberg, Free-Floating Agent of Chaos


Comment on Re: Re: Want a Hashref. Getting a List in Scalar Context.
Download Code
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.
      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

        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.
        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.

        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

        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.

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?

      This form of '+' doesn't produce an opcode. Its there purely for the parser's benefit.

      map +( ... ), ...
Re: Re: Re: Want a Hashref. Getting a List in Scalar Context.
by dave_the_m (Parson) 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.

      So it does; I never noticed that before.

      I guess we're back to the "less typing good" vs. "more redundancy good" argument, already in progress.

          -- Chip Salzenberg, Free-Floating Agent of Chaos

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (5)
As of 2014-11-22 16:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My preferred Perl binaries come from:














    Results (123 votes), past polls