in reply to style for returning errors from subroutines

How about return 0 for success and non-zero for an error with that non-zero number perhaps being an error code?

my $rc = doThis(); if ( $rc ) { error_routine($rc); # error_routine handles messaging for various e +rrors }

Replies are listed 'Best First'.
Re: Re: style for returning errors from subroutines
by Boldra (Curate) on Jun 14, 2001 at 18:58 UTC
    Here's a few reasons I don't like this:
    • I can't return anything but errors (For one thing, I like to assign a meaningful variable name to the output to help explain the purpose of the subroutine)
    • The truth value of a subroutine is counter intuitive (The opposite of perl's built-in functions)
    • I still have the unreadability of mixing lots of technical error gunk with procedural code.
    Thanks for the suggestion anyway :)
Re: Re: style for returning errors from subroutines
by bwana147 (Pilgrim) on Jun 14, 2001 at 19:25 UTC

    Well, I voted you up, for your entry adds some food for thought, but I disagree. Returning 0 on success and an error code in case of failure, that's what the shell works. In a a shell script, you mostly run commands for what they give you on the standard output, which you can pipe into other commands. The return value is merely a side effect, that can only cary a number, and that is perfect for error reporting. To this effect, the truth in Shell is the opposite as in Perl: 0 is true, while everything else is false.

    If you want to do the same in Perl, you'd have to cram ! everywhere, which IMHO does not participate to the legibility of your code. And you deprive yourself of the ability of returning a wealth of data, scalars, lists, references from functions.

    --bwana147