February 2012
Intermediate to advanced
1184 pages
37h 17m
English
use autodie;
This pragma turns failures of Perl function calls into fatal
errors, but only in its lexical scope. It replaces the standard Perl
functions that return false on failure with versions that throw exceptions
on failure. The exception message in $@
lets you know which sort you encountered:
eval {
use autodie;
open my $fh, "<:encoding(UTF–8)", $filename;
my @lines = <$fh>;
close $fh;
}
for ($@) {
when (undef) { }
when ("open") { say "Open failed"; }
when (":io") { say "Some other IO error"; }
when (":all") { say "Some other autodie error" }
default { say "Non–autodie error" }
}The pragma can replace related sets of functions, too, such as just the ones that deal with input and output:
use autodie qw(:io);
If you don’t want this feature for an inner scope, turn it off:
no autodie;
Read now
Unlock full access