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   9187 use warnings;
  13         28  
  13         483  
6 13     13   70 use strict;
  13         25  
  13         369  
7              
8             package Log::Report::Die;
9 13     13   61 use vars '$VERSION';
  13         22  
  13         630  
10             $VERSION = '1.21';
11              
12 13     13   66 use base 'Exporter';
  13         26  
  13         1199  
13              
14             our @EXPORT = qw/die_decode/;
15              
16 13     13   72 use POSIX qw/locale_h/;
  13         25  
  13         85  
17              
18              
19             sub die_decode($%)
20 18     18 1 6490 { my ($text, %args) = @_;
21              
22 18         68 my @text = split /\n/, $text;
23 18 50       51 @text or return ();
24 18         42 chomp $text[-1];
25              
26             # Try to catch the error directly, to remove it from the error text
27 18         93 my %opt = (errno => $! + 0);
28 18         52 my $err = "$!";
29              
30 18         32 my $dietxt = $text[0];
31 18 100 66     147 if($text[0] =~ s/ at (.+) line (\d+)\.?$// )
    100          
32 11         49 { $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         19 $opt{location} = [undef, $1, $2, undef];
37 4         11 splice @text, 1, 1;
38             }
39              
40             $text[0] =~ s/\s*[.:;]?\s*$err\s*$// # the $err is translation sensitive
41 18 100       227 or delete $opt{errno};
42              
43 18         53 my @msg = shift @text;
44 18 50       54 length $msg[0] or $msg[0] = 'stopped';
45              
46 18         26 my @stack;
47 18         38 foreach (@text)
48 15 50       72 { if(m/^\s*(.*?)\s+called at (.*?) line (\d+)\s*$/)
49 15         59 { push @stack, [ $1, $2, $3 ] }
50 0         0 else { push @msg, $_ }
51             }
52 18         38 $opt{stack} = \@stack;
53 18 100       62 $opt{classes} = [ 'perl', (@stack ? 'confess' : 'die') ];
54              
55             my $reason
56             = $opt{errno} ? 'FAULT'
57             : @stack ? 'PANIC'
58 18 100 100     68 : $args{on_die} || 'ERROR';
    100          
59              
60 18         109 ($dietxt, \%opt, $reason, join("\n", @msg));
61             }
62              
63             "to die or not to die, that's the question";