in reply to hash slice ? No thanks, I'm about to return...

...which is why I always do an explicit return. Some might consider it baby Perl to do so, but I don't rely on the value of the last expression in a subroutine for the return value of a sub. In your case, if you put a "return %return" at the end of your sub, everything's golden.

thor

Feel the white light, the light within
Be your own disciple, fan the sparks of will
For all of us waiting, your kingdom will come

  • Comment on Re: hash slice ? No thanks, I'm about to return...

Replies are listed 'Best First'.
Re^2: hash slice ? No thanks, I'm about to return...
by Tanktalus (Canon) on Feb 19, 2005 at 16:30 UTC

    Or just a "%return" - return is optional. Either way, make the return value explicit, rather than relying on a complex operation to do it, whether you make the fact you're returning explicit or not.

    (I did some benchmarks a while back which surprised me to find out that "return %hash" was actually slower than "%hash" and falling out the bottom of the function...)

      You surprised me with this, but:
      Rate return no ret return 4682554/s -- -30% no ret 6642457/s 42% --
      Is what i got from:
      use Benchmark qw( cmpthese ); my %hash = (1, 2, 4, 6, 43, 32, 5, 6, 6, 43, 3, 54, 5, 1); cmpthese(0, { 'return' => sub { return \%hash }, 'no ret' => sub { \%hash }, });
        Surprising but pointless. Even the "slow" explicit return runs at 4 million iterations per second. Don't waste your time trying to use this as a real optimization.

      Depends. I often implicitly return the result of an operation that would obviously be useless in void context, as in

      sub as_string { my $self = shift; join '-', $self->date; }

      or the like.

      Makeshifts last the longest.

Re^2: hash slice ? No thanks, I'm about to return...
by cbrandtbuffalo (Deacon) on Feb 19, 2005 at 18:18 UTC
    We actually have it as a coding standard. The explicit return is required. It's obvious to some, but the time spent by a beginner or intermeddiate programmer thinking about exactly what is being returned can offset the shortcut. We have quite a lot of perl code, so we often have beginner level people looking at stuff and implicit return values really slow them down.

    The performance penalty is interesting. I wasn't aware of that. Might add up if you had a sub that is called many times in your program.

      so we often have beginner level people looking at stuff and implicit return values really slow them down
      It would be faster to take the 15 minutes to teach each of them about Perl's very simple return policy ("last expression evaluated is always the return value") than to require every coder in the world to make up for their lack of training.

      Even if you could establish policy for newly written code at your organization, these coders will always be looking at code from the CPAN or from consultants that clearly are not going to put in all of your handholding.

      I don't want to suggest that coding standards are categorically silly, but don't try to make Perl look like another language in the standard. Lack of the "return" keyword is both idiomatic and commonplace.

      It's a bit like demanding that every sharp corner in the workplace be covered with Nerf foam, just because there are some people who walk around with their eyes shut. No, it'd be simpler to teach them to watch where they are going.

      -- Randal L. Schwartz, Perl hacker
      Be sure to read my standard disclaimer if this is a reply.

        I generally agree with your point, but when those coders are learning a bunch of other things at the same time (like why it's nicer to pass back a reference than an array or hash), it can be one more thing on their stack. I have some influence on the standards, so I try to pick and choose which Perl idioms I want to push for and which I let go.

        So, given your example, I guess we've decided that we'll go ahead and put the padding on the corners. At first, they'll run into the table, later they'll go around but hit the padded corners. Eventually, they'll navigate the office (or the code) nearly flawlessly, and even then the padding doesn't hurt them. There are a few drawbacks, but not many. And the additional advantage is we avoid some ambiguity.

        I think the cool thing is that Perl makes this sort of thing available, even if it is optional. Your padding example is apt because this sort of thing does pad the learning process and 'takes the edges off' the learning curve. These things can be invaluable when you have an office of people re-learning (from a mainframe environment or a non-coding background) or tackling other similar learning curves.

Re^2: hash slice ? No thanks, I'm about to return...
by dragonchild (Archbishop) on Feb 19, 2005 at 22:29 UTC
    I tend to agree with you, except for certain situations. I will get rid of the return when my function is a one-liner, along the lines of:
    sub foo { (+shift)->foo_bar( 'foo', @_ ) } sub bar { (+shift)->foo_bar( 'foo', @_ ) } sub foo_bar { my $self = shift; my ($type, @args) = @_; # Do stuff here } sub unique { my %x; @x{@_} = @_; values %x } sub stringify { join '-', $_[0]->date }

    If my function gets to more than 80 characters, including the subroutine name, and/or it isn't a drop-in replacement for where it's called, then I use more than one line, don't directly access @_, and use an explicit return.

    (I access @_ for aliasing-type stuff as well, but I'll do it here for brevity.)

    Being right, does not endow the right to be rude; politeness costs nothing.
    Being unknowing, is not the same as being stupid.
    Expressing a contrary opinion, whether to the individual or the group, is more often a sign of deeper thought than of cantankerous belligerence.
    Do not mistake your goals as the only goals; your opinion as the only opinion; your confidence as correctness. Saying you know better is not the same as explaining you know better.