in reply to style for returning errors from subroutines

The Exceptional Philosophy: the function returns the correct value, period. If it can't, it doesn't return at all—it throws an exception.

In your example, get_users() will return a list of users, and an empty list means there are none. If there is an error of some kind, the caller doesn't need to know just what and how to check. Instead, the system is pro-active and jumps to the error handler. In Perl that's done with die/eval.

—John

  • Comment on Re: style for returning errors from subroutines

Replies are listed 'Best First'.
Re: Re: style for returning errors from subroutines
by Boldra (Curate) on Jun 15, 2001 at 15:22 UTC
    Actually, the way my examples would have worked (and it wasn't explicitly spelt out), was that they returned a reference to a list of users. Since assigning a reference to a empty list is true I wouldn't have gotten a false negative by returning an empty list. eg:
    @foo = (); $bar = \@foo; ($baz = $bar) or warn("assigning a (reference to an empty list) to another scalar is + false");
    Of course, what you pointed out is a common gotcha for newbies, and one that I've fallen for a couple of times. Not this time though :)

    As I said in reply to bluto's suggestion, I do like the "Exceptional Philosophy", but so far the neatest implementation seems to be signal handling (IMHO).

    - Boldra