File Coverage

blib/lib/Devel/Confess/Source/Patch/ExcludePackage.pm
Criterion Covered Total %
statement 14 15 93.3
branch n/a
condition n/a
subroutine 5 6 83.3
pod 0 1 0.0
total 19 22 86.3


line stmt bran cond sub pod time code
1             package Devel::Confess::Source::Patch::ExcludePackage;
2              
3 1     1   457496 use 5.010001;
  1         5  
4 1     1   6 use strict;
  1         3  
  1         35  
5 1     1   5 no warnings;
  1         2  
  1         82  
6              
7 1     1   659 use Module::Patch;
  1         31607  
  1         12  
8 1     1   62 use base qw(Module::Patch);
  1         3  
  1         1100  
9              
10             our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
11             our $DATE = '2024-02-06'; # DATE
12             our $DIST = 'Devel-Confess-Source-Patch-ExcludePackage'; # DIST
13             our $VERSION = '0.002'; # VERSION
14              
15             our %config;
16              
17             my $want_color = $^O ne 'MSWin32' ? 1 : eval {
18             require Win32::Console::ANSI;
19             Win32::Console::ANSI->import;
20             1;
21             };
22              
23             my $p_source_trace = sub {
24             my ($skip, $context, $evalonly) = @_;
25             $skip ||= 1;
26             $skip += $Carp::CarpLevel;
27             $context ||= 3;
28             my $i = $skip;
29             my @out;
30             while (my ($pack, $file, $line) = (caller($i++))[0..2]) {
31             next
32             if $Carp::Internal{$pack} || $Carp::CarpInternal{$pack};
33             next
34             if $evalonly && $file !~ /^\(eval \d+\)(?:\[|$)/;
35             if (defined $config{-exclude_pat} && $pack =~ /$config{-exclude_pat}/) {
36             $context .= "Skipped stack trace level (package $pack excluded)\n" if $config{-show_excluded};
37             next;
38             }
39             if (defined $config{-include_pat} && $pack !~ /$config{-include_pat}/) {
40             $context .= "Skipped stack trace level (package $pack not included)\n" if $config{-show_excluded};
41             next;
42             }
43             die;
44             my $lines = _get_content($file) || next;
45              
46             my $start = $line - $context;
47             $start = 1 if $start < 1;
48             $start = $#$lines if $start > $#$lines;
49             my $end = $line + $context;
50             $end = $#$lines if $end > $#$lines;
51              
52             my $context = "context for $file line $line:\n";
53             for my $read_line ($start..$end) {
54             my $code = $lines->[$read_line];
55             $code =~ s/\n\z//;
56             if ($want_color && $read_line == $line) {
57             $code = "\e[30;43m$code\e[m";
58             }
59             $context .= sprintf "%5s : %s\n", $read_line, $code;
60             }
61             push @out, $context;
62             }
63             return ''
64             if !@out;
65             return join(('=' x 75) . "\n",
66             '',
67             join(('-' x 75) . "\n", @out),
68             '',
69             );
70             };
71              
72             sub patch_data {
73             return {
74 0     0 0   v => 3,
75             config => {
76             -exclude_pat => {
77             schema => 're*',
78             },
79             -include_pat => {
80             schema => 're*',
81             },
82             -show_excluded => {
83             schema => 'bool*',
84             },
85             },
86             patches => [
87             {
88             action => 'replace',
89             sub_name => 'source_trace',
90             code => $p_source_trace,
91             },
92             ],
93             };
94             }
95              
96             1;
97             # ABSTRACT: Exclude some packages from source trace
98              
99             __END__