in reply to Calling sub's: return true

First, I hope you mean that you're dividing your code into separate modules (using package) for a clean distinction among responsibilities. It is fundamental to creating maintainable software that you think critically about what parts of your code naturally belong together, and which ought to be split off into separate namespaces. The approach you're taking now feels easier to you as the author, because you're already familiar with the code. But the person coming along after you will have a terrible time making sense of the codebase.

Second, what you're seeing is the fact that the subroutine is returning the result of the last evaluated expression. Unless it's a number less than 1, undef, or the empty string, you had a success. So, the short answer to your question is that you can say return undef at the close of that sub if you really want it to be silent. However, there are three reasons (at least) that you do not want to do this:

  1. if you don't want that value, don't test for it and don't use it, just let it go.
  2. you may not want to test for the success of that sub, but someone else maintaining your code may wish to do so, and be very perplexed that this sub is returning a value that traditionally indicates failure.
  3. testing for the success of subroutines is a Good Thing, and you should get in the habit.

Replies are listed 'Best First'.
Re: Re: Calling sub's: return true
by FamousLongAgo (Friar) on Jan 11, 2003 at 03:05 UTC
    I agree with fever completely, but I would suggest using a plain return; instead of return undef; in those cases where you want to return an undefined value. The plain return returns undef when called in scalar context and an empty list when called in list context, which is very handy (for example, if you are calling the subroutine through map and want the bits that don't return a value to be ignored altogether).