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   35 use 5.006;
  2         5  
3 2     2   6 use strict;
  2         2  
  2         38  
4 2     2   7 use warnings FATAL => 'all';
  2         2  
  2         778  
5              
6             sub import {
7 10 50   10   52 $^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 25 my ($skip, $context, $evalonly) = @_;
22 21   50     32 $skip ||= 1;
23 21         19 $skip += $Carp::CarpLevel;
24 21   50     30 $context ||= 3;
25 21         15 my $i = $skip;
26 21         16 my @out;
27 21         87 while (my ($pack, $file, $line) = (caller($i++))[0..2]) {
28             next
29 180 100 66     538 if $Carp::Internal{$pack} || $Carp::CarpInternal{$pack};
30             next
31 126 50 33     205 if $evalonly && $file !~ /^\(eval \d+\)(?:\[|$)/;
32 126   50     161 my $lines = _get_content($file) || next;
33              
34 126         116 my $start = $line - $context;
35 126 100       161 $start = 1 if $start < 1;
36 126 100       166 $start = $#$lines if $start > $#$lines;
37 126         93 my $end = $line + $context;
38 126 100       166 $end = $#$lines if $end > $#$lines;
39              
40 126         172 my $context = "context for $file line $line:\n";
41 126         173 for my $read_line ($start..$end) {
42 740         615 my $code = $lines->[$read_line];
43 740         1249 $code =~ s/\n\z//;
44 740 100 66     1869 if ($want_color && $read_line == $line) {
45 119         149 $code = "\e[30;43m$code\e[m";
46             }
47 740         1341 $context .= sprintf "%5s : %s\n", $read_line, $code;
48             }
49 126         1146 push @out, $context;
50             }
51 21 50       32 return ''
52             if !@out;
53 21         209 return join(('=' x 75) . "\n",
54             '',
55             join(('-' x 75) . "\n", @out),
56             '',
57             );
58             }
59              
60             sub _get_content {
61 126     126   98 my $file = shift;
62 2     2   8 no strict 'refs';
  2         4  
  2         288  
63 126 100 100     251 if (exists $::{'_<'.$file} && @{ '::_<'.$file }) {
  119 100       1386  
    50          
64 70         79 return \@{ '::_<'.$file };
  70         190  
65             }
66             elsif ($file =~ /^\(eval \d+\)(?:\[.*\])?$/) {
67 7         16 return ["Can't get source of evals unless debugger available!"];
68             }
69             elsif (open my $fh, '<', $file) {
70 49         4716 my @lines = ('', <$fh>);
71 49         610 return \@lines;
72             }
73             else {
74 0           return ["Source file not available!"];
75             }
76             }
77              
78             1;