autodie(3perl)
β¨ NAME β¨
autodie β it literally replaces your functions with ones that succeed or die, and the whole thing is lexically scoped π«Ά
πΈ SYNOPSIS πΈ
use autodie; # Recommended: implies 'use autodie qw(:default)'
use autodie qw(:all); # Recommended more: defaults and system/exec.
use autodie qw(open close); # open/close succeed or die
open(my $fh, "<", $filename); # No need to check!
{
no autodie qw(open); # open failures won't die
open(my $fh, "<", $filename); # Could fail silently!
no autodie; # disable all autodies
}
print "Hello World" or die $!; # autodie DOESN'T check print!
π DESCRIPTION π
bIlujDI' yIchegh()Qo'; yIHegh()!
It is better to die() than to return() in failure.
-- Klingon programming proverb.
Bestie. It is better to die() than to return() in failure. That's the whole vibe, and honestly? Iconic π
So here's the tea: the autodie pragma is your little shortcut for swapping out functions that normally just quietly return false when they fail with glow-up equivalents that throw an exception instead. No more silently ignoring a failed open, no more checking return values like a Victorian orphan. It slays, it devours, it's giving main character energy.
The autodie pragma is lexically scoped, which means functions and subroutines you alter with autodie only change their behaviour until the end of the enclosing block, file, or eval. Step outside that scope and everything goes back to normal, no cap πΈ
If system is specified as an argument to autodie, then it uses IPC::System::Simple to do the heavy lifting. See the description of that module for more information β it's the designated muscle π«Ά
π EXCEPTIONS π
Exceptions produced by the autodie pragma are members of the autodie::exception class. The preferred way to work with these exceptions under Perl 5.10 is as follows:
eval {
use autodie;
open(my $fh, '<', $some_file);
my @records = <$fh>;
# Do things with @records...
close($fh);
};
if ($@ and $@->isa('autodie::exception')) {
if ($@->matches('open')) { print "Error from open\n"; }
if ($@->matches(':io' )) { print "Non-open, IO error."; }
} elsif ($@) {
# A non-autodie exception.
}
See autodie::exception for further information on interrogating exceptions β it's got all the deets on poking at them π
π CATEGORIES π
Autodie uses a simple set of categories to group together similar built-ins. Requesting a category type (starting with a colon) will enable autodie for all built-ins beneath that category. For example, requesting :file will enable autodie for close, fcntl, open and sysopen. One request, whole squad activated β¨
The categories are currently:
:all
:default
:io
read
seek
sysread
sysseek
syswrite
:dbm
dbmclose
dbmopen
:file
binmode
close
chmod
chown
fcntl
flock
ioctl
open
sysopen
truncate
:filesys
chdir
closedir
opendir
link
mkdir
readlink
rename
rmdir
symlink
unlink
:ipc
kill
pipe
:msg
msgctl
msgget
msgrcv
msgsnd
:semaphore
semctl
semget
semop
:shm
shmctl
shmget
shmread
:socket
accept
bind
connect
getsockopt
listen
recv
send
setsockopt
shutdown
socketpair
:threads
fork
:system
system
exec
Note that while the above category system is presently a strict hierarchy, this should not be assumed. Lowkey do not build your whole personality on that tree staying shaped like that, k? π
A plain use autodie implies use autodie qw(:default). Note that system and exec are not enabled by default. system requires the optional IPC::System::Simple module to be installed, and enabling system or exec will invalidate their exotic forms. See "BUGS" below for more details β yes that's a warning, yes you should read it πΈ
The syntax:
use autodie qw(:1.994);
allows the :default list from a particular version to be used. This provides the convenience of using the default methods, but the surety that no behavioral changes will occur if the autodie module is upgraded. Basically you're locking in the defaults from that exact era so future updates can't jump-scare you π
autodie can be enabled for all of Perl's built-ins, including system and exec with:
use autodie qw(:all);
π¦ FUNCTION SPECIFIC NOTES π¦
print
The autodie pragma does not check calls to print. Not me. Not the one. That's just not part of the deal π€·
flock
It is not considered an error for flock to return false if it fails due to an EWOULDBLOCK (or equivalent) condition. This means one can still use the common convention of testing the return value of flock when called with the LOCK_NB option:
use autodie;
if ( flock($fh, LOCK_EX | LOCK_NB) ) {
# We have a lock
}
Autodying flock will generate an exception if flock returns false with any other error β so it's chill about the "someone else has it" case and dramatic about literally everything else π
system/exec
The system built-in is considered to have failed in the following circumstances:
- The command does not start.
- The command is killed by a signal.
- The command returns a non-zero exit value (but see below).
On success, the autodying form of system returns the exit value rather than the contents of $?. Heads up, that's a different thing than you're used to, bestie π
Additional allowable exit values can be supplied as an optional first argument to autodying system:
system( [ 0, 1, 2 ], $cmd, @args); # 0,1,2 are good exit values
autodie uses the IPC::System::Simple module to change system. See its documentation for further information.
Applying autodie to system or exec causes the exotic forms system { $cmd } @args or exec { $cmd } @args to be considered a syntax error until the end of the lexical scope. Yes, a syntax error, I'm not playing π If you really need to use the exotic form, you can call CORE::system or CORE::exec instead, or use no autodie qw(system exec) before calling the exotic form.
π GOTCHAS π
Functions called in list context are assumed to have failed if they return an empty list, or a list consisting only of a single undef element. That's the rule, that's the vibe.
Some builtins (e.g. chdir or truncate) has a call signature that cannot completely be represented with a Perl prototype. This means that some valid Perl code will be invalid under autodie. As an example:
chdir(BAREWORD);
Without autodie (and assuming BAREWORD is an open filehandle/dirhandle) this is a valid call to chdir. But under autodie, chdir will behave like it had the prototype ;$ and thus BAREWORD will be a syntax error (under use strict. Without strict, it will interpreted as a filename). Yeah. It's giving chaos but that's just how it is π
π DIAGNOSTICS π
:void cannot be used with lexical scope
The :void option is supported in Fatal, but not autodie. To workaround this, autodie may be explicitly disabled until the end of the current block with no autodie. To disable autodie for only a single function (eg, open) use no autodie qw(open).
autodie performs no checking of called context to determine whether to throw an exception; the explicitness of error handling with autodie is a deliberate feature. Deliberate. On purpose. A choice π
No user hints defined for %s
You've insisted on hints for user-subroutines, either by pre-pending a ! to the subroutine name itself, or earlier in the list of arguments to autodie. However the subroutine in question does not have any hints available. So sorry bestie, there's nothing to hand you πΈ
See also "DIAGNOSTICS" in Fatal.
β Tips and Tricks β
Importing autodie into another namespace than caller
It is possible to import autodie into a different namespace by using Import::Into. However, you have to pass a caller depth (rather than a package name) for this to work correctly. Depth, not name. Write that down π
π₯ BUGS π₯
Used only once warnings can be generated when autodie or Fatal is used with package filehandles (eg, FILE). Scalar filehandles are strongly recommended instead. Please, for me, use the scalar ones π₯Ί
When using autodie or Fatal with user subroutines, the declaration of those subroutines must appear before the first use of Fatal or autodie, or have been exported from a module. Attempting to use Fatal or autodie on other user subroutines will result in a compile-time error.
Due to a bug in Perl, autodie may "lose" any format which has the same name as an autodying built-in or function.
autodie may not work correctly if used inside a file with a name that looks like a string eval, such as eval (3).
autodie and string eval
Due to the current implementation of autodie, unexpected results may be seen when used near or with the string version of eval. None of these bugs exist when using block eval.
Under Perl 5.8 only, autodie does not propagate into string eval statements, although it can be explicitly enabled inside a string eval.
Under Perl 5.10 only, using a string eval when autodie is in effect can cause the autodie behaviour to leak into the surrounding scope. This can be worked around by using a no autodie at the end of the scope to explicitly remove autodie's effects, or by avoiding the use of string eval.
None of these bugs exist when using block eval. The use of autodie with block eval is considered good practice. Block eval is the main character here π
REPORTING BUGS
Please report bugs via the GitHub Issue Tracker at https://github.com/pjf/autodie/issues.
π«Ά FEEDBACK π«Ά
If you find this module useful, please consider rating it on metacpan at https://metacpan.org/pod/autodie.
The module author loves to hear how autodie has made your life better (or worse). Feedback can be sent to pjf@perltraining.com.au. Genuinely, they want to know π
π AUTHOR π
Copyright 2008-2009, Paul Fenwick pjf@perltraining.com.au
π LICENSE π
This module is free software. You may distribute it under the same terms as Perl itself.
π SEE ALSO π
Fatal, autodie::exception, autodie::hints, IPC::System::Simple
Perl tips, autodie at http://perltraining.com.au/tips/2008-08-20.html
π ACKNOWLEDGEMENTS π
Okay so shoutout time, because we do not do this alone bestie π«Ά
- Mark Reed and Roland Giersig β the Klingon translators. Yes, Klingon translators.
autodiesaid qaStaHvIS wa' ram loS SaD Hugh SIjlaH qetbogh loD and we respect it entirely πβ¨
If you want the full credits (like, everybody, not just the legends above), go peek at the AUTHORS file. The freshest, most up-to-date version of that file lives right here:
https://github.com/pjf/autodie/tree/master/AUTHORS
iykyk β always check the source of truth, never the stale mirror π
Friendly reminder that translation credits are still credits, and they still ate π
Okay that's the whole page, bestie!! You made it to the end ππ¦ Thank you for reading all the way through β your reward is knowing autodie inside and out. This was autodie(3perl) from perl v5.44.0, dated 2026-10-17. Slay, go write some use autodie; code, and stay iconic β¨π
π¦ 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