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   46 use 5.006;
  2         5  
3 2     2   7 use strict;
  2         3  
  2         48  
4 2     2   6 use warnings FATAL => 'all';
  2         3  
  2         872  
5              
6             sub import {
7 10 50   10   40 $^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 26 my ($skip, $context, $evalonly) = @_;
22 21   50     35 $skip ||= 1;
23 21         20 $skip += $Carp::CarpLevel;
24 21   50     29 $context ||= 3;
25 21         13 my $i = $skip;
26 21         20 my @out;
27 21         100 while (my ($pack, $file, $line) = (caller($i++))[0..2]) {
28             next
29 180 100 66     556 if $Carp::Internal{$pack} || $Carp::CarpInternal{$pack};
30             next
31 126 50 33     178 if $evalonly && $file !~ /^\(eval \d+\)(?:\[|$)/;
32 126   50     143 my $lines = _get_content($file) || next;
33              
34 126         118 my $start = $line - $context;
35 126 100       165 $start = 1 if $start < 1;
36 126 100       180 $start = $#$lines if $start > $#$lines;
37 126         99 my $end = $line + $context;
38 126 100       169 $end = $#$lines if $end > $#$lines;
39              
40 126         205 my $context = "context for $file line $line:\n";
41 126         172 for my $read_line ($start..$end) {
42 740         658 my $code = $lines->[$read_line];
43 740         1202 $code =~ s/\n\z//;
44 740 100 66     1902 if ($want_color && $read_line == $line) {
45 119         150 $code = "\e[30;43m$code\e[m";
46             }
47 740         1314 $context .= sprintf "%5s : %s\n", $read_line, $code;
48             }
49 126         1130 push @out, $context;
50             }
51 21 50       29 return ''
52             if !@out;
53 21         213 return join(('=' x 75) . "\n",
54             '',
55             join(('-' x 75) . "\n", @out),
56             '',
57             );
58             }
59              
60             sub _get_content {
61 126     126   104 my $file = shift;
62 2     2   10 no strict 'refs';
  2         9  
  2         271  
63 126 100 100     324 if (exists $::{'_<'.$file} && @{ '::_<'.$file }) {
  119 100       1427  
    50          
64 70         48 return \@{ '::_<'.$file };
  70         160  
65             }
66             elsif ($file =~ /^\(eval \d+\)(?:\[.*\])?$/) {
67 7         15 return ["Can't get source of evals unless debugger available!"];
68             }
69             elsif (open my $fh, '<', $file) {
70 49         4740 my @lines = ('', <$fh>);
71 49         611 return \@lines;
72             }
73             else {
74 0           return ["Source file not available!"];
75             }
76             }
77              
78             1;