eval {
use autodie;
open(my $fh, '<', 'some_file.txt');
...
};
if (my $E = $@) {
say "Ooops! ",$E->caller," had problems: $@";
}
This document is broken into two sections; those methods that are most useful to the end-developer, and those methods for anyone wishing to subclass or get very familiar with "autodie::exception".
The following assume that the error has been copied into a separate scalar:
if ($E = $@) {
...
}
This is not required, but is recommended in case any code is called which may reset or alter $@.
my $array_ref = $E->args;
Provides a reference to the arguments passed to the subroutine that died.
my $sub = $E->function;
The subroutine (including package) that threw the exception.
my $file = $E->file;
The file in which the error occurred (eg, "myscript.pl" or "MyTest.pm").
my $package = $E->package;
The package from which the exceptional subroutine was called.
my $caller = $E->caller;
The subroutine that called the exceptional code.
my $line = $E->line;
The line in "$E->file" where the exceptional code was called.
my $errno = $E->errno;
The value of $! at the time when the exception occurred.
NOTE: This method will leave the main "autodie::exception" class and become part of a role in the future. You should only call "errno" for exceptions where $! would reasonably have been set on failure.
if ( $e->matches('open') ) { ... }
if ( $e ~~ 'open' ) { ... }
"matches" is used to determine whether a given exception matches a particular role. On Perl 5.10, using smart-match ("~~") with an "autodie::exception" object will use "matches" underneath.
An exception is considered to match a string if:
See ``CATEGORIES'' in autodie for futher information.
autodie::exception->register( 'CORE::open' => \&mysub );
The "register" method allows for the registration of a message handler for a given subroutine. The full subroutine name including the package should be used.
Registered message handlers will receive the "autodie::exception" object as the first parameter.
say "Problem occurred",$@->add_file_and_line;
Returns the string " at %s line %d", where %s is replaced with the filename, and %d is replaced with the line number.
Primarily intended for use by format handlers.
say "The error was: ",$@->stringify;
Formats the error as a human readable string. Usually there's no reason to call this directly, as it is used automatically if an "autodie::exception" object is ever used as a string.
Child classes can override this method to change how they're stringified.
my $error_string = $E->format_default;
This produces the default error string for the given exception, without using any registered message handlers. It is primarily intended to be called from a message handler when they have been passed an exception they don't want to format.
Child classes can override this method to change how default messages are formatted.
my $error = autodie::exception->new(
args => \@_,
function => "CORE::open",
errno => $!,
);
Creates a new "autodie::exception" object. Normally called directly from an autodying function. The "function" argument is required, its the function we were trying to call that generated the exception. The "args" parameter is optional.
The "errno" value is optional. In versions of "autodie::exception" 1.99 and earlier the code would try to automatically use the current value of $!, but this was unreliable and is no longer supported.
Atrributes such as package, file, and caller are determined automatically, and cannot be specified.
This is free software. You may modify and/or redistribute this code under the same terms as Perl 5.10 itself, or, at your option, any later version of Perl 5.