blib/lib/Data/Decycle.pm

filter: Statementstmt 82.3% 84 / 102 Branchbran 74.0% 37 / 50 Conditioncond 66.6% 4 / 6 MC/DCmcdc 50.0% 2 / 4 Subroutinesub 88.4% 23 / 26 Podpod 100% 7 / 7 totaltotal 80.5% 157 / 195 CC 34 SCAR 24.6
CC
34
Cov
79%
DESTROY
42.0
__ANON__
16.0
add
12.0
CRAP 11.7
↑ Summary
1 #
2 # $Id: Decycle.pm,v 0.2 2010/08/23 09:11:03 dankogai Exp dankogai $
3 #
4 package Data::Decycle;
5 6 ▶use 5.008001;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 6 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 6 ▶use Scalar::Util qw/refaddr weaken isweak/;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 6 our $VERSION = sprintf "%d.%02d", q$Revision: 0.2 $ =~ /(\d+)/g;
12 6 our $DEBUG = 0;
13  
14 6 ▶use base 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
15 6 our @EXPORT = ();
16 6 our @EXPORT_OK = qw(recsub $CALLEE
17 may_leak has_cyclic_ref decycle_deeply weaken_deeply
18 );
19 6 our %EXPORT_TAGS = ( all => [ @EXPORT, @EXPORT_OK ], );
20  
21 6 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
22 6 require constant;
23 constant->import(
24 6 HAS_PADWALKER => eval {
25 6 require PadWalker;
26 6 $PadWalker::VERSION >= 1.0;
27 }
28 );
29 }
30  
31 0 ▶sub new {
Subroutine new: not called CC=1 SCAR=6.9
Pod documented
32 0 my $class = shift;
33 0 my $self = bless [], $class;
34 0 $self->add(@_);
35 }
36  
37 0 ▶sub add {
Subroutine add: not called CC=3 SCAR=24.8
Pod documented
38 0 my $self = shift;
39 0 for (@_){
40 0 ▶ croak "$_ is not a reference" unless ref $_;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $_ 0 0 0
41 0 push @{$self}, $_;
42 }
43 0 $self;
44 }
45  
46 0 ▶sub DESTROY {
Subroutine DESTROY: not called CC=6 SCAR=37.4
47 0 my $self = shift;
48 0 ▶ if ($DEBUG > 1){
Branch: 1 branch
BranchTrueFalseTotal
if ($Data::Decycle::DEBUG > 1) 0 0 0
49 0 ▶ require Data::Dumper and Data::Dumper->import;
Branch: 1 branch
BranchTrueFalseTotal
require Data::Dumper and "Data::Dumper"->import 0 0 0
50 0 print Dumper($self);
51 }
52 0 for (@{$self}){
53 0 ▶ next unless ref $_;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $_ 0 0 0
54 0 ▶ carp "decyling ($_)" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $Data::Decycle::DEBUG 0 0 0
55 0 decycle_deeply($_);
56 }
57 }
58  
59 6 our $CALLEE;
60  
61 27 ▶sub recsub(&) {
Subroutine recsub: called CC=1 SCAR=0.0
Pod documented
62 27 my $code = shift;
63 47 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
64 47 local *CALLEE = \$code;
65 47 $code->(@_);
66 }
67 27 }
68  
69 12 ▶sub _mkfinder(&) {
Subroutine _mkfinder: called CC=1 SCAR=0.0
70 12 my $cb = shift;
71 70 ▶ return recsub {
Subroutine __ANON__: called CC=16 SCAR=27.7
72 70 ▶ return unless ref $_[0];
Branch: 1 branch
BranchTrueFalseTotal
unless ref $_[0] 6 64 70
73 6 ▶ no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
74 64 ▶ return $cb->( $_[0] ) if $_[1]->{ refaddr $_[0] }++;
Branch: 1 branch
BranchTrueFalseTotal
if $_[1]{builtin::refaddr($_[0])}++ 17 47 64
75 47 ▶ if ( UNIVERSAL::isa( $_[0], 'HASH' ) ) {
Branch: 4 branches
BranchTrueFalseTotal
if (UNIVERSAL::isa($_[0], 'HASH')) { } 15 32 47
elsif (UNIVERSAL::isa($_[0], 'ARRAY')) { } 8 24 32
elsif (UNIVERSAL::isa($_[0], 'SCALAR') or UNIVERSAL::isa($_[0], 'REF')) { } 11 13 24
elsif (UNIVERSAL::isa($_[0], 'CODE')) { } 13 0 13
76 15 for ( values %{ $_[0] } ) {
77 18 ▶ next unless ref $_;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $_ 6 12 18
78 12 ▶ return 1 if $CALLEE->( $_, $_[1] );
Branch: 1 branch
BranchTrueFalseTotal
if &$Data::Decycle::CALLEE($_, $_[1]) 8 4 12
79 }
80 }
81 elsif ( UNIVERSAL::isa( $_[0], 'ARRAY' ) ) {
82 8 for ( @{ $_[0] } ) {
83 13 ▶ next unless ref $_;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $_ 9 4 13
84 4 ▶ return 1 if $CALLEE->( $_, $_[1] );
Branch: 1 branch
BranchTrueFalseTotal
if &$Data::Decycle::CALLEE($_, $_[1]) 3 1 4
85 }
86 }
87 ▶ elsif (UNIVERSAL::isa( $_[0], 'SCALAR' )
Condition66%2/3
UNIVERSAL::isa($_[0], "SCALAR") or UNIVERSAL::isa($_[0], "REF")
l!l&&r!l&&!r
01113
MC/DC50%1/2
UNIVERSAL::isa($_[0], "SCALAR") or UNIVERSAL::isa($_[0], "REF")
UNIVERSAL::isa($_[0], "SCALAR") UNIVERSAL::isa($_[0], "REF")
Truth table66%2/3
A or B
A= UNIVERSAL::isa($_[0], "SCALAR")B= UNIVERSAL::isa($_[0], "REF")
ABresult
1X1
011
000
88 || UNIVERSAL::isa( $_[0], 'REF' ) )
89 {
90 11 return $CALLEE->( ${ $_[0] }, $_[1] );
91 }
92 elsif ( HAS_PADWALKER && UNIVERSAL::isa( $_[0], 'CODE' ) ) {
93 13 my $r = PadWalker::closed_over( $_[0] );
94 13 ▶ return unless keys %$r;
Branch: 1 branch
BranchTrueFalseTotal
unless keys %$r 6 7 13
95 7 ▶ $CALLEE->( $r, $_[1] ) && return 1;
Branch: 1 branch
BranchTrueFalseTotal
&$Data::Decycle::CALLEE($r, $_[1]) and return 1 4 3 7
96 }
97 15 return;
98 }
99 12 }
100  
101 10 ▶*_has_cyclic_ref = _mkfinder { 1 };
Subroutine __ANON__: called CC=1 SCAR=0.0
102 24 ▶sub has_cyclic_ref($){ _has_cyclic_ref($_[0], {}) }
Subroutine has_cyclic_ref: called CC=1 SCAR=0.0
Pod documented
103  
104 7 ▶*_may_leak = _mkfinder { !isweak($_[0]) };
Subroutine __ANON__: called CC=1 SCAR=0.0
105 12 ▶sub may_leak($){ _may_leak($_[0], {}) }
Subroutine may_leak: called CC=1 SCAR=0.0
Pod documented
106  
107 12 ▶sub _mkwalker(&){
Subroutine _mkwalker: called CC=1 SCAR=0.0
108 12 my $cb = shift;
109 30 ▶ return recsub {
Subroutine __ANON__: called CC=11 SCAR=24.1
110 30 ▶ return unless ref $_[0];
Branch: 1 branch
BranchTrueFalseTotal
unless ref $_[0] 4 26 30
111 6 ▶ no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
112 26 ▶ return $cb->( $_[0] ) if $_[1]->{ refaddr $_[0] }++;
Branch: 1 branch
BranchTrueFalseTotal
if $_[1]{builtin::refaddr($_[0])}++ 11 15 26
113 15 ▶ if ( UNIVERSAL::isa( $_[0], 'HASH' ) ) {
Branch: 4 branches
BranchTrueFalseTotal
if (UNIVERSAL::isa($_[0], 'HASH')) { } 5 10 15
elsif (UNIVERSAL::isa($_[0], 'ARRAY')) { } 3 7 10
elsif (UNIVERSAL::isa($_[0], 'SCALAR') or UNIVERSAL::isa($_[0], 'REF')) { } 5 2 7
elsif (UNIVERSAL::isa($_[0], 'CODE')) { } 2 0 2
114 5 $CALLEE->( $_, $_[1] ) for values %{ $_[0] };
115 }
116 elsif ( UNIVERSAL::isa( $_[0], 'ARRAY' ) ) {
117 3 $CALLEE->( $_, $_[1] ) for @{ $_[0] };
118 }
119 ▶ elsif (UNIVERSAL::isa( $_[0], 'SCALAR' )
Condition66%2/3
UNIVERSAL::isa($_[0], "SCALAR") or UNIVERSAL::isa($_[0], "REF")
l!l&&r!l&&!r
052
MC/DC50%1/2
UNIVERSAL::isa($_[0], "SCALAR") or UNIVERSAL::isa($_[0], "REF")
UNIVERSAL::isa($_[0], "SCALAR") UNIVERSAL::isa($_[0], "REF")
Truth table66%2/3
A or B
A= UNIVERSAL::isa($_[0], "SCALAR")B= UNIVERSAL::isa($_[0], "REF")
ABresult
1X1
011
000
120 || UNIVERSAL::isa( $_[0], 'REF' ) )
121 {
122 5 $CALLEE->( ${ $_[0] }, $_[1] );
123 }
124 elsif ( HAS_PADWALKER && UNIVERSAL::isa( $_[0], 'CODE' ) ) {
125 2 my $r = PadWalker::closed_over( $_[0] );
126 2 ▶ return unless keys %$r;
Branch: 1 branch
BranchTrueFalseTotal
unless keys %$r 0 2 2
127 2 $CALLEE->( $r, $_[1] );
128 }
129 15 return;
130 12 };
131 }
132  
133 4 ▶*_decycle_deeply = _mkwalker { undef $_[0] };
Subroutine __ANON__: called CC=1 SCAR=0.0
134 4 ▶sub decycle_deeply($) { _decycle_deeply( $_[0], {} ) }
Subroutine decycle_deeply: called CC=1 SCAR=0.0
Pod documented
135  
136 7 ▶*_weaken_deeply = _mkwalker {
Subroutine __ANON__: called CC=2 SCAR=6.9
137 7 ▶ weaken $_[0] unless UNIVERSAL::isa( $_[0], 'CODE' )
Branch: 1 branch
BranchTrueFalseTotal
unless UNIVERSAL::isa($_[0], "CODE") 6 1 7
138 6 };
139 7 ▶sub weaken_deeply($) { _weaken_deeply( $_[0], {} ) }
Subroutine weaken_deeply: called CC=1 SCAR=0.0
Pod documented
140  
141 6 1; # End of Data::Decycle
142  
143 __END__
↑ Summary