autodie - Replace functions with ones that succeed or die with lexical scope
use autodie; # Recommended, implies 'use autodie qw(:default)' 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 }
bIlujDI' yIchegh()Qo'; yIHegh()! It is better to die() than to return() in failure. -- Klingon programming proverb.
The autodie pragma provides a convenient way to replace functions that normally return false on failure with equivalents that throw an exception on failure.
autodie
The autodie pragma has lexical scope, meaning that functions and subroutines altered with autodie will only change their behaviour until the end of the enclosing block, file, or eval.
eval
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.
system
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:
use feature qw(switch); eval { use autodie; open(my $fh, '<', $some_file); my @records = <$fh>; # Do things with @records... close($fh); }; given ($@) { when (undef) { say "No error"; } when ('open') { say "Error from open"; } when (':io') { say "Non-open, IO error."; } when (':all') { say "All other autodie errors." } default { say "Not an autodie error at all." } }
Under Perl 5.8, the given/when structure is not available, so the following structure may be used:
given/when
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.
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, fileno, open and sysopen.
:file
close
fcntl
fileno
open
sysopen
The categories are currently:
:all :default :io read seek sysread sysseek syswrite :dbm dbmclose dbmopen :file binmode close fcntl fileno flock ioctl open sysopen truncate :filesys chdir closedir opendir link mkdir readlink rename rmdir symlink unlink :ipc 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.
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.
use autodie
use autodie qw(:default)
exec
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 surity that no behavorial changes will occur if the autodie module is upgraded.
:default
It is not considered an error for flock to return false if it fails 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:
flock
EWOULDBLOCK
LOCK_NB
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.
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. 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.
system { $cmd } @args
exec { $cmd } @args
CORE::system
CORE::exec
no autodie qw(system exec)
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.
The :void option is supported in Fatal, but not autodie. However you can explicitly disable autodie end the end of the current block with no autodie. To disable autodie for only a single function (eg, open) use or no autodie qw(open).
:void
no autodie
no autodie qw(open)
See also "DIAGNOSTICS" in Fatal.
"Used only once" warnings can be generated when autodie or Fatal is used with package filehandles (eg, FILE). It's strongly recommended you use scalar filehandles instead.
Fatal
FILE
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 ue Fatal or autodie on other user subroutines will result in a compile-time error.
Please report bugs via the CPAN Request Tracker at http://rt.cpan.org/NoAuth/Bugs.html?Dist=autodie.
If you find this module useful, please consider rating it on the CPAN Ratings service at http://cpanratings.perl.org/rate?distribution=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>.
Copyright 2008, Paul Fenwick <pjf@perltraining.com.au>
This module is free software. You may distribute it under the same terms as Perl itself.
Fatal, autodie::exception, IPC::System::Simple
Perl tips, autodie at http://perltraining.com.au/tips/2008-08-20.html
Mark Reed and Roland Giersig -- Klingon translators.
See the AUTHORS file for full credits. The latest version of this file can be found at http://github.com/pfenwick/autodie/tree/AUTHORS .
To install autodie, copy and paste the appropriate command in to your terminal.
cpanm
cpanm autodie
CPAN shell
perl -MCPAN -e shell install autodie
For more information on module installation, please visit the detailed CPAN module installation guide.