I've been musing over how to report errors from a module I'm writing. Specifically, when to issue warnings and when to throw exceptions (
warn vs.
die, or better
Carp::carp vs.
Carp::croak). There are various ways of doing this:
- Everything is an exception - stick to the documented interface and give sane values, or we're outta here.
- Everything is a warning - Hey, we're easy man, you wanna try this, I'm cool, but I'm not playing along. Peace, brother.
- Mixing stuff - Hah, you thought this would be no problem: wrong, bozo. I'm outta here.
Each of these options has its disadvantages (somewhat exemplified by the italicised comments), be it that they are (respectively) too strict, not strict enough and confusing
So, I'm now making this behaviour configurable. Something like (for an OO inspired module):
package Foo;
# ...
do_tricky_stuff or $self->_complain("Tricky stuff failed");
do_unrecoverable_stuff or return $self->_complain("Grave problem");
# ...
sub _complain {
my $self=shift;
my $msg=shift;
croak($msg) if $self->fatal_warnings();
carp($msg);
return undef;
}
This allows for exceptions (everything is an exception) or recoverable / non recoverable warnings (non recoverable return undef), depending on the (user settable) value of an instance attribute.
I'm looking for some input on how other people handle this.
CU
Robartes-