1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
|
package autodie;
use 5.008;
use strict;
use warnings;
use Fatal ();
our @ISA = qw(Fatal);
our $VERSION;
BEGIN {
$VERSION = "1.998";
}
use constant ERROR_WRONG_FATAL => q{
Incorrect version of Fatal.pm loaded by autodie.
The autodie pragma uses an updated version of Fatal to do its
heavy lifting. We seem to have loaded Fatal version %s, which is
probably the version that came with your version of Perl. However
autodie needs version %s, which would have come bundled with
autodie.
You may be able to solve this problem by adding the following
line of code to your main program, before any use of Fatal or
autodie.
use lib "%s";
};
# We have to check we've got the right version of Fatal before we
# try to compile the rest of our code, lest we use a constant
# that doesn't exist.
BEGIN {
# If we have the wrong Fatal, then we've probably loaded the system
# one, not our own. Complain, and give a useful hint. ;)
if ($Fatal::VERSION ne $VERSION) {
my $autodie_path = $INC{'autodie.pm'};
$autodie_path =~ s/autodie\.pm//;
require Carp;
Carp::croak sprintf(
ERROR_WRONG_FATAL, $Fatal::VERSION, $VERSION, $autodie_path
);
}
}
# When passing args to Fatal we want to keep the first arg
# (our package) in place. Hence the splice.
sub import {
splice(@_,1,0,Fatal::LEXICAL_TAG);
goto &Fatal::import;
}
sub unimport {
splice(@_,1,0,Fatal::LEXICAL_TAG);
goto &Fatal::unimport;
}
1;
__END__
=head1 NAME
autodie - Replace functions with ones that succeed or die with lexical scope
=head1 SYNOPSIS
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
}
=head1 DESCRIPTION
bIlujDI' yIchegh()Qo'; yIHegh()!
It is better to die() than to return() in failure.
-- Klingon programming proverb.
The C<autodie> pragma provides a convenient way to replace functions
that normally return false on failure with equivalents that throw
an exception on failure.
The C<autodie> pragma has I<lexical scope>, meaning that functions
and subroutines altered with C<autodie> will only change their behaviour
until the end of the enclosing block, file, or C<eval>.
If C<system> is specified as an argument to C<autodie>, then it
uses L<IPC::System::Simple> to do the heavy lifting. See the
description of that module for more information.
=head1 EXCEPTIONS
Exceptions produced by the C<autodie> pragma are members of the
L<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 C<given/when> structure is not available, so the
following structure may be used:
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 L<autodie::exception> for further information on interrogating
exceptions.
=head1 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 C<:file> will enable autodie for C<close>, C<fcntl>,
C<fileno>, C<open> and C<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 C<use autodie> implies C<use autodie qw(:default)>. Note that
C<system> and C<exec> are not enabled by default. C<system> requires
the optional L<IPC::System::Simple> module to be installed, and enabling
C<system> or C<exec> will invalidate their exotic forms. See L</BUGS>
below for more details.
The syntax:
use autodie qw(:1.994);
allows the C<: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 C<autodie> module is
upgraded.
=head1 FUNCTION SPECIFIC NOTES
=head2 flock
It is not considered an error for C<flock> to return false if it fails
to an C<EWOULDBLOCK> (or equivalent) condition. This means one can
still use the common convention of testing the return value of
C<flock> when called with the C<LOCK_NB> option:
use autodie;
if ( flock($fh, LOCK_EX | LOCK_NB) ) {
# We have a lock
}
Autodying C<flock> will generate an exception if C<flock> returns
false with any other error.
=head2 system/exec
Applying C<autodie> to C<system> or C<exec> causes the exotic
forms C<system { $cmd } @args > or C<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 C<CORE::system>
or C<CORE::exec> instead, or use C<no autodie qw(system exec)> before
calling the exotic form.
=head1 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.
=head1 DIAGNOSTICS
=over 4
=item :void cannot be used with lexical scope
The C<:void> option is supported in L<Fatal>, but not
C<autodie>. However you can explicitly disable autodie
end the end of the current block with C<no autodie>.
To disable autodie for only a single function (eg, open)
use or C<no autodie qw(open)>.
=back
See also L<Fatal/DIAGNOSTICS>.
=head1 BUGS
"Used only once" warnings can be generated when C<autodie> or C<Fatal>
is used with package filehandles (eg, C<FILE>). It's strongly recommended
you use scalar filehandles instead.
When using C<autodie> or C<Fatal> with user subroutines, the
declaration of those subroutines must appear before the first use of
C<Fatal> or C<autodie>, or have been exported from a module.
Attempting to ue C<Fatal> or C<autodie> on other user subroutines will
result in a compile-time error.
=head2 REPORTING BUGS
Please report bugs via the CPAN Request Tracker at
L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=autodie>.
=head1 FEEDBACK
If you find this module useful, please consider rating it on the
CPAN Ratings service at
L<http://cpanratings.perl.org/rate?distribution=autodie> .
The module author loves to hear how C<autodie> has made your life
better (or worse). Feedback can be sent to
E<lt>pjf@perltraining.com.auE<gt>.
=head1 AUTHOR
Copyright 2008, Paul Fenwick E<lt>pjf@perltraining.com.auE<gt>
=head1 LICENSE
This module is free software. You may distribute it under the
same terms as Perl itself.
=head1 SEE ALSO
L<Fatal>, L<autodie::exception>, L<IPC::System::Simple>
I<Perl tips, autodie> at
L<http://perltraining.com.au/tips/2008-08-20.html>
=head1 ACKNOWLEDGEMENTS
Mark Reed and Roland Giersig -- Klingon translators.
See the F<AUTHORS> file for full credits. The latest version of this
file can be found at
L<http://github.com/pfenwick/autodie/tree/AUTHORS> .
=cut
|