in reply to Re^4: A question about method return values and error checking
in thread A question about method return values and error checking

my @names = $os->name( long => 1 ) or next;

That seems more like a strawman to me. I just haven't run into code where people do list assignments in boolean context when using a sub that just returns a single scalar. It is an unnatural construct.

While using a scalar-returning sub as part of some list is done very often.

So subs that never return more than 1 scalar should never use return;. Subs that clearly return a list should avoid using return undef; to indicate failure.

- tye        

Replies are listed 'Best First'.
Re^6: A question about method return values and error checking (unnatural)
by kennethk (Abbot) on Nov 05, 2015 at 21:10 UTC
    I concur that it's a poor example, but I'm a bit pressed to come up with a good example to demonstrate why it's good practice. Ultimately, I think we agree on what good form should probably be. Maybe if I mull it for a while, I can come up with a better justification.

    #11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.

      Decisions in interface design can sometimes have far-reaching consequences. The class of errors where values get injected, omitted or mixed up is typically more disruptive than a case of a missing value.

      To sum it up: where there's a method to fetch a single item, it is probably a good idea to return one item precisely.