File Coverage

blib/lib/autodie/exception.pm
Criterion Covered Total %
statement 191 225 84.8
branch 51 80 63.7
condition 13 25 52.0
subroutine 34 35 97.1
pod 16 16 100.0
total 305 381 80.0


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