blib/lib/Devel/Cycle.pm

filter: Statementstmt 78.5% 117 / 149 Branchbran 45.0% 36 / 80 Conditioncond 50.0% 9 / 18 MC/DCmcdc 41.6% 5 / 12 Subroutinesub 77.2% 17 / 22 Podpod 100% 2 / 2 totaltotal 65.7% 186 / 283 CC 50 SCAR 43.5
CC
50
Cov
66%
_format_reference
306.0
_format_index
20.0
_do_report
12.0
CRAP 77.5
↑ Summary
1 package Devel::Cycle;
2 # $Id: Cycle.pm,v 1.15 2009/08/24 12:51:02 lstein Exp $
3  
4 1 ▶use 5.006001;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Carp 'croak','carp';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 1 ▶use Scalar::Util qw(isweak blessed refaddr reftype);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 1 my $SHORT_NAME = 'A';
12 1 my %SHORT_NAMES;
13  
14  
15 1 require Exporter;
16  
17 1 our @ISA = qw(Exporter);
18 1 our @EXPORT = qw(find_cycle find_weakened_cycle);
19 1 our @EXPORT_OK = qw($FORMATTING);
20 1 our $VERSION = '1.12';
21 1 our $FORMATTING = 'roasted';
22 1 our $QUIET = 0;
23  
24 1 my %import_args = (-quiet =>1,
25 -raw =>1,
26 -cooked =>1,
27 -roasted=>1);
28  
29 1 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
30 1 require constant;
31 constant->import( HAVE_PADWALKER =>
32 1 eval {
33 1 require PadWalker;
34 1 $PadWalker::VERSION >= 1.0;
35 });
36 }
37  
38 1 ▶sub import {
Subroutine import: called CC=5 SCAR=17.8
39 1 my $self = shift;
40 1 my @args = @_;
41 0 my %args = map {$_=>1} @args;
42 1 ▶ $QUIET++ if exists $args{-quiet};
Branch: 1 branch
BranchTrueFalseTotal
if exists $args{"-quiet"} 0 1 1
43 1 ▶ $FORMATTING = 'roasted' if exists $args{-roasted};
Branch: 1 branch
BranchTrueFalseTotal
if exists $args{"-roasted"} 0 1 1
44 1 ▶ $FORMATTING = 'raw' if exists $args{-raw};
Branch: 1 branch
BranchTrueFalseTotal
if exists $args{"-raw"} 0 1 1
45 1 ▶ $FORMATTING = 'cooked' if exists $args{-cooked};
Branch: 1 branch
BranchTrueFalseTotal
if exists $args{"-cooked"} 0 1 1
46 0 $self->export_to_level(1,$self,grep {!exists $import_args{$_}} @_);
47 }
48  
49 2 ▶sub find_weakened_cycle {
Subroutine find_weakened_cycle: called CC=2 SCAR=8.5
Pod documented
50 2 my $ref = shift;
51 2 my $callback = shift;
52 2 ▶ unless ($callback) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($callback) 0 2 2
53 0 my $counter = 0;
54 0 ▶ $callback = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
55 0 _do_report(++$counter,shift)
56 }
57 0 }
58 2 _find_cycle($ref,{},$callback,1,{},());
59 }
60  
61 8 ▶sub find_cycle {
Subroutine find_cycle: called CC=2 SCAR=7.0
Pod documented
62 8 my $ref = shift;
63 8 my $callback = shift;
64 8 ▶ unless ($callback) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($callback) 2 6 8
65 2 my $counter = 0;
66 0 ▶ $callback = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
67 0 _do_report(++$counter,shift)
68 }
69 2 }
70 8 _find_cycle($ref,{},$callback,0,{},());
71 }
72  
73 143 ▶sub _find_cycle {
Subroutine _find_cycle: called CC=3 SCAR=11.0
74 143 my $current = shift;
75 143 my $seenit = shift;
76 143 my $callback = shift;
77 143 my $inc_weak_refs = shift;
78 143 my $complain = shift;
79 143 my @report = @_;
80  
81 143 ▶ return unless ref $current;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $current 83 60 143
82  
83 # note: it seems like you could just do:
84 #
85 # return if isweak($current);
86 #
87 # but strangely the weak flag doesn't seem to survive the copying,
88 # so the test has to happen directly on the reference in the data
89 # structure being scanned.
90  
91 60 ▶ if ($seenit->{refaddr $current}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$seenit{builtin::refaddr($current)}) 18 42 60
92 18 $callback->(\@report);
93 18 return;
94 }
95 42 $seenit->{refaddr $current}++;
96  
97 42 _find_cycle_dispatch($current,{%$seenit},$callback,$inc_weak_refs,$complain,@report);
98 }
99  
100 44 ▶sub _find_cycle_dispatch {
Subroutine _find_cycle_dispatch: called CC=3 SCAR=11.0
101 44 my $type = _get_type($_[0]);
102  
103 44 ▶ if (!defined $type) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $type) 2 42 44
104 2 my $ref = reftype $_[0];
105 2 our %already_warned;
106 2 ▶ if (!$already_warned{$ref}++) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($Devel::Cycle::already_warned{$ref}++) 1 1 2
107 1 warn "Unhandled type: $ref";
108 }
109 2 return;
110 }
111 1 ▶ my $sub = do { no strict 'refs'; \&{"_find_cycle_$type"} };
Subroutine BEGIN: called CC=1 SCAR=0.0
112 42 $sub->(@_);
113 }
114  
115 2 ▶sub _find_cycle_SCALAR {
Subroutine _find_cycle_SCALAR: called CC=4 SCAR=14.6
116 2 my $current = shift;
117 2 my $seenit = shift;
118 2 my $callback = shift;
119 2 my $inc_weak_refs = shift;
120 2 my $complain = shift;
121 2 my @report = @_;
122  
123 2 ▶ return if !$inc_weak_refs && isweak($$current);
Branch: 1 branch
BranchTrueFalseTotal
if not $inc_weak_refs and builtin::is_weak($$current) 0 2 2
Condition33%1/3
not $inc_weak_refs and builtin::is_weak($$current)
!ll&&!rl&&r
020
MC/DC0%0/2
not $inc_weak_refs and builtin::is_weak($$current)
not $inc_weak_refs builtin::is_weak($$current)
Truth table33%1/3
not(A) and B
A= not $inc_weak_refsB= builtin::is_weak($$current)
ABresult
0X0
100
111
124 2 ▶ _find_cycle($$current,{%$seenit},$callback,$inc_weak_refs,$complain,
Branch: 1 branch
BranchTrueFalseTotal
$inc_weak_refs ? : 0 2 2
125 (@report,['SCALAR',undef,$current => $$current,$inc_weak_refs?isweak($$current):()]));
126 }
127  
128 19 ▶sub _find_cycle_ARRAY {
Subroutine _find_cycle_ARRAY: called CC=5 SCAR=16.1
129 19 my $current = shift;
130 19 my $seenit = shift;
131 19 my $callback = shift;
132 19 my $inc_weak_refs = shift;
133 19 my $complain = shift;
134 19 my @report = @_;
135  
136 19 ▶ for (my $i=0; $i<@$current; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$current) 79 19 98
137 79 ▶ next if !$inc_weak_refs && isweak($current->[$i]);
Branch: 1 branch
BranchTrueFalseTotal
if not $inc_weak_refs and builtin::is_weak($current->[$i]) 2 77 79
Condition100%3/3
not $inc_weak_refs and builtin::is_weak($current->[$i])
!ll&&!rl&&r
30472
MC/DC100%2/2
not $inc_weak_refs and builtin::is_weak($current->[$i])
not $inc_weak_refs builtin::is_weak($current->[$i])
Truth table100%3/3
not(A) and B
A= not $inc_weak_refsB= builtin::is_weak($current->[$i])
ABresult
0X0
100
111
138 77 ▶ _find_cycle($current->[$i],{%$seenit},$callback,$inc_weak_refs,$complain,
Branch: 1 branch
BranchTrueFalseTotal
$inc_weak_refs ? : 30 47 77
139 (@report,['ARRAY',$i,$current => $current->[$i],$inc_weak_refs?isweak($current->[$i]):()]));
140 }
141 }
142  
143 20 ▶sub _find_cycle_HASH {
Subroutine _find_cycle_HASH: called CC=5 SCAR=16.1
144 20 my $current = shift;
145 20 my $seenit = shift;
146 20 my $callback = shift;
147 20 my $inc_weak_refs = shift;
148 20 my $complain = shift;
149 20 my @report = @_;
150  
151 20 for my $key (sort keys %$current) {
152 57 ▶ next if !$inc_weak_refs && isweak($current->{$key});
Branch: 1 branch
BranchTrueFalseTotal
if not $inc_weak_refs and builtin::is_weak($current->{$key}) 3 54 57
Condition100%3/3
not $inc_weak_refs and builtin::is_weak($current->{$key})
!ll&&!rl&&r
22323
MC/DC100%2/2
not $inc_weak_refs and builtin::is_weak($current->{$key})
not $inc_weak_refs builtin::is_weak($current->{$key})
Truth table100%3/3
not(A) and B
A= not $inc_weak_refsB= builtin::is_weak($current->{$key})
ABresult
0X0
100
111
153 _find_cycle($current->{$key},{%$seenit},$callback,$inc_weak_refs,$complain,
154 54 ▶ (@report,['HASH',$key,$current => $current->{$key},$inc_weak_refs?isweak($current->{$key}):()]));
Branch: 1 branch
BranchTrueFalseTotal
$inc_weak_refs ? : 22 32 54
155 }
156 }
157  
158 1 ▶sub _find_cycle_CODE {
Subroutine _find_cycle_CODE: called CC=2 SCAR=6.9
159 1 my $current = shift;
160 1 my $seenit = shift;
161 1 my $callback = shift;
162 1 my $inc_weak_refs = shift;
163 1 my $complain = shift;
164 1 my @report = @_;
165  
166 1 unless (HAVE_PADWALKER) {
167 if (!$complain->{$current} && !$QUIET) {
168 carp "A code closure was detected in but we cannot check it unless the PadWalker module is installed";
169 }
170  
171 return;
172 }
173  
174 1 my $closed_vars = PadWalker::closed_over( $current );
175 1 foreach my $varname ( sort keys %$closed_vars ) {
176 2 my $value = $closed_vars->{$varname};
177 2 _find_cycle_dispatch($value,{%$seenit},$callback,$inc_weak_refs,$complain,
178 (@report,['CODE',$varname,$current => $value]));
179 }
180 }
181  
182 0 ▶sub _do_report {
Subroutine _do_report: not called CC=3 SCAR=24.8
183 0 my $counter = shift;
184 0 my $path = shift;
185 0 print "Cycle ($counter):\n";
186 0 foreach (@$path) {
187 0 my ($type,$index,$ref,$value,$is_weak) = @$_;
188 0 ▶ printf("\t%30s => %-30s\n",($is_weak ? 'w-> ' : '')._format_reference($type,$index,$ref,0),_format_reference(undef,undef,$value,1));
Branch: 1 branch
BranchTrueFalseTotal
$is_weak ? : 0 0 0
189 }
190 0 print "\n";
191 }
192  
193 0 ▶sub _format_reference {
Subroutine _format_reference: not called CC=17 SCAR=57.2
194 0 my ($type,$index,$ref,$deref) = @_;
195 0 ▶ $type ||= _get_type($ref);
Condition0%0/3
$type ||= _get_type($ref)
l!l&&r!l&&!r
000
MC/DC0%0/2
$type ||= _get_type($ref)
$type _get_type($ref)
Truth table0%0/3
A ||= B
A= $typeB= _get_type($ref)
ABresult
1X1
011
000
196 0 ▶ return $ref unless $type;
Branch: 1 branch
BranchTrueFalseTotal
unless $type 0 0 0
197 0 ▶ my $suffix = defined $index ? _format_index($type,$index) : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $index ? : 0 0 0
198 0 ▶ if ($FORMATTING eq 'raw') {
Branch: 1 branch
BranchTrueFalseTotal
if ($Devel::Cycle::FORMATTING eq 'raw') { } 0 0 0
199 0 return $ref.$suffix;
200 }
201  
202 else {
203 0 my $package = blessed($ref);
204 0 ▶ my $prefix = $package ? ($FORMATTING eq 'roasted' ? "${package}::" : "${package}=" ) : '';
Branch: 2 branches
BranchTrueFalseTotal
$package ? : 0 0 0
$Devel::Cycle::FORMATTING eq 'roasted' ? : 0 0 0
205 0 ▶ my $sygil = $deref ? '\\' : '';
Branch: 1 branch
BranchTrueFalseTotal
$deref ? : 0 0 0
206 0 ▶ my $shortname = ($SHORT_NAMES{$ref} ||= $SHORT_NAME++);
Condition0%0/3
$SHORT_NAMES{$ref} ||= $SHORT_NAME++
l!l&&r!l&&!r
000
MC/DC0%0/2
$SHORT_NAMES{$ref} ||= $SHORT_NAME++
$SHORT_NAMES{$ref} $SHORT_NAME++
Truth table0%0/3
A ||= B
A= $SHORT_NAMES{$ref}B= $SHORT_NAME++
ABresult
1X1
011
000
207 0 ▶ return $sygil . ($sygil ? '$' : '$$'). $prefix . $shortname . $suffix if $type eq 'SCALAR';
Branch: 2 branches
BranchTrueFalseTotal
if $type eq "SCALAR" 0 0 0
$sygil ? : 0 0 0
208 0 ▶ return $sygil . ($sygil ? '@' : '$') . $prefix . $shortname . $suffix if $type eq 'ARRAY';
Branch: 2 branches
BranchTrueFalseTotal
if $type eq "ARRAY" 0 0 0
$sygil ? : 0 0 0
209 0 ▶ return $sygil . ($sygil ? '%' : '$') . $prefix . $shortname . $suffix if $type eq 'HASH';
Branch: 2 branches
BranchTrueFalseTotal
if $type eq "HASH" 0 0 0
$sygil ? : 0 0 0
210 0 ▶ return $sygil . ($sygil ? '&' : '$') . $prefix . $shortname . $suffix if $type eq 'CODE';
Branch: 2 branches
BranchTrueFalseTotal
if $type eq "CODE" 0 0 0
$sygil ? : 0 0 0
211 }
212 }
213  
214 # why not Scalar::Util::reftype?
215 44 ▶sub _get_type {
Subroutine _get_type: called CC=7 SCAR=19.5
216 44 my $thingy = shift;
217 44 ▶ return unless ref $thingy;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $thingy 0 44 44
218 44 ▶ return 'SCALAR' if UNIVERSAL::isa($thingy,'SCALAR') || UNIVERSAL::isa($thingy,'REF');
Branch: 1 branch
BranchTrueFalseTotal
if UNIVERSAL::isa($thingy, "SCALAR") or UNIVERSAL::isa($thingy, "REF") 2 42 44
Condition66%2/3
UNIVERSAL::isa($thingy, "SCALAR") or UNIVERSAL::isa($thingy, "REF")
l!l&&r!l&&!r
0242
MC/DC50%1/2
UNIVERSAL::isa($thingy, "SCALAR") or UNIVERSAL::isa($thingy, "REF")
UNIVERSAL::isa($thingy, "SCALAR") UNIVERSAL::isa($thingy, "REF")
Truth table66%2/3
A or B
A= UNIVERSAL::isa($thingy, "SCALAR")B= UNIVERSAL::isa($thingy, "REF")
ABresult
1X1
011
000
219 42 ▶ return 'ARRAY' if UNIVERSAL::isa($thingy,'ARRAY');
Branch: 1 branch
BranchTrueFalseTotal
if UNIVERSAL::isa($thingy, "ARRAY") 19 23 42
220 23 ▶ return 'HASH' if UNIVERSAL::isa($thingy,'HASH');
Branch: 1 branch
BranchTrueFalseTotal
if UNIVERSAL::isa($thingy, "HASH") 20 3 23
221 3 ▶ return 'CODE' if UNIVERSAL::isa($thingy,'CODE');
Branch: 1 branch
BranchTrueFalseTotal
if UNIVERSAL::isa($thingy, "CODE") 1 2 3
222 2 undef;
223 }
224  
225 0 ▶sub _format_index {
Subroutine _format_index: not called CC=4 SCAR=30.0
226 0 my ($type,$index) = @_;
227 0 ▶ return "->[$index]" if $type eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "ARRAY" 0 0 0
228 0 ▶ return "->{'$index'}" if $type eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "HASH" 0 0 0
229 0 ▶ return " variable $index" if $type eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "CODE" 0 0 0
230 0 return;
231 }
232  
233 1 1;
234 __END__
↑ Summary