autodie - Replace functions with ones that succeed or die with lexical scope
use autodie; # Recommended, implies 'use autodie qw(:all)' 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.
NOTE! This is BETA code. It is NOT the final release. Implementation and interface may change!
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 is as follows:
use feature qw(switch); eval { use autodie ':io'; open(my $fh, '<', $some_file); my @records = <$fh>; 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." } }
See autodie::exception for further information on interrogating exceptions.
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.
autodie only works on Perl 5.10. We'd like it to be able to work on Perl 5.8.
Currently, autodying system returns only a string, not a real exception object. This will change before the full release.
A bare autodie will change from meaning :all to :default before the final release.
:all
:default
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
Mark Reed and Roland Giersig -- Klingon translators.
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.