Fatal::Exception - Succeed or throw exception
use Fatal::Exception 'Exception::System' => qw< open close >; open my $fh, "/nonexistent"; # throw Exception::System use Exception::Base 'Exception::My'; sub juggle { ... } Fatal::Exception->import('Exception::My' => 'juggle'); juggle; # succeed or throw exception Fatal::Exception->unimport('juggle'); juggle or die; # restore original behavior
Fatal::Exception provides a way to conveniently replace functions which normally return a false value when they fail with equivalents which raise exceptions if they are not successful. This is the same as Fatal module from Perl 5.8 and previous but it throws Exception::Base object on error.
Replaces the original functions with wrappers which provide do-or-throw equivalents. You may wrap both user-defined functions and overridable CORE operators (except exec, system which cannot be expressed via prototypes) in this way.
If wrapped function occurs fatal error, the error is converted into Exception::Fatal exception.
If the symbol :void appears in the import list, then functions named later in that import list raise an exception only when these are called in void context.
:void
You should not fatalize functions that are called in list context, because this module tests whether a function has failed by testing the boolean truth of its return value in scalar context.
If the exception class is not exist, its module is loaded with "use Exception" automatically.
Restores original functions for user-defined functions or replaces the functions with do-without-die wrappers for CORE operators.
In fact, the CORE operators cannot be restored, so the non-fatalized alternative is provided instead.
The functions can be wrapped and unwrapped all the time.
The Fatal::Exception module was benchmarked with other implementations. The results are following:
--------------------------------------------------------------- | Module | Success | Failure | --------------------------------------------------------------- | eval/die | 289616/s | 236308/s | --------------------------------------------------------------- | Fatal | 94627/s | 8967/s | --------------------------------------------------------------- | Fatal::Exception | 143479/s | 9677/s | ---------------------------------------------------------------
This module is a fork of Fatal module from Perl 5.8. The latest Perl will replace the Fatal module with autodie module which is similar to Fatal::Exception.
Fatal::Exception
The Fatal::Exception doesn't work with lexical scope, yet. It also doesn't support "system" in perlfunc or "exec" in perlfunc core functions and extra import tags. It throws Exception::Base-d exceptions on failure so they can be handled as other Exception::Base-d exceptions.
More details:
Fatal, autodie, Exception::Base, Exception::System
If you find the bug, please report it.
Piotr Roszatycki <dexter@debian.org>
Copyright (C) 2007, 2008 by Piotr Roszatycki <dexter@debian.org>.
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
See http://www.perl.com/perl/misc/Artistic.html
To install Fatal::Exception, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Fatal::Exception
CPAN shell
perl -MCPAN -e shell install Fatal::Exception
For more information on module installation, please visit the detailed CPAN module installation guide.