File Coverage

blib/lib/Devel/Confess/Source.pm
Criterion Covered Total %
statement 45 48 93.7
branch 18 24 75.0
condition 11 18 61.1
subroutine 7 8 87.5
pod 0 1 0.0
total 81 99 81.8


line stmt bran cond sub pod time code
1             package Devel::Confess::Source;
2 2     2   62 use 5.006;
  2         9  
3 2     2   16 use strict;
  2         3  
  2         83  
4 2     2   16 use warnings FATAL => 'all';
  2         6  
  2         1509  
5              
6             sub import {
7 10 50   10   70 $^P |= "$]" >= 5.010 ? 0x400 : do {
8       0     *DB::DB = sub {}
9 0 0       0 unless defined &DB::DB;
10 0         0 0x02;
11             };
12             }
13              
14             my $want_color = $^O ne 'MSWin32' ? 1 : eval {
15             require Win32::Console::ANSI;
16             Win32::Console::ANSI->import;
17             1;
18             };
19              
20             sub source_trace {
21 21     21 0 32 my ($skip, $context, $evalonly) = @_;
22 21   50     47 $skip ||= 1;
23 21         28 $skip += $Carp::CarpLevel;
24 21   50     54 $context ||= 3;
25 21         24 my $i = $skip;
26 21         23 my @out;
27 21         118 while (my ($pack, $file, $line) = (caller($i++))[0..2]) {
28             next
29 180 100 66     706 if $Carp::Internal{$pack} || $Carp::CarpInternal{$pack};
30             next
31 126 50 33     242 if $evalonly && $file !~ /^\(eval \d+\)(?:\[|$)/;
32 126   50     175 my $lines = _get_content($file) || next;
33              
34 126         150 my $start = $line - $context;
35 126 100       210 $start = 1 if $start < 1;
36 126 100       219 $start = $#$lines if $start > $#$lines;
37 126         138 my $end = $line + $context;
38 126 100       200 $end = $#$lines if $end > $#$lines;
39              
40 126         235 my $context = "context for $file line $line:\n";
41 126         236 for my $read_line ($start..$end) {
42 740         790 my $code = $lines->[$read_line];
43 740         1565 $code =~ s/\n\z//;
44 740 100 66     2269 if ($want_color && $read_line == $line) {
45 119         200 $code = "\e[30;43m$code\e[m";
46             }
47 740         1713 $context .= sprintf "%5s : %s\n", $read_line, $code;
48             }
49 126         1638 push @out, $context;
50             }
51 21 50       47 return ''
52             if !@out;
53 21         251 return join(('=' x 75) . "\n",
54             '',
55             join(('-' x 75) . "\n", @out),
56             '',
57             );
58             }
59              
60             sub _get_content {
61 126     126   136 my $file = shift;
62 2     2   19 no strict 'refs';
  2         6  
  2         477  
63 126 100 100     360 if (exists $::{'_<'.$file} && @{ '::_<'.$file }) {
  119 100       1855  
    50          
64 70         62 return \@{ '::_<'.$file };
  70         271  
65             }
66             elsif ($file =~ /^\(eval \d+\)(?:\[.*\])?$/) {
67 7         43 return ["Can't get source of evals unless debugger available!"];
68             }
69             elsif (open my $fh, '<', $file) {
70 49         6299 my @lines = ('', <$fh>);
71 49         846 return \@lines;
72             }
73             else {
74 0           return ["Source file not available!"];
75             }
76             }
77              
78             1;