14 use constant ERROR_WRONG_FATAL => q{
15 Incorrect version of Fatal.pm loaded by autodie.
17 The autodie pragma uses an updated version of Fatal to do its
18 heavy lifting. We seem to have loaded Fatal version %s, which is
19 probably the version that came with your version of Perl. However
20 autodie needs version %s, which would have come bundled with
23 You may be able to solve this problem by adding the following
24 line of code to your main program, before any use of Fatal or
31 # We have to check we've got the right version of Fatal before we
32 # try to compile the rest of our code, lest we use a constant
37 # If we have the wrong Fatal, then we've probably loaded the system
38 # one, not our own. Complain, and give a useful hint. ;)
40 if ($Fatal::VERSION ne $VERSION) {
41 my $autodie_path = $INC{'autodie.pm'};
43 $autodie_path =~ s/autodie\.pm//;
48 ERROR_WRONG_FATAL, $Fatal::VERSION, $VERSION, $autodie_path
53 # When passing args to Fatal we want to keep the first arg
54 # (our package) in place. Hence the splice.
57 splice(@_,1,0,Fatal::LEXICAL_TAG);
62 splice(@_,1,0,Fatal::LEXICAL_TAG);
63 goto &Fatal::unimport;
72 autodie - Replace functions with ones that succeed or die with lexical scope
76 use autodie; # Recommended: implies 'use autodie qw(:default)'
78 use autodie qw(:all); # Recommended more: defaults and system/exec.
80 use autodie qw(open close); # open/close succeed or die
82 open(my $fh, "<", $filename); # No need to check!
85 no autodie qw(open); # open failures won't die
86 open(my $fh, "<", $filename); # Could fail silently!
87 no autodie; # disable all autodies
92 bIlujDI' yIchegh()Qo'; yIHegh()!
94 It is better to die() than to return() in failure.
96 -- Klingon programming proverb.
98 The C<autodie> pragma provides a convenient way to replace functions
99 that normally return false on failure with equivalents that throw
100 an exception on failure.
102 The C<autodie> pragma has I<lexical scope>, meaning that functions
103 and subroutines altered with C<autodie> will only change their behaviour
104 until the end of the enclosing block, file, or C<eval>.
106 If C<system> is specified as an argument to C<autodie>, then it
107 uses L<IPC::System::Simple> to do the heavy lifting. See the
108 description of that module for more information.
112 Exceptions produced by the C<autodie> pragma are members of the
113 L<autodie::exception> class. The preferred way to work with
114 these exceptions under Perl 5.10 is as follows:
116 use feature qw(switch);
121 open(my $fh, '<', $some_file);
125 # Do things with @records...
132 when (undef) { say "No error"; }
133 when ('open') { say "Error from open"; }
134 when (':io') { say "Non-open, IO error."; }
135 when (':all') { say "All other autodie errors." }
136 default { say "Not an autodie error at all." }
139 Under Perl 5.8, the C<given/when> structure is not available, so the
140 following structure may be used:
145 open(my $fh, '<', $some_file);
149 # Do things with @records...
154 if ($@ and $@->isa('autodie::exception')) {
155 if ($@->matches('open')) { print "Error from open\n"; }
156 if ($@->matches(':io' )) { print "Non-open, IO error."; }
158 # A non-autodie exception.
161 See L<autodie::exception> for further information on interrogating
166 Autodie uses a simple set of categories to group together similar
167 built-ins. Requesting a category type (starting with a colon) will
168 enable autodie for all built-ins beneath that category. For example,
169 requesting C<:file> will enable autodie for C<close>, C<fcntl>,
170 C<fileno>, C<open> and C<sysopen>.
172 The categories are currently:
239 Note that while the above category system is presently a strict
240 hierarchy, this should not be assumed.
242 A plain C<use autodie> implies C<use autodie qw(:default)>. Note that
243 C<system> and C<exec> are not enabled by default. C<system> requires
244 the optional L<IPC::System::Simple> module to be installed, and enabling
245 C<system> or C<exec> will invalidate their exotic forms. See L</BUGS>
246 below for more details.
250 use autodie qw(:1.994);
252 allows the C<:default> list from a particular version to be used. This
253 provides the convenience of using the default methods, but the surety
254 that no behavorial changes will occur if the C<autodie> module is
257 C<autodie> can be enabled for all of Perl's built-ins, including
258 C<system> and C<exec> with:
260 use autodie qw(:all);
262 =head1 FUNCTION SPECIFIC NOTES
266 It is not considered an error for C<flock> to return false if it fails
267 to an C<EWOULDBLOCK> (or equivalent) condition. This means one can
268 still use the common convention of testing the return value of
269 C<flock> when called with the C<LOCK_NB> option:
273 if ( flock($fh, LOCK_EX | LOCK_NB) ) {
277 Autodying C<flock> will generate an exception if C<flock> returns
278 false with any other error.
282 The C<system> built-in is considered to have failed in the following
289 The command does not start.
293 The command is killed by a signal.
297 The command returns a non-zero exit value (but see below).
301 On success, the autodying form of C<system> returns the I<exit value>
302 rather than the contents of C<$?>.
304 Additional allowable exit values can be supplied as an optional first
305 argument to autodying C<system>:
307 system( [ 0, 1, 2 ], $cmd, @args); # 0,1,2 are good exit values
309 C<autodie> uses the L<IPC::System::Simple> module to change C<system>.
310 See its documentation for further information.
312 Applying C<autodie> to C<system> or C<exec> causes the exotic
313 forms C<system { $cmd } @args > or C<exec { $cmd } @args>
314 to be considered a syntax error until the end of the lexical scope.
315 If you really need to use the exotic form, you can call C<CORE::system>
316 or C<CORE::exec> instead, or use C<no autodie qw(system exec)> before
317 calling the exotic form.
321 Functions called in list context are assumed to have failed if they
322 return an empty list, or a list consisting only of a single undef
329 =item :void cannot be used with lexical scope
331 The C<:void> option is supported in L<Fatal>, but not
332 C<autodie>. To workaround this, C<autodie> may be explicitly disabled until
333 the end of the current block with C<no autodie>.
334 To disable autodie for only a single function (eg, open)
335 use C<no autodie qw(open)>.
337 =item No user hints defined for %s
339 You've insisted on hints for user-subroutines, either by pre-pending
340 a C<!> to the subroutine name itself, or earlier in the list of arguments
341 to C<autodie>. However the subroutine in question does not have
346 See also L<Fatal/DIAGNOSTICS>.
350 "Used only once" warnings can be generated when C<autodie> or C<Fatal>
351 is used with package filehandles (eg, C<FILE>). Scalar filehandles are
352 strongly recommended instead.
354 When using C<autodie> or C<Fatal> with user subroutines, the
355 declaration of those subroutines must appear before the first use of
356 C<Fatal> or C<autodie>, or have been exported from a module.
357 Attempting to use C<Fatal> or C<autodie> on other user subroutines will
358 result in a compile-time error.
360 Due to a bug in Perl, C<autodie> may "lose" any format which has the
361 same name as an autodying built-in or function.
363 C<autodie> may not work correctly if used inside a file with a
364 name that looks like a string eval, such as F<eval (3)>.
366 =head2 autodie and string eval
368 Due to the current implementation of C<autodie>, unexpected results
369 may be seen when used near or with the string version of eval.
370 I<None of these bugs exist when using block eval>.
372 Under Perl 5.8 only, C<autodie> I<does not> propagate into string C<eval>
373 statements, although it can be explicitly enabled inside a string
376 Under Perl 5.10 only, using a string eval when C<autodie> is in
377 effect can cause the autodie behaviour to leak into the surrounding
378 scope. This can be worked around by using a C<no autodie> at the
379 end of the scope to explicitly remove autodie's effects, or by
380 avoiding the use of string eval.
382 I<None of these bugs exist when using block eval>. The use of
383 C<autodie> with block eval is considered good practice.
385 =head2 REPORTING BUGS
387 Please report bugs via the CPAN Request Tracker at
388 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=autodie>.
392 If you find this module useful, please consider rating it on the
393 CPAN Ratings service at
394 L<http://cpanratings.perl.org/rate?distribution=autodie> .
396 The module author loves to hear how C<autodie> has made your life
397 better (or worse). Feedback can be sent to
398 E<lt>pjf@perltraining.com.auE<gt>.
402 Copyright 2008-2009, Paul Fenwick E<lt>pjf@perltraining.com.auE<gt>
406 This module is free software. You may distribute it under the
407 same terms as Perl itself.
411 L<Fatal>, L<autodie::exception>, L<autodie::hints>, L<IPC::System::Simple>
413 I<Perl tips, autodie> at
414 L<http://perltraining.com.au/tips/2008-08-20.html>
416 =head1 ACKNOWLEDGEMENTS
418 Mark Reed and Roland Giersig -- Klingon translators.
420 See the F<AUTHORS> file for full credits. The latest version of this
422 L<http://github.com/pfenwick/autodie/tree/master/AUTHORS> .