autodi...eption(3perl)
β¨ autodie::exception π
okay bestie so this whole page is about autodie::exception β the little object that shows up when an autodying function has a moment. it lives in the Perl Programmers Reference Guide, and it's literally just: Exceptions from autodying functions. π¦
πΈ SYNOPSIS πΈ
eval {
use autodie;
open(my $fh, '<', 'some_file.txt');
...
};
if (my $E = $@) {
say "Ooops! ",$E->caller," had problems: $@";
}
π DESCRIPTION π
So here's the tea π«: when a function that's been enabled by autodie fails, it doesn't just quietly die β it makes an autodie::exception object. and you can interrogate that object to find out more about what actually went wrong. it's giving detective energy. π
This doc is split into two sections, bestie:
- the methods that are most useful to the end-developer (that's you, day to day)
- the methods for anyone who wants to subclass
autodie::exceptionor get very familiar with it
π Common Methods π
these are the ones you'll be using in the everyday dealing-of-exceptions grind. β¨
The following all assume that the error has been copied into a separate scalar:
if ($E = $@) {
...
}
This is not required, but it is recommended in case any code gets called that may reset or alter $@. (your $@ is precious, protect it π)
args
my $array_ref = $E->args;
Gives you a reference to the arguments that were passed to the subroutine that died. π
function
my $sub = $E->function;
The subroutine (including its package, thank you) that threw the exception.
file
my $file = $E->file;
The file where the error happened (eg, myscript.pl or MyTest.pm). π
package
my $package = $E->package;
The package from which the exceptional subroutine was called.
caller
my $caller = $E->caller;
The subroutine that called the exceptional code. the accomplice, basically. π
line
my $line = $E->line;
The line in $E->file where the exceptional code was called.
context
my $context = $E->context;
The context in which the subroutine was called by autodie β usually the same as the context in which you called the autodying subroutine. It can be 'list', 'scalar', or undefined (aka unknown). It will never be 'void', because autodie always captures the return value one way or another. no cap.
Also, for some core functions that always return a scalar value no matter their context (eg, chown), this may be 'scalar', even if you used a list context. iconic behaviour, honestly.
return
my $return_value = $E->return;
The value(s) returned by the failed subroutine. If the subroutine was called in a list context, this will always be a reference to an array containing the results. If it was called in a scalar context, this will be the actual scalar returned. π«
errno
my $errno = $E->errno;
The value of $! at the time when the exception occurred.
NOTE: This method will leave the mainautodie::exceptionclass and become part of a role in the future. You should only callerrnofor exceptions where$!would reasonably have been set on failure. β οΈ
eval_error
my $old_eval_error = $E->eval_error;
The contents of $@ immediately after autodie triggered an exception. This may be useful when dealing with modules such as Text::Balanced that set (but do not throw) $@ on error.
matches
if ( $e->matches('open') ) { ... }
if ( 'open' ~~ $e ) { ... }
matches is how you figure out whether a given exception matches a particular role. β¨
An exception is considered to match a string if:
- For a string not starting with a colon, the string exactly matches the package and subroutine that threw the exception. For example,
MyModule::log. If the string does not contain a package name,CORE::is assumed. π - For a string that does start with a colon, if the subroutine throwing the exception does that behaviour. For example, the
CORE::opensubroutine does:file,:ioand:all.
See "CATEGORIES" in autodie for further information.
On Perl 5.10 and above, using smart-match (~~) with an autodie::exception object will use matches underneath. This module used to recommend using smart-match with the exception object on the left hand side, but in future Perls that is likely to stop working. π¨ The smart-match facility of this class should only be used with the exception object on the right hand side. Having the exception object on the right is both future-proof and portable to older Perls, back to 5.10. Beware that this facility can only be relied upon when it is certain that the exception object actually is an autodie::exception object; it is no more capable than an explicit call to the matches method.
π Advanced methods π
These are usable from anywhere, but they're primarily for developers who want to subclass autodie::exception, write code that registers custom error messages, or otherwise work very closely with the autodie::exception model. main character energy, honestly. π
register
autodie::exception->register( 'CORE::open' => \&mysub );
The register method lets you register a message handler for a given subroutine. You should use the full subroutine name, including the package. π
Registered message handlers will receive the autodie::exception object as the first parameter.
add_file_and_line
say "Problem occurred",$@->add_file_and_line;
Returns the string " at %s line %d", where %s gets replaced with the filename, and %d gets replaced with the line number. π’
Primarily intended for use by format handlers.
stringify
say "The error was: ",$@->stringify;
Formats the error as a human readable string. Usually there's no reason to call this directly, since it gets 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.
format_default
my $error_string = $E->format_default;
This produces the default error string for the given exception, without using any registered message handlers. It's primarily intended to be called from a message handler when they've been passed an exception they don't want to format. π
Child classes can override this method to change how default messages are formatted.
new
my $error = autodie::exception->new(
args => \@_,
function => "CORE::open",
errno => $!,
context => 'scalar',
return => undef,
);
Creates a new autodie::exception object. Normally called directly from an autodying function. The function argument is required β it's 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. π
Attributes such as package, file, and caller are determined automatically, and cannot be specified.
π«Ά SEE ALSO π«Ά
autodieautodie::exception::system
π LICENSE π
Copyright (C)2008 Paul Fenwick
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. πΈ
π AUTHOR π
Paul Fenwick <pjf@perltraining.com.au>
and that's the whole page bestie, you ate that π
β¨ this was perl v5.44.0, dated 2026-10-17, filed under autodi...eption(3perl) β go forth and interrogate your exceptions, periodt π¦π
π¦ where this page comes from
- arch rolling: perl 5.44.0-1 Β· Artistic-1.0-Perl AND GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms