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   240 use strict;
  35         74  
  35         990  
3 35     35   174 use warnings;
  35         66  
  35         857  
4 35     35   169 use Data::Printer::Filter;
  35         72  
  35         198  
5 35     35   187 use Data::Printer::Common;
  35         67  
  35         787  
6 35     35   204 use Scalar::Util ();
  35         97  
  35         727  
7 35     35   212 use Fcntl;
  35         74  
  35         22692  
8              
9             filter 'CODE' => \&parse;
10              
11              
12             sub parse {
13 6     6 0 15 my ($subref, $ddp) = @_;
14 6         10 my $string;
15 6         13 my $color = 'code';
16 6 100 33     21 if ($ddp->deparse) {
    50          
17 2         5 $string = _deparse($subref, $ddp);
18 2 50 33     13 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         20 $string = $ddp->coderef_stub;
29             }
30 6         41 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         11 require B::Deparse;
40              
41             # FIXME: line below breaks encapsulation on Data::Printer::Object
42 2         5 my $i = $ddp->{indent} + $ddp->{_array_padding};
43              
44 2         8 my $deparseopts = ["-sCi${i}v'Useless const omitted'"];
45              
46 2         1572 my $sub = 'sub ' . B::Deparse->new($deparseopts)->coderef2text($subref);
47 2         21 my $pad = $ddp->newline;
48 2         13 $sub =~ s/\n/$pad/gse;
  4         12  
49 2         8 return $sub;
50             }
51              
52             sub _subref_is_reachable {
53 4     4   12 my ($subref) = @_;
54 4         23 require B;
55 4         18 my $cv = B::svref_2object($subref);
56 4   33     118 return !(B::class($cv->ROOT) eq 'NULL' && !${ $cv->const_sv });
57             }
58              
59             1;