File Coverage

blib/lib/Data/Printer/Filter/CODE.pm
Criterion Covered Total %
statement 39 43 90.7
branch 4 6 66.6
condition 3 9 33.3
subroutine 9 9 100.0
pod 0 1 0.0
total 55 68 80.8


line stmt bran cond sub pod time code
1             package Data::Printer::Filter::CODE;
2 35     35   249 use strict;
  35         70  
  35         1358  
3 35     35   161 use warnings;
  35         85  
  35         1789  
4 35     35   185 use Data::Printer::Filter;
  35         60  
  35         284  
5 35     35   207 use Data::Printer::Common;
  35         77  
  35         1068  
6 35     35   223 use Scalar::Util ();
  35         116  
  35         970  
7 35     35   176 use Fcntl;
  35         74  
  35         27544  
8              
9             filter 'CODE' => \&parse;
10              
11              
12             sub parse {
13 6     6 0 16 my ($subref, $ddp) = @_;
14 6         16 my $string;
15 6         13 my $color = 'code';
16 6 100 33     107 if ($ddp->deparse) {
    50          
17 2         10 $string = _deparse($subref, $ddp);
18 2 50 33     8 if ($ddp->coderef_undefined && $string =~ /\A\s*sub\s*;\s*\z/) {
19 0         0 $string = $ddp->coderef_undefined;
20 0         0 $color = 'undef';
21             }
22             }
23             elsif ($ddp->coderef_undefined && !_subref_is_reachable($subref)) {
24 0         0 $string = $ddp->coderef_undefined;
25 0         0 $color = 'undef';
26             }
27             else {
28 4         22 $string = $ddp->coderef_stub;
29             }
30 6         54 return $ddp->maybe_colorize($string, $color);
31             };
32              
33             #######################################
34             ### Private auxiliary helpers below ###
35             #######################################
36              
37             sub _deparse {
38 2     2   5 my ($subref, $ddp) = @_;
39 2         19 require B::Deparse;
40              
41             # FIXME: line below breaks encapsulation on Data::Printer::Object
42 2         7 my $i = $ddp->{indent} + $ddp->{_array_padding};
43              
44 2         8 my $deparseopts = ["-sCi${i}v'Useless const omitted'"];
45              
46 2         1898 my $sub = 'sub ' . B::Deparse->new($deparseopts)->coderef2text($subref);
47 2         28 my $pad = $ddp->newline;
48 2         14 $sub =~ s/\n/$pad/gse;
  4         13  
49 2         10 return $sub;
50             }
51              
52             sub _subref_is_reachable {
53 4     4   13 my ($subref) = @_;
54 4         31 require B;
55 4         26 my $cv = B::svref_2object($subref);
56 4   33     129 return !(B::class($cv->ROOT) eq 'NULL' && !${ $cv->const_sv });
57             }
58              
59             1;