File Coverage

blib/lib/Log/Report/Die.pm
Criterion Covered Total %
statement 37 38 97.3
branch 15 18 83.3
condition 4 5 80.0
subroutine 6 6 100.0
pod 1 1 100.0
total 63 68 92.6


line stmt bran cond sub pod time code
1             # Copyrights 2007-2017 by [Mark Overmeer].
2             # For other contributors see ChangeLog.
3             # See the manual pages for details on the licensing terms.
4             # Pod stripped from pm file by OODoc 2.02.
5 13     13   8673 use warnings;
  13         24  
  13         415  
6 13     13   59 use strict;
  13         37  
  13         348  
7              
8             package Log::Report::Die;
9 13     13   61 use vars '$VERSION';
  13         51  
  13         610  
10             $VERSION = '1.22';
11              
12 13     13   63 use base 'Exporter';
  13         21  
  13         1499  
13              
14             our @EXPORT = qw/die_decode/;
15              
16 13     13   69 use POSIX qw/locale_h/;
  13         18  
  13         84  
17              
18              
19             sub die_decode($%)
20 18     18 1 6284 { my ($text, %args) = @_;
21              
22 18         56 my @text = split /\n/, $text;
23 18 50       40 @text or return ();
24 18         27 chomp $text[-1];
25              
26             # Try to catch the error directly, to remove it from the error text
27 18         82 my %opt = (errno => $! + 0);
28 18         43 my $err = "$!";
29              
30 18         27 my $dietxt = $text[0];
31 18 100 66     131 if($text[0] =~ s/ at (.+) line (\d+)\.?$// )
    100          
32 11         42 { $opt{location} = [undef, $1, $2, undef];
33             }
34             elsif(@text > 1 && $text[1] =~ m/^\s*at (.+) line (\d+)\.?$/ )
35             { # sometimes people carp/confess with \n, folding the line
36 4         13 $opt{location} = [undef, $1, $2, undef];
37 4         8 splice @text, 1, 1;
38             }
39              
40             $text[0] =~ s/\s*[.:;]?\s*$err\s*$// # the $err is translation sensitive
41 18 100       186 or delete $opt{errno};
42              
43 18         36 my @msg = shift @text;
44 18 50       33 length $msg[0] or $msg[0] = 'stopped';
45              
46 18         22 my @stack;
47 18         36 foreach (@text)
48 15 50       69 { if(m/^\s*(.*?)\s+called at (.*?) line (\d+)\s*$/)
49 15         46 { push @stack, [ $1, $2, $3 ] }
50 0         0 else { push @msg, $_ }
51             }
52 18         24 $opt{stack} = \@stack;
53 18 100       48 $opt{classes} = [ 'perl', (@stack ? 'confess' : 'die') ];
54              
55             my $reason
56             = $opt{errno} ? 'FAULT'
57             : @stack ? 'PANIC'
58 18 100 100     56 : $args{on_die} || 'ERROR';
    100          
59              
60 18         89 ($dietxt, \%opt, $reason, join("\n", @msg));
61             }
62              
63             "to die or not to die, that's the question";