I'm sure the other monks would have some good ideas on this, with readability and consistency being my most important objectives.
($formatted_users,$error) = &format_output($users); defined($error) && &fatal_error($error); #logging/feedback to user
The disadvantages of this approach are:
$formatted_users = &format_output(&get_users($fh));
With regard to the final point, I've always been a huge fan of the way perl determines a truth/falsehood for each expression, since it allows much more elegant referencing to errors, eg:
($formatted_users = &format_output(&get_users($fh))) || &fatal_error("something went wrong formatting the users");
In this example, &format_output would return undef if there was any error, making the truth value of the assignment false, and causing the error code to be executed.
The disadvantage of this approach is the granularity of the error message. The message isn't generated inside the subroutine, so it doesn't tell you why something went wrong.
So, the next idea I had been tossing around was to use a global error value, sort of a home-made $! (error indicators). Then I could do something like this:
($formatted_users = &format_output(&get_users($fh))) || &fatal_error($ERR);
which gives the user lots more information, and keeps the code compact and readable. $ERR might even be a hashref with fields like $ERR->{severity} and $ERR->{send_email}. It might be imported from a package, if the sub belonged to a foreign package, eg
&user_delete || &fatal_error($user::ERR->{message})
Does anyone have any criticism for this last idea? What about other ideas? What about putting my error inside $ENV? Perhaps using signals?
In reply to style for returning errors from subroutines by Boldra
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |