in reply to [SOLVED] Return statement in subroutine(s) is necessary when not returning anything?

based on replies of stevieb and Anonymous cleared out that is better to implicitly use a return; statement to clearly show where your subroutine ends and also to avoid implicit return data.

If it's true that the trailing return; is better, why does practically noone use it?

  • Comment on Re: [SOLVED] Return statement in subroutine(s) is necessary when not returning anything?
  • Download Code

Replies are listed 'Best First'.
Re^2: [SOLVED] Return statement in subroutine(s) is necessary when not returning anything?
by Preceptor (Deacon) on Nov 03, 2015 at 14:37 UTC

    Well, whilst I hesitate to say it, there are a few places where perl lets people get away with shoddy coding practices...

      I disagree. A large part of my affection for Perl is its terseness. It doesn't force you to do things that are obvious enough for the compiler so obvious enough for a good dev. Verbosity can be an impediment to communication; especially in technical matters. Back to the OP's example–

      sub ohai { print "OHAI"; return; }

      That is technically "wrong." It prevents you from being able to use the sub like this do_something() if ohai(); It makes the sub less useful (Update: and harder to test). To fix that, you would report on success. That RFC:SHOULD look like:

      sub ohai { return print "ohai"; }

      How good a practice does that appear to be? Looks foolish to me. I much prefer the implicit return (when it's sensible, it's not always) than the explicit being recommended.

      I didn't say a few people don't use the practice. In fact, I don't think I've come across any CPAN code that uses the practice. Claims of shoddiness seem to be *greatly* exaggerated.

        Indeed.

        But my point was just because a lot of people do something, really doesn't have any bearing on it being a good - or bad - practice. Argumentum ad populum

        Especially in perl, which lets you get away with all sorts of horrific nastiness for extended periods of time.

Re^2: [SOLVED] Return statement in subroutine(s) is necessary when not returning anything?
by Anonymous Monk on Nov 06, 2015 at 23:52 UTC