G'day Christian,
On Sun Apr 12 14:23:53 2009, MITHALDU wrote:
Show quoted text> Would it be possible to add an option to make it die with something
> more useful, like "confess"?
There is, but you need to look under the hood to find it. There's the
short way:
package my::autodie;
use base qw(autodie);
sub exception_class { return "my::exception"; }
If you now use the 'my::autodie' pragma, whenever it raises an
exception, it will do so using:
my::exception->new(
function => 'what_just_died',
args => [ 'args', 'to', 'what', 'died' ],
pragma => 'my::autodie',
errno => \$!,
);
The exception is expected to find other remaining information using
caller().
If you just wanted confess functionality, you could *probably* use (in
my/exception.pm):
package my::exception;
use Carp qw(confess);
sub throw { shift; confess @_; }
If you *know* that 'my::exception' is already going to be loaded, you
can overload autodie::throw rather than autodie::exception_class. The
default throw() jumps through hoops to load the exception module first.
The ideal situation wouldn't be to use confess() (which loses you all
the rich exception objects), but to instead create an exception in (say)
Class::Exception, which has a stack backtrace option built-in. This
would allow you to have your backtrace, and your rich exception objects
at the same time.
The core autodie implementation won't contain such code, as autodie is
slated to be released with Perl 5.10.1, and as such there are limits to
its dependencies. However I'd *love* to see a module that does this
released on the CPAN. If you wish to take a shot, I'm *very* happy to
provide support, including tweaking the internals of autodie if needed.
Since I'm presenting on autodie at OSCON, and since stack-backtrace
functionality really is a must-have feature, there's an excellent chance
I'll write this myself on a trans-pacific flight in July.
The public-API part of my brain tells me that the best way to implement
the stack backtrace option is to allow the autodie exception class to be
controlled with an environment variable:
export AUTODIE_EXCEPTION=autodie::exception::backtrace
since that allows users to re-run their programs without having to make
code changes. However since it's before my first coffee of the day, I'm
not sure yet if this is actually a good idea.
All the best,
Paul