blib/lib/auto/share/dist/Cmd-Dwarf/app/lib/Dwarf/Plugin/Devel/StackTrace.pm untested

filter: Statementstmt 0.0% 0 / 48 Branchbran 0.0% 0 / 10 Conditioncond 0.0% 0 / 12 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 0.0% 0 / 6 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 85 CC 6 SCAR 27.4
CC
6
Cov
0%
_build_context
30.0
_build_stacktrace
6.0
init
2.0
CRAP 15.5
1 package Dwarf::Plugin::Devel::StackTrace;
2 use Dwarf::Pragma;
3 use Dwarf::Util qw/add_method/;
4 use Devel::StackTrace;
5  
6 sub init {
7 my ($class, $c, $conf) = @_;
8 $conf ||= {};
9  
10 add_method($c, stacktrace => sub {
11 my ($self, $error) = @_;
12 _build_stacktrace($self->is_production, $error);
13 });
14 }
15  
16 sub _build_stacktrace {
17 my ($is_production, $error) = @_;
18 $error //= '';
19  
20 return '' if $is_production;
21  
22 my @frames = Devel::StackTrace->new()->frames;
23  
24 $error .= "\n";
25 $error .= join "\n", map {
26 my $s = $_->subroutine // '';
27 $s .= ' at ';
28 $s .= $_->filename // '';
29 $s .= ' line ';
30 $s .= $_->line // '';
31 $s .= "\n";
32 $s .= _build_context($_);
33 $s;
34 } @frames[ 1 .. $#frames ];
35  
36 return $error;
37 }
38  
39 sub _build_context {
40 my $frame = shift;
41 my $file = $frame->filename;
42 my $linenum = $frame->line;
43 my $code;
44 if (-f $file) {
45 my $start = $linenum - 3;
46 my $end = $linenum + 3;
47 $start = $start < 1 ? 1 : $start;
48 open my $fh, '<', $file
49 or die "cannot open $file:$!";
50 my $cur_line = 0;
51 while (my $line = <$fh>) {
52 ++$cur_line;
53 last if $cur_line > $end;
54 next if $cur_line < $start;
55 $line =~ s|\t| |g;
56 $code .= sprintf('%5d: %s', $cur_line, $line);
57 }
58 close $file;
59 }
60 return $code;
61 }
62  
63 1;