Debian Codegraph @

cpan/autodie/lib/autodie.pm

raw
1 package autodie;
2 use 5.008;
3 use strict;
4 use warnings;
5
6 use parent qw(Fatal);
7 our $VERSION;
8
9 # ABSTRACT: Replace functions with ones that succeed or die with lexical scope
10
11 BEGIN {
12 our $VERSION = '2.37'; # VERSION: Generated by DZP::OurPkg::Version
13 }
14
15 use constant ERROR_WRONG_FATAL => q{
16 Incorrect version of Fatal.pm loaded by autodie.
17
18 The autodie pragma uses an updated version of Fatal to do its
19 heavy lifting. We seem to have loaded Fatal version %s, which is
20 probably the version that came with your version of Perl. However
21 autodie needs version %s, which would have come bundled with
22 autodie.
23
24 You may be able to solve this problem by adding the following
25 line of code to your main program, before any use of Fatal or
26 autodie.
27
28 use lib "%s";
29
30 };
31
32 # We have to check we've got the right version of Fatal before we
33 # try to compile the rest of our code, lest we use a constant
34 # that doesn't exist.
35
36 BEGIN {
37
38 # If we have the wrong Fatal, then we've probably loaded the system
39 # one, not our own. Complain, and give a useful hint. ;)
40
41 if (defined($Fatal::VERSION) and defined($VERSION) and $Fatal::VERSION ne $VERSION) {
42 my $autodie_path = $INC{'autodie.pm'};
43
44 $autodie_path =~ s/autodie\.pm//;
45
46 require Carp;
47
48 Carp::croak sprintf(
49 ERROR_WRONG_FATAL, $Fatal::VERSION, $VERSION, $autodie_path
50 );
51 }
52 }
53
54 # When passing args to Fatal we want to keep the first arg
55 # (our package) in place. Hence the splice.
56
57 sub import {
58 splice(@_,1,0,Fatal::LEXICAL_TAG);
59 goto &Fatal::import;
60 }
61
62 sub unimport {
63 splice(@_,1,0,Fatal::LEXICAL_TAG);
64 goto &Fatal::unimport;
65 }
66
67 1;
68
69 __END__
70
71 =head1 NAME
72
73 autodie - Replace functions with ones that succeed or die with lexical scope
74
75 =head1 SYNOPSIS
76
77 use autodie; # Recommended: implies 'use autodie qw(:default)'
78
79 use autodie qw(:all); # Recommended more: defaults and system/exec.
80
81 use autodie qw(open close); # open/close succeed or die
82
83 open(my $fh, "<", $filename); # No need to check!
84
85 {
86 no autodie qw(open); # open failures won't die
87 open(my $fh, "<", $filename); # Could fail silently!
88 no autodie; # disable all autodies
89 }
90
91 print "Hello World" or die $!; # autodie DOESN'T check print!
92
93 =head1 DESCRIPTION
94
95 bIlujDI' yIchegh()Qo'; yIHegh()!
96
97 It is better to die() than to return() in failure.
98
99 -- Klingon programming proverb.
100
101 The C<autodie> pragma provides a convenient way to replace functions
102 that normally return false on failure with equivalents that throw
103 an exception on failure.
104
105 The C<autodie> pragma has I<lexical scope>, meaning that functions
106 and subroutines altered with C<autodie> will only change their behaviour
107 until the end of the enclosing block, file, or C<eval>.
108
109 If C<system> is specified as an argument to C<autodie>, then it
110 uses L<IPC::System::Simple> to do the heavy lifting. See the
111 description of that module for more information.
112
113 =head1 EXCEPTIONS
114
115 Exceptions produced by the C<autodie> pragma are members of the
116 L<autodie::exception> class. The preferred way to work with
117 these exceptions under Perl 5.10 is as follows:
118
119 eval {
120 use autodie;
121
122 open(my $fh, '<', $some_file);
123
124 my @records = <$fh>;
125
126 # Do things with @records...
127
128 close($fh);
129 };
130
131 if ($@ and $@->isa('autodie::exception')) {
132 if ($@->matches('open')) { print "Error from open\n"; }
133 if ($@->matches(':io' )) { print "Non-open, IO error."; }
134 } elsif ($@) {
135 # A non-autodie exception.
136 }
137
138 See L<autodie::exception> for further information on interrogating
139 exceptions.
140
141 =head1 CATEGORIES
142
143 Autodie uses a simple set of categories to group together similar
144 built-ins. Requesting a category type (starting with a colon) will
145 enable autodie for all built-ins beneath that category. For example,
146 requesting C<:file> will enable autodie for C<close>, C<fcntl>,
147 C<open> and C<sysopen>.
148
149 The categories are currently:
150
151 :all
152 :default
153 :io
154 read
155 seek
156 sysread
157 sysseek
158 syswrite
159 :dbm
160 dbmclose
161 dbmopen
162 :file
163 binmode
164 close
165 chmod
166 chown
167 fcntl
168 flock
169 ioctl
170 open
171 sysopen
172 truncate
173 :filesys
174 chdir
175 closedir
176 opendir
177 link
178 mkdir
179 readlink
180 rename
181 rmdir
182 symlink
183 unlink
184 :ipc
185 kill
186 pipe
187 :msg
188 msgctl
189 msgget
190 msgrcv
191 msgsnd
192 :semaphore
193 semctl
194 semget
195 semop
196 :shm
197 shmctl
198 shmget
199 shmread
200 :socket
201 accept
202 bind
203 connect
204 getsockopt
205 listen
206 recv
207 send
208 setsockopt
209 shutdown
210 socketpair
211 :threads
212 fork
213 :system
214 system
215 exec
216
217
218 Note that while the above category system is presently a strict
219 hierarchy, this should not be assumed.
220
221 A plain C<use autodie> implies C<use autodie qw(:default)>. Note that
222 C<system> and C<exec> are not enabled by default. C<system> requires
223 the optional L<IPC::System::Simple> module to be installed, and enabling
224 C<system> or C<exec> will invalidate their exotic forms. See L</BUGS>
225 below for more details.
226
227 The syntax:
228
229 use autodie qw(:1.994);
230
231 allows the C<:default> list from a particular version to be used. This
232 provides the convenience of using the default methods, but the surety
233 that no behavioral changes will occur if the C<autodie> module is
234 upgraded.
235
236 C<autodie> can be enabled for all of Perl's built-ins, including
237 C<system> and C<exec> with:
238
239 use autodie qw(:all);
240
241 =head1 FUNCTION SPECIFIC NOTES
242
243 =head2 print
244
245 The autodie pragma B<does not check calls to C<print>Z<>>.
246
247 =head2 flock
248
249 It is not considered an error for C<flock> to return false if it fails
250 due to an C<EWOULDBLOCK> (or equivalent) condition. This means one can
251 still use the common convention of testing the return value of
252 C<flock> when called with the C<LOCK_NB> option:
253
254 use autodie;
255
256 if ( flock($fh, LOCK_EX | LOCK_NB) ) {
257 # We have a lock
258 }
259
260 Autodying C<flock> will generate an exception if C<flock> returns
261 false with any other error.
262
263 =head2 system/exec
264
265 The C<system> built-in is considered to have failed in the following
266 circumstances:
267
268 =over 4
269
270 =item *
271
272 The command does not start.
273
274 =item *
275
276 The command is killed by a signal.
277
278 =item *
279
280 The command returns a non-zero exit value (but see below).
281
282 =back
283
284 On success, the autodying form of C<system> returns the I<exit value>
285 rather than the contents of C<$?>.
286
287 Additional allowable exit values can be supplied as an optional first
288 argument to autodying C<system>:
289
290 system( [ 0, 1, 2 ], $cmd, @args); # 0,1,2 are good exit values
291
292 C<autodie> uses the L<IPC::System::Simple> module to change C<system>.
293 See its documentation for further information.
294
295 Applying C<autodie> to C<system> or C<exec> causes the exotic
296 forms C<system { $cmd } @args > or C<exec { $cmd } @args>
297 to be considered a syntax error until the end of the lexical scope.
298 If you really need to use the exotic form, you can call C<CORE::system>
299 or C<CORE::exec> instead, or use C<no autodie qw(system exec)> before
300 calling the exotic form.
301
302 =head1 GOTCHAS
303
304 Functions called in list context are assumed to have failed if they
305 return an empty list, or a list consisting only of a single undef
306 element.
307
308 Some builtins (e.g. C<chdir> or C<truncate>) has a call signature that
309 cannot completely be represented with a Perl prototype. This means
310 that some valid Perl code will be invalid under autodie. As an example:
311
312 chdir(BAREWORD);
313
314 Without autodie (and assuming BAREWORD is an open
315 filehandle/dirhandle) this is a valid call to chdir. But under
316 autodie, C<chdir> will behave like it had the prototype ";$" and thus
317 BAREWORD will be a syntax error (under "use strict". Without strict, it
318 will interpreted as a filename).
319
320 =head1 DIAGNOSTICS
321
322 =over 4
323
324 =item :void cannot be used with lexical scope
325
326 The C<:void> option is supported in L<Fatal>, but not
327 C<autodie>. To workaround this, C<autodie> may be explicitly disabled until
328 the end of the current block with C<no autodie>.
329 To disable autodie for only a single function (eg, open)
330 use C<no autodie qw(open)>.
331
332 C<autodie> performs no checking of called context to determine whether to throw
333 an exception; the explicitness of error handling with C<autodie> is a deliberate
334 feature.
335
336 =item No user hints defined for %s
337
338 You've insisted on hints for user-subroutines, either by pre-pending
339 a C<!> to the subroutine name itself, or earlier in the list of arguments
340 to C<autodie>. However the subroutine in question does not have
341 any hints available.
342
343 =back
344
345 See also L<Fatal/DIAGNOSTICS>.
346
347 =head1 Tips and Tricks
348
349 =head2 Importing autodie into another namespace than "caller"
350
351 It is possible to import autodie into a different namespace by using
352 L<Import::Into>. However, you have to pass a "caller depth" (rather
353 than a package name) for this to work correctly.
354
355 =head1 BUGS
356
357 "Used only once" warnings can be generated when C<autodie> or C<Fatal>
358 is used with package filehandles (eg, C<FILE>). Scalar filehandles are
359 strongly recommended instead.
360
361 When using C<autodie> or C<Fatal> with user subroutines, the
362 declaration of those subroutines must appear before the first use of
363 C<Fatal> or C<autodie>, or have been exported from a module.
364 Attempting to use C<Fatal> or C<autodie> on other user subroutines will
365 result in a compile-time error.
366
367 Due to a bug in Perl, C<autodie> may "lose" any format which has the
368 same name as an autodying built-in or function.
369
370 C<autodie> may not work correctly if used inside a file with a
371 name that looks like a string eval, such as F<eval (3)>.
372
373 =head2 autodie and string eval
374
375 Due to the current implementation of C<autodie>, unexpected results
376 may be seen when used near or with the string version of eval.
377 I<None of these bugs exist when using block eval>.
378
379 Under Perl 5.8 only, C<autodie> I<does not> propagate into string C<eval>
380 statements, although it can be explicitly enabled inside a string
381 C<eval>.
382
383 Under Perl 5.10 only, using a string eval when C<autodie> is in
384 effect can cause the autodie behaviour to leak into the surrounding
385 scope. This can be worked around by using a C<no autodie> at the
386 end of the scope to explicitly remove autodie's effects, or by
387 avoiding the use of string eval.
388
389 I<None of these bugs exist when using block eval>. The use of
390 C<autodie> with block eval is considered good practice.
391
392 =head2 REPORTING BUGS
393
394 Please report bugs via the GitHub Issue Tracker at
395 L<https://github.com/pjf/autodie/issues>.
396
397 =head1 FEEDBACK
398
399 If you find this module useful, please consider rating it on metacpan at
400 L<https://metacpan.org/pod/autodie> .
401
402 The module author loves to hear how C<autodie> has made your life
403 better (or worse). Feedback can be sent to
404 E<lt>[email protected]<gt>.
405
406 =head1 AUTHOR
407
408 Copyright 2008-2009, Paul Fenwick E<lt>[email protected]<gt>
409
410 =head1 LICENSE
411
412 This module is free software. You may distribute it under the
413 same terms as Perl itself.
414
415 =head1 SEE ALSO
416
417 L<Fatal>, L<autodie::exception>, L<autodie::hints>, L<IPC::System::Simple>
418
419 I<Perl tips, autodie> at
420 L<http://perltraining.com.au/tips/2008-08-20.html>
421
422 =head1 ACKNOWLEDGEMENTS
423
424 Mark Reed and Roland Giersig -- Klingon translators.
425
426 See the F<AUTHORS> file for full credits. The latest version of this
427 file can be found at
428 L<https://github.com/pjf/autodie/tree/master/AUTHORS> .
429
430 =cut