autodie/exception.pm

filter: Statementstmt 86.3% 202 / 234 Branchbran 65.8% 54 / 82 Conditioncond 53.5% 15 / 28 MC/DCmcdc 33.3% 6 / 18 Subroutinesub 97.2% 35 / 36 Podpod 100% 16 / 16 totaltotal 79.2% 328 / 414 CC 53 SCAR 20.8
CC
53
Cov
79%
_format_flock
42.0
_init
13.1
matches
8.8
CRAP 8.0
« Fatal.pm ↑ Summary autodie/hints.pm »
1 package autodie::exception;
2 42 ▶use 5.008;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 42 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 42 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 42 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 42 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 42 our $VERSION = '2.37'; # VERSION: Generated by DZP::OurPkg:Version
10 # ABSTRACT: Exceptions from autodying functions.
11  
12 42 our $DEBUG = 0;
13  
14 use overload
15 42 ▶ q{""} => "stringify",
Subroutine BEGIN: called CC=3 SCAR=12.5
Branch: 1 branch
BranchTrueFalseTotal
$] >= 5.01 && $] < 5.041 ? : 0 42 42
Condition33%1/3
$] >= "5.01" and $] < "5.041"
!ll&&!rl&&r
0420
MC/DC0%0/2
$] >= "5.01" and $] < "5.041"
$] >= "5.01" $] < "5.041"
Truth table33%1/3
A and B
A= $] >= "5.01"B= $] < "5.041"
ABresult
0X0
100
111
16 # Overload smart-match only if we're using 5.10 or up
17 (( $] >= 5.010 && $] < 5.041 ) ? ('~~' => "matches") : ()),
18 fallback => 1,
19 42 ;
20  
21 42 my $PACKAGE = __PACKAGE__; # Useful to have a scalar for hash keys.
22  
23 =head1 NAME
24  
25 autodie::exception - Exceptions from autodying functions.
26  
27 =head1 SYNOPSIS
28  
29 eval {
30 use autodie;
31  
32 open(my $fh, '<', 'some_file.txt');
33  
34 ...
35 };
36  
37 if (my $E = $@) {
38 say "Ooops! ",$E->caller," had problems: $@";
39 }
40  
41  
42 =head1 DESCRIPTION
43  
44 When an L<autodie> enabled function fails, it generates an
45 C<autodie::exception> object. This can be interrogated to
46 determine further information about the error that occurred.
47  
48 This document is broken into two sections; those methods that
49 are most useful to the end-developer, and those methods for
50 anyone wishing to subclass or get very familiar with
51 C<autodie::exception>.
52  
53 =head2 Common Methods
54  
55 These methods are intended to be used in the everyday dealing
56 of exceptions.
57  
58 The following assume that the error has been copied into
59 a separate scalar:
60  
61 if ($E = $@) {
62 ...
63 }
64  
65 This is not required, but is recommended in case any code
66 is called which may reset or alter C<$@>.
67  
68 =cut
69  
70 =head3 args
71  
72 my $array_ref = $E->args;
73  
74 Provides a reference to the arguments passed to the subroutine
75 that died.
76  
77 =cut
78  
79 91 ▶sub args { return $_[0]->{$PACKAGE}{args}; }
Subroutine args: called CC=1 SCAR=0.0
Pod documented
80  
81 =head3 function
82  
83 my $sub = $E->function;
84  
85 The subroutine (including package) that threw the exception.
86  
87 =cut
88  
89 141 ▶sub function { return $_[0]->{$PACKAGE}{function}; }
Subroutine function: called CC=1 SCAR=0.0
Pod documented
90  
91 =head3 file
92  
93 my $file = $E->file;
94  
95 The file in which the error occurred (eg, C<myscript.pl> or
96 C<MyTest.pm>).
97  
98 =cut
99  
100 98 ▶sub file { return $_[0]->{$PACKAGE}{file}; }
Subroutine file: called CC=1 SCAR=0.0
Pod documented
101  
102 =head3 package
103  
104 my $package = $E->package;
105  
106 The package from which the exceptional subroutine was called.
107  
108 =cut
109  
110 4 ▶sub package { return $_[0]->{$PACKAGE}{package}; }
Subroutine package: called CC=1 SCAR=0.0
Pod documented
111  
112 =head3 caller
113  
114 my $caller = $E->caller;
115  
116 The subroutine that I<called> the exceptional code.
117  
118 =cut
119  
120 4 ▶sub caller { return $_[0]->{$PACKAGE}{caller}; }
Subroutine caller: called CC=1 SCAR=0.0
Pod documented
121  
122 =head3 line
123  
124 my $line = $E->line;
125  
126 The line in C<< $E->file >> where the exceptional code was called.
127  
128 =cut
129  
130 98 ▶sub line { return $_[0]->{$PACKAGE}{line}; }
Subroutine line: called CC=1 SCAR=0.0
Pod documented
131  
132 =head3 context
133  
134 my $context = $E->context;
135  
136 The context in which the subroutine was called by autodie; usually
137 the same as the context in which you called the autodying subroutine.
138 This can be 'list', 'scalar', or undefined (unknown). It will never
139 be 'void', as C<autodie> always captures the return value in one way
140 or another.
141  
142 For some core functions that always return a scalar value regardless
143 of their context (eg, C<chown>), this may be 'scalar', even if you
144 used a list context.
145  
146 =cut
147  
148 # TODO: The comments above say this can be undefined. Is that actually
149 # the case? (With 'system', perhaps?)
150  
151 8 ▶sub context { return $_[0]->{$PACKAGE}{context} }
Subroutine context: called CC=1 SCAR=0.0
Pod documented
152  
153 =head3 return
154  
155 my $return_value = $E->return;
156  
157 The value(s) returned by the failed subroutine. When the subroutine
158 was called in a list context, this will always be a reference to an
159 array containing the results. When the subroutine was called in
160 a scalar context, this will be the actual scalar returned.
161  
162 =cut
163  
164 11 ▶sub return { return $_[0]->{$PACKAGE}{return} }
Subroutine return: called CC=1 SCAR=0.0
Pod documented
165  
166 =head3 errno
167  
168 my $errno = $E->errno;
169  
170 The value of C<$!> at the time when the exception occurred.
171  
172 B<NOTE>: This method will leave the main C<autodie::exception> class
173 and become part of a role in the future. You should only call
174 C<errno> for exceptions where C<$!> would reasonably have been
175 set on failure.
176  
177 =cut
178  
179 # TODO: Make errno part of a role. It doesn't make sense for
180 # everything.
181  
182 87 ▶sub errno { return $_[0]->{$PACKAGE}{errno}; }
Subroutine errno: called CC=1 SCAR=0.0
Pod documented
183  
184 =head3 eval_error
185  
186 my $old_eval_error = $E->eval_error;
187  
188 The contents of C<$@> immediately after autodie triggered an
189 exception. This may be useful when dealing with modules such
190 as L<Text::Balanced> that set (but do not throw) C<$@> on error.
191  
192 =cut
193  
194 1 ▶sub eval_error { return $_[0]->{$PACKAGE}{eval_error}; }
Subroutine eval_error: called CC=1 SCAR=0.0
Pod documented
195  
196 =head3 matches
197  
198 if ( $e->matches('open') ) { ... }
199  
200 if ( 'open' ~~ $e ) { ... }
201  
202 C<matches> is used to determine whether a
203 given exception matches a particular role.
204  
205 An exception is considered to match a string if:
206  
207 =over 4
208  
209 =item *
210  
211 For a string not starting with a colon, the string exactly matches the
212 package and subroutine that threw the exception. For example,
213 C<MyModule::log>. If the string does not contain a package name,
214 C<CORE::> is assumed.
215  
216 =item *
217  
218 For a string that does start with a colon, if the subroutine
219 throwing the exception I<does> that behaviour. For example, the
220 C<CORE::open> subroutine does C<:file>, C<:io> and C<:all>.
221  
222 See L<autodie/CATEGORIES> for further information.
223  
224 On Perl 5.10 and above, using smart-match (C<~~>) with an
225 C<autodie::exception> object will use C<matches> underneath. This module
226 used to recommend using smart-match with the exception object on the left
227 hand side, but in future Perls that is likely to stop working.
228 The smart-match facility of this class should only be used with the
229 exception object on the right hand side. Having the exception object on
230 the right is both future-proof and portable to older Perls, back to 5.10.
231 Beware that this facility can only
232 be relied upon when it is certain that the exception object actually is
233 an C<autodie::exception> object; it is no more capable than an explicit
234 call to the C<matches> method.
235  
236 =back
237  
238 =cut
239  
240 {
241 42 my (%cache);
242  
243 16 ▶ sub matches {
Subroutine matches: called CC=8 SCAR=21.8
Pod documented
244 16 my ($this, $that) = @_;
245  
246 # TODO - Handle references
247 16 ▶ croak "UNIMPLEMENTED" if ref $that;
Branch: 1 branch
BranchTrueFalseTotal
if ref $that 0 16 16
248  
249 16 my $sub = $this->function;
250  
251 16 ▶ if ($DEBUG) {
Branch: 1 branch
BranchTrueFalseTotal
if ($autodie::exception::DEBUG) 0 16 16
252 0 my $sub2 = $this->function;
253 0 warn "Smart-matching $that against $sub / $sub2\n";
254 }
255  
256 # Direct subname match.
257 16 ▶ return 1 if $that eq $sub;
Branch: 1 branch
BranchTrueFalseTotal
if $that eq $sub 0 16 16
258 16 ▶ return 1 if $that !~ /:/ and "CORE::$that" eq $sub;
Branch: 1 branch
BranchTrueFalseTotal
if not $that =~ /:/ and "CORE::$that" eq $sub 10 6 16
Condition66%2/3
not $that =~ /:/ and "CORE::$that" eq $sub
!ll&&!rl&&r
6010
MC/DC50%1/2
not $that =~ /:/ and "CORE::$that" eq $sub
not $that =~ /:/ "CORE::$that" eq $sub
Truth table66%2/3
not(A) and B
A= not $that =~ /:/B= "CORE::$that" eq $sub
ABresult
0X0
100
111
259 6 ▶ return 0 if $that !~ /^:/;
Branch: 1 branch
BranchTrueFalseTotal
unless $that =~ /^:/ 0 6 6
260  
261 # Cached match / check tags.
262 6 require Fatal;
263  
264 6 ▶ if (exists $cache{$sub}{$that}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $cache{$sub}{$that}) 1 5 6
265 1 return $cache{$sub}{$that};
266 }
267  
268 # This rather awful looking line checks to see if our sub is in the
269 # list of expanded tags, caches it, and returns the result.
270  
271 5 return $cache{$sub}{$that} = grep { $_ eq $sub } @{ $this->_expand_tag($that) };
272 }
273 }
274  
275 # This exists primarily so that child classes can override or
276 # augment it if they wish.
277  
278 42 ▶sub _expand_tag {
Subroutine _expand_tag: called CC=1 SCAR=0.0
279 5 my ($this, @args) = @_;
280  
281 5 return Fatal->_expand_tag(@args);
282 }
283  
284 =head2 Advanced methods
285  
286 The following methods, while usable from anywhere, are primarily
287 intended for developers wishing to subclass C<autodie::exception>,
288 write code that registers custom error messages, or otherwise
289 work closely with the C<autodie::exception> model.
290  
291 =cut
292  
293 # The table below records customer formatters.
294 # TODO - Should this be a package var instead?
295 # TODO - Should these be in a completely different file, or
296 # perhaps loaded on demand? Most formatters will never
297 # get used in most programs.
298  
299 42 my %formatter_of = (
300 'CORE::close' => \&_format_close,
301 'CORE::open' => \&_format_open,
302 'CORE::dbmopen' => \&_format_dbmopen,
303 'CORE::flock' => \&_format_flock,
304 'CORE::read' => \&_format_readwrite,
305 'CORE::sysread' => \&_format_readwrite,
306 'CORE::syswrite' => \&_format_readwrite,
307 'CORE::chmod' => \&_format_chmod,
308 'CORE::mkdir' => \&_format_mkdir,
309 );
310  
311 30 ▶sub _beautify_arguments {
Subroutine _beautify_arguments: called CC=4 SCAR=13.9
312 30 shift @_;
313  
314 # Walk through all our arguments, and...
315 #
316 # * Replace undef with the word 'undef'
317 # * Replace globs with the string '$fh'
318 # * Quote all other args.
319 30 foreach my $arg (@_) {
320 31 ▶ if (not defined($arg)) { $arg = 'undef' }
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $arg) { } 3 28 31
elsif (ref $arg eq 'GLOB') { } 2 26 28
321 2 elsif (ref($arg) eq "GLOB") { $arg = '$fh' }
322 26 else { $arg = qq{'$arg'} }
323 }
324  
325 30 return @_;
326 }
327  
328 31 ▶sub _trim_package_name {
Subroutine _trim_package_name: called CC=1 SCAR=0.0
329 # Info: The following is done since 05/2008 (which is before v1.10)
330  
331 # TODO: This is probably a good idea for CORE, is it
332 # a good idea for other subs?
333  
334 # Trim package name off dying sub for error messages
335 31 (my $name = $_[1]) =~ s/.*:://;
336 31 return $name;
337 }
338  
339 # Returns the parameter formatted as octal number
340 11 ▶sub _octalize_number {
Subroutine _octalize_number: called CC=2 SCAR=7.0
341 11 my $number = $_[1];
342  
343 # Only reformat if it looks like a whole number
344 11 ▶ if ($number =~ /^\d+$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($number =~ /^\d+$/) 11 0 11
345 11 $number = sprintf("%#04lo", $number);
346 }
347  
348 11 return $number;
349 }
350  
351 # TODO: Our tests only check LOCK_EX | LOCK_NB is properly
352 # formatted. Try other combinations and ensure they work
353 # correctly.
354  
355 0 ▶sub _format_flock {
Subroutine _format_flock: not called CC=6 SCAR=37.4
356 0 my ($this) = @_;
357  
358 0 require Fcntl;
359  
360 0 my $filehandle = $this->args->[0];
361 0 my $raw_mode = $this->args->[1];
362  
363 0 my $mode_type;
364 my $lock_unlock;
365  
366 0 ▶ if ($raw_mode & Fcntl::LOCK_EX() ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($raw_mode & 2) { } 0 0 0
elsif ($raw_mode & 1) { } 0 0 0
elsif ($raw_mode & 8) { } 0 0 0
367 0 $lock_unlock = "lock";
368 0 $mode_type = "for exclusive access";
369 }
370 elsif ($raw_mode & Fcntl::LOCK_SH() ) {
371 0 $lock_unlock = "lock";
372 0 $mode_type = "for shared access";
373 }
374 elsif ($raw_mode & Fcntl::LOCK_UN() ) {
375 0 $lock_unlock = "unlock";
376 0 $mode_type = "";
377 }
378 else {
379 # I've got no idea what they're trying to do.
380 0 $lock_unlock = "lock";
381 0 $mode_type = "with mode $raw_mode";
382 }
383  
384 0 my $cooked_filehandle;
385  
386 0 ▶ if ($filehandle and not ref $filehandle) {
Branch: 1 branch
BranchTrueFalseTotal
if ($filehandle and not ref $filehandle) { } 0 0 0
Condition0%0/3
$filehandle and not ref $filehandle
!ll&&!rl&&r
000
MC/DC0%0/2
$filehandle and not ref $filehandle
$filehandle not ref $filehandle
Truth table0%0/3
A and not(B)
A= $filehandleB= not ref $filehandle
ABresult
0X0
100
111
387  
388 # A package filehandle with a name!
389  
390 0 $cooked_filehandle = " $filehandle";
391 }
392 else {
393 # Otherwise we have a scalar filehandle.
394  
395 0 $cooked_filehandle = '';
396  
397 }
398  
399 0 local $! = $this->errno;
400  
401 0 return "Can't $lock_unlock filehandle$cooked_filehandle $mode_type: $!";
402  
403 }
404  
405 # Default formatter for CORE::chmod
406 2 ▶sub _format_chmod {
Subroutine _format_chmod: called CC=1 SCAR=0.0
407 2 my ($this) = @_;
408 2 my @args = @{$this->args};
409  
410 2 my $mode = shift @args;
411 2 local $! = $this->errno;
412  
413 2 $mode = $this->_octalize_number($mode);
414  
415 2 @args = $this->_beautify_arguments(@args);
416  
417 2 return "Can't chmod($mode, ". join(q{, }, @args) ."): $!";
418 }
419  
420 # Default formatter for CORE::mkdir
421 4 ▶sub _format_mkdir {
Subroutine _format_mkdir: called CC=2 SCAR=7.0
422 4 my ($this) = @_;
423 4 my @args = @{$this->args};
424  
425 # If no mask is specified use default formatter
426 4 ▶ if (@args < 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (@args < 2) 0 4 4
427 0 return $this->format_default;
428 }
429  
430 4 my $file = $args[0];
431 4 my $mask = $args[1];
432 4 local $! = $this->errno;
433  
434 4 $mask = $this->_octalize_number($mask);
435  
436 4 return "Can't mkdir('$file', $mask): '$!'";
437 }
438  
439 # Default formatter for CORE::dbmopen
440 5 ▶sub _format_dbmopen {
Subroutine _format_dbmopen: called CC=1 SCAR=0.0
441 5 my ($this) = @_;
442 5 my @args = @{$this->args};
443  
444 # TODO: Presently, $args flattens out the (usually empty) hash
445 # which is passed as the first argument to dbmopen. This is
446 # a bug in our args handling code (taking a reference to it would
447 # be better), but for the moment we'll just examine the end of
448 # our arguments list for message formatting.
449  
450 5 my $mode = $args[-1];
451 5 my $file = $args[-2];
452  
453 5 $mode = $this->_octalize_number($mode);
454  
455 5 local $! = $this->errno;
456  
457 5 return "Can't dbmopen(%hash, '$file', $mode): '$!'";
458 }
459  
460 # Default formatter for CORE::close
461  
462 6 ▶sub _format_close {
Subroutine _format_close: called CC=3 SCAR=12.3
463 6 my ($this) = @_;
464 6 my $close_arg = $this->args->[0];
465  
466 6 local $! = $this->errno;
467  
468 # If we've got an old-style filehandle, mention it.
469 6 ▶ if ($close_arg and not ref $close_arg) {
Branch: 1 branch
BranchTrueFalseTotal
if ($close_arg and not ref $close_arg) 6 0 6
Condition33%1/3
$close_arg and not ref $close_arg
!ll&&!rl&&r
006
MC/DC0%0/2
$close_arg and not ref $close_arg
$close_arg not ref $close_arg
Truth table33%1/3
A and not(B)
A= $close_argB= not ref $close_arg
ABresult
0X0
100
111
470 6 return "Can't close filehandle '$close_arg': '$!'";
471 }
472  
473 # TODO - This will probably produce an ugly error. Test and fix.
474 0 return "Can't close($close_arg) filehandle: '$!'";
475  
476 }
477  
478 # Default formatter for CORE::read, CORE::sysread and CORE::syswrite
479 #
480 # Similar to default formatter with the buffer filtered out as it
481 # may contain binary data.
482 3 ▶sub _format_readwrite {
Subroutine _format_readwrite: called CC=4 SCAR=14.1
483 3 my ($this) = @_;
484 3 my $call = $this->_trim_package_name($this->function);
485 3 local $! = $this->errno;
486  
487 # These subs receive the following arguments (in order):
488 #
489 # * FILEHANDLE
490 # * SCALAR (buffer, we do not want to write this)
491 # * LENGTH (optional for syswrite)
492 # * OFFSET (optional for all)
493 3 my (@args) = @{$this->args};
494 3 my $arg_name = $args[1];
495 3 ▶ if (defined($arg_name)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $arg_name) { } 3 0 3
496 3 ▶ if (ref($arg_name)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $arg_name) { } 1 2 3
497 1 ▶ my $name = blessed($arg_name) || ref($arg_name);
Condition33%1/3
builtin::blessed($arg_name) || ref $arg_name
l!l&&r!l&&!r
010
MC/DC0%0/2
builtin::blessed($arg_name) || ref $arg_name
builtin::blessed($arg_name) ref $arg_name
Truth table33%1/3
A || B
A= builtin::blessed($arg_name)B= ref $arg_name
ABresult
1X1
011
000
498 1 $arg_name = "<${name}>";
499 } else {
500 2 $arg_name = '<BUFFER>';
501 }
502 } else {
503 0 $arg_name = '<UNDEF>';
504 }
505 3 $args[1] = $arg_name;
506  
507 3 return "Can't $call(" . join(q{, }, @args) . "): $!";
508 }
509  
510 # Default formatter for CORE::open
511  
512 42 ▶use constant _FORMAT_OPEN => "Can't open '%s' for %s: '%s'";
Subroutine BEGIN: called CC=1 SCAR=0.0
513  
514 38 ▶sub _format_open_with_mode {
Subroutine _format_open_with_mode: called CC=6 SCAR=19.0
515 38 my ($this, $mode, $file, $error) = @_;
516  
517 38 my $wordy_mode;
518  
519 38 ▶ if ($mode eq '<') { $wordy_mode = 'reading'; }
Branch: 3 branches
BranchTrueFalseTotal
if ($mode eq '<') { } 35 3 38
elsif ($mode eq '>') { } 0 3 3
elsif ($mode eq '>>') { } 0 3 3
520 0 elsif ($mode eq '>') { $wordy_mode = 'writing'; }
521 0 elsif ($mode eq '>>') { $wordy_mode = 'appending'; }
522  
523 38 ▶ $file = '<undef>' if not defined $file;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $file 0 38 38
524  
525 38 ▶ return sprintf _FORMAT_OPEN, $file, $wordy_mode, $error if $wordy_mode;
Branch: 1 branch
BranchTrueFalseTotal
if $wordy_mode 35 3 38
526  
527 3 Carp::confess("Internal autodie::exception error: Don't know how to format mode '$mode'.");
528  
529 }
530  
531 41 ▶sub _format_open {
Subroutine _format_open: called CC=8 SCAR=20.9
532 41 my ($this) = @_;
533  
534 41 my @open_args = @{$this->args};
535  
536 # Use the default formatter for single-arg and many-arg open
537 41 ▶ if (@open_args <= 1 or @open_args >= 4) {
Branch: 1 branch
BranchTrueFalseTotal
if (@open_args <= 1 or @open_args >= 4) 3 38 41
Condition66%2/3
@open_args <= 1 or @open_args >= 4
l!l&&r!l&&!r
3038
MC/DC50%1/2
@open_args <= 1 or @open_args >= 4
@open_args <= 1 @open_args >= 4
Truth table66%2/3
A or B
A= @open_args <= 1B= @open_args >= 4
ABresult
1X1
011
000
538 3 return $this->format_default;
539 }
540  
541 # For two arg open, we have to extract the mode
542 38 ▶ if (@open_args == 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (@open_args == 2) 7 31 38
543 7 my ($fh, $file) = @open_args;
544  
545 7 ▶ if (ref($fh) eq "GLOB") {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $fh eq "GLOB") 7 0 7
546 7 $fh = '$fh';
547 }
548  
549 7 my ($mode) = $file =~ m{
550 ^\s* # Spaces before mode
551 (
552 (?> # Non-backtracking subexp.
553 < # Reading
554 |>>? # Writing/appending
555 )
556 )
557 [^&] # Not an ampersand (which means a dup)
558 }x;
559  
560 7 ▶ if (not $mode) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($mode) 4 3 7
561 # Maybe it's a 2-arg open without any mode at all?
562 # Detect the most simple case for this, where our
563 # file consists only of word characters.
564  
565 4 ▶ if ( $file =~ m{^\s*\w+\s*$} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file =~ /^\s*\w+\s*$/) { } 4 0 4
566 4 $mode = '<'
567 }
568 else {
569 # Otherwise, we've got no idea what's going on.
570 # Use the default.
571 0 return $this->format_default;
572 }
573 }
574  
575 # Localising $! means perl makes it a pretty error for us.
576 7 local $! = $this->errno;
577  
578 7 return $this->_format_open_with_mode($mode, $file, $!);
579 }
580  
581 # Here we must be using three arg open.
582  
583 31 my $file = $open_args[2];
584  
585 31 local $! = $this->errno;
586  
587 31 my $mode = $open_args[1];
588  
589 31 local $@;
590  
591 31 my $msg = eval { $this->_format_open_with_mode($mode, $file, $!); };
592  
593 31 ▶ return $msg if $msg;
Branch: 1 branch
BranchTrueFalseTotal
if $msg 28 3 31
594  
595 # Default message (for pipes and odd things)
596  
597 3 return "Can't open '$file' with mode '$open_args[1]': '$!'";
598 }
599  
600 =head3 register
601  
602 autodie::exception->register( 'CORE::open' => \&mysub );
603  
604 The C<register> method allows for the registration of a message
605 handler for a given subroutine. The full subroutine name including
606 the package should be used.
607  
608 Registered message handlers will receive the C<autodie::exception>
609 object as the first parameter.
610  
611 =cut
612  
613 1 ▶sub register {
Subroutine register: called CC=2 SCAR=7.1
Pod documented
614 1 my ($class, $symbol, $handler) = @_;
615  
616 1 ▶ croak "Incorrect call to autodie::register" if @_ != 3;
Branch: 1 branch
BranchTrueFalseTotal
if @_ != 3 0 1 1
617  
618 1 $formatter_of{$symbol} = $handler;
619  
620 }
621  
622 =head3 add_file_and_line
623  
624 say "Problem occurred",$@->add_file_and_line;
625  
626 Returns the string C< at %s line %d>, where C<%s> is replaced with
627 the filename, and C<%d> is replaced with the line number.
628  
629 Primarily intended for use by format handlers.
630  
631 =cut
632  
633 # Simply produces the file and line number; intended to be added
634 # to the end of error messages.
635  
636 95 ▶sub add_file_and_line {
Subroutine add_file_and_line: called CC=1 SCAR=0.0
Pod documented
637 95 my ($this) = @_;
638  
639 95 return sprintf(" at %s line %d\n", $this->file, $this->line);
640 }
641  
642 =head3 stringify
643  
644 say "The error was: ",$@->stringify;
645  
646 Formats the error as a human readable string. Usually there's no
647 reason to call this directly, as it is used automatically if an
648 C<autodie::exception> object is ever used as a string.
649  
650 Child classes can override this method to change how they're
651 stringified.
652  
653 =cut
654  
655 90 ▶sub stringify {
Subroutine stringify: called CC=4 SCAR=15.0
Pod documented
656 90 my ($this) = @_;
657  
658 90 my $call = $this->function;
659 90 my $msg;
660  
661 90 ▶ if ($DEBUG) {
Branch: 1 branch
BranchTrueFalseTotal
if ($autodie::exception::DEBUG) 0 90 90
662 0 my $dying_pkg = $this->package;
663 0 my $sub = $this->function;
664 0 my $caller = $this->caller;
665 0 warn "Stringifing exception for $dying_pkg :: $sub / $caller / $call\n";
666 }
667  
668 # TODO - This isn't using inheritance. Should it?
669 90 ▶ if ( my $sub = $formatter_of{$call} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $sub = $formatter_of{$call}) { } 65 25 90
670 65 $msg = $sub->($this) . $this->add_file_and_line;
671 } else {
672 25 $msg = $this->format_default . $this->add_file_and_line;
673 }
674 $msg .= $this->{$PACKAGE}{_stack_trace}
675 90 ▶ if $Carp::Verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $Carp::Verbose 0 90 90
676  
677 90 return $msg;
678 }
679  
680 =head3 format_default
681  
682 my $error_string = $E->format_default;
683  
684 This produces the default error string for the given exception,
685 I<without using any registered message handlers>. It is primarily
686 intended to be called from a message handler when they have
687 been passed an exception they don't want to format.
688  
689 Child classes can override this method to change how default
690 messages are formatted.
691  
692 =cut
693  
694 # TODO: This produces ugly errors. Is there any way we can
695 # dig around to find the actual variable names? I know perl 5.10
696 # does some dark and terrible magicks to find them for undef warnings.
697  
698 28 ▶sub format_default {
Subroutine format_default: called CC=1 SCAR=0.0
Pod documented
699 28 my ($this) = @_;
700  
701 28 my $call = $this->_trim_package_name($this->function);
702  
703 28 local $! = $this->errno;
704  
705 28 my @args = @{ $this->args() };
706 28 @args = $this->_beautify_arguments(@args);
707  
708 # Format our beautiful error.
709  
710 28 return "Can't $call(". join(q{, }, @args) . "): $!" ;
711  
712 # TODO - Handle user-defined errors from hash.
713  
714 # TODO - Handle default error messages.
715  
716 }
717  
718 =head3 new
719  
720 my $error = autodie::exception->new(
721 args => \@_,
722 function => "CORE::open",
723 errno => $!,
724 context => 'scalar',
725 return => undef,
726 );
727  
728  
729 Creates a new C<autodie::exception> object. Normally called
730 directly from an autodying function. The C<function> argument
731 is required, its the function we were trying to call that
732 generated the exception. The C<args> parameter is optional.
733  
734 The C<errno> value is optional. In versions of C<autodie::exception>
735 1.99 and earlier the code would try to automatically use the
736 current value of C<$!>, but this was unreliable and is no longer
737 supported.
738  
739 Atrributes such as package, file, and caller are determined
740 automatically, and cannot be specified.
741  
742 =cut
743  
744 96 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
745 96 my ($class, @args) = @_;
746  
747 96 my $this = {};
748  
749 96 bless($this,$class);
750  
751 # I'd love to use EVERY here, but it causes our code to die
752 # because it wants to stringify our objects before they're
753 # initialised, causing everything to explode.
754  
755 96 $this->_init(@args);
756  
757 96 return $this;
758 }
759  
760 96 ▶sub _init {
Subroutine _init: called CC=13 SCAR=25.7
761  
762 96 my ($this, %args) = @_;
763  
764 # Capturing errno here is not necessarily reliable.
765 96 my $original_errno = $!;
766  
767 96 our $init_called = 1;
768  
769 96 my $class = ref $this;
770  
771 # We're going to walk up our call stack, looking for the
772 # first thing that doesn't look like our exception
773 # code, autodie/Fatal, or some whacky eval.
774  
775 96 my ($package, $file, $line, $sub);
776  
777 96 my $depth = 0;
778  
779 96 while (1) {
780 224 $depth++;
781  
782 224 ($package, $file, $line, $sub) = CORE::caller($depth);
783  
784 # Skip up the call stack until we find something outside
785 # of the Fatal/autodie/eval space.
786  
787 224 ▶ next if $package->isa('Fatal');
Branch: 1 branch
BranchTrueFalseTotal
if $package->isa("Fatal") 124 100 224
788 100 ▶ next if $package->isa($class);
Branch: 1 branch
BranchTrueFalseTotal
if $package->isa($class) 0 100 100
789 100 ▶ next if $package->isa(__PACKAGE__);
Branch: 1 branch
BranchTrueFalseTotal
if $package->isa(__PACKAGE__) 2 98 100
790  
791 # Anything with the 'autodie::skip' role wants us to skip it.
792 # https://github.com/pjf/autodie/issues/15
793  
794 98 ▶ next if ($package->can('DOES') and $package->DOES('autodie::skip'));
Branch: 1 branch
BranchTrueFalseTotal
if $package->can("DOES") and $package->DOES("autodie::skip") 2 96 98
Condition66%2/3
$package->can("DOES") and $package->DOES("autodie::skip")
!ll&&!rl&&r
0962
MC/DC50%1/2
$package->can("DOES") and $package->DOES("autodie::skip")
$package->can("DOES") $package->DOES("autodie::skip")
Truth table66%2/3
A and B
A= $package->can("DOES")B= $package->DOES("autodie::skip")
ABresult
0X0
100
111
795  
796 96 ▶ next if $file =~ /^\(eval\s\d+\)$/;
Branch: 1 branch
BranchTrueFalseTotal
if $file =~ /^\(eval\s\d+\)$/ 0 96 96
797  
798 96 last;
799  
800 }
801  
802 # We now have everything correct, *except* for our subroutine
803 # name. If it's __ANON__ or (eval), then we need to keep on
804 # digging deeper into our stack to find the real name. However we
805 # don't update our other information, since that will be correct
806 # for our current exception.
807  
808 96 my $first_guess_subroutine = $sub;
809  
810 96 ▶ while (defined $sub and $sub =~ /^\(eval\)$|::__ANON__$/) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $sub and $sub =~ /^\(eval\)$|::__ANON__$/) 59 96 155
Condition100%3/3
defined $sub and $sub =~ /^\(eval\)$|::__ANON__$/
!ll&&!rl&&r
296759
MC/DC100%2/2
defined $sub and $sub =~ /^\(eval\)$|::__ANON__$/
defined $sub $sub =~ /^\(eval\)$|::__ANON__$/
Truth table100%3/3
A and B
A= defined $subB= $sub =~ /^\(eval\)$|::__ANON__$/
ABresult
0X0
100
111
811 59 $depth++;
812  
813 59 $sub = (CORE::caller($depth))[3];
814 }
815  
816 # If we end up falling out the bottom of our stack, then our
817 # __ANON__ guess is the best we can get. This includes situations
818 # where we were called from the top level of a program.
819  
820 96 ▶ if (not defined $sub) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $sub) 29 67 96
821 29 $sub = $first_guess_subroutine;
822 }
823  
824 96 $this->{$PACKAGE}{package} = $package;
825 96 $this->{$PACKAGE}{file} = $file;
826 96 $this->{$PACKAGE}{line} = $line;
827 96 $this->{$PACKAGE}{caller} = $sub;
828  
829 # Tranks to %Carp::CarpInternal all Fatal, autodie and
830 # autodie::exception stack frames are filtered already, but our
831 # nameless wrapper is still present, so strip that.
832  
833 96 my $trace = Carp::longmess();
834 96 $trace =~ s/^\s*at \(eval[^\n]+\n//;
835  
836 # And if we see an __ANON__, then we'll replace that with the actual
837 # name of our autodying function.
838  
839 96 my $short_func = $args{function};
840 96 $short_func =~ s/^CORE:://;
841 96 $trace =~ s/(\s*[\w:]+)__ANON__/$1$short_func/;
842  
843 # And now we just fill in all our attributes.
844  
845 96 $this->{$PACKAGE}{_stack_trace} = $trace;
846  
847 96 ▶ $this->{$PACKAGE}{errno} = $args{errno} || 0;
Condition100%2/2
$args{'errno'} || 0
l!l
8115
MC/DC100%1/1
$args{'errno'} || 0
$args{'errno'}
Truth table100%2/2
A
A= $args{'errno'}
Aresult
11
00
848  
849 96 $this->{$PACKAGE}{context} = $args{context};
850 96 $this->{$PACKAGE}{return} = $args{return};
851 96 $this->{$PACKAGE}{eval_error} = $args{eval_error};
852  
853 96 ▶ $this->{$PACKAGE}{args} = $args{args} || [];
Condition50%1/2
$args{'args'} || []
l!l
960
MC/DC0%0/1
$args{'args'} || []
$args{'args'}
Truth table50%1/2
A
A= $args{'args'}
Aresult
11
00
854 $this->{$PACKAGE}{function}= $args{function} or
855 96 ▶ croak("$class->new() called without function arg");
Branch: 1 branch
BranchTrueFalseTotal
$this->{$PACKAGE}{"function"} = $args{"function"} or croak("$class->new() called without function arg") 96 0 96
856  
857 96 return $this;
858  
859 }
860  
861 42 1;
862  
863 __END__
« Fatal.pm ↑ Summary autodie/hints.pm »