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 34     34   218 use strict;
  34         75  
  34         955  
3 34     34   172 use warnings;
  34         97  
  34         825  
4 34     34   172 use Data::Printer::Filter;
  34         79  
  34         186  
5 34     34   184 use Data::Printer::Common;
  34         72  
  34         708  
6 34     34   226 use Scalar::Util ();
  34         109  
  34         745  
7 34     34   171 use Fcntl;
  34         89  
  34         21868  
8              
9             filter 'CODE' => \&parse;
10              
11              
12             sub parse {
13 6     6 0 14 my ($subref, $ddp) = @_;
14 6         12 my $string;
15 6         11 my $color = 'code';
16 6 100 33     18 if ($ddp->deparse) {
    50          
17 2         29 $string = _deparse($subref, $ddp);
18 2 50 33     7 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         19 $string = $ddp->coderef_stub;
29             }
30 6         33 return $ddp->maybe_colorize($string, $color);
31             };
32              
33             #######################################
34             ### Private auxiliary helpers below ###
35             #######################################
36              
37             sub _deparse {
38 2     2   7 my ($subref, $ddp) = @_;
39 2         12 require B::Deparse;
40              
41             # FIXME: line below breaks encapsulation on Data::Printer::Object
42 2         8 my $i = $ddp->{indent} + $ddp->{_array_padding};
43              
44 2         8 my $deparseopts = ["-sCi${i}v'Useless const omitted'"];
45              
46 2         1426 my $sub = 'sub ' . B::Deparse->new($deparseopts)->coderef2text($subref);
47 2         22 my $pad = $ddp->newline;
48 2         10 $sub =~ s/\n/$pad/gse;
  4         12  
49 2         8 return $sub;
50             }
51              
52             sub _subref_is_reachable {
53 4     4   11 my ($subref) = @_;
54 4         25 require B;
55 4         21 my $cv = B::svref_2object($subref);
56 4   33     93 return !(B::class($cv->ROOT) eq 'NULL' && !${ $cv->const_sv });
57             }
58              
59             1;