Monitor.pm

filter: Statementstmt 82.9% 228 / 275 Branchbran 65.8% 79 / 120 Conditioncond 44.4% 8 / 18 MC/DCmcdc 36.3% 4 / 11 Subroutinesub 71.1% 32 / 45 Podpod 0.0% 0 / 3 totaltotal 74.3% 351 / 472 CC 74 SCAR 24.2
CC
74
Cov
76%
_checkCircularRef
26.3
_monitorRecursively
22.5
printCircularRef
16.1
CRAP 11.3
↑ Summary Monitor/Array.pm »
1 package Devel::Monitor;
2  
3 1 ▶use 5.008006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 require Exporter;
8 1 ▶use AutoLoader qw(AUTOLOAD);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 our @ISA = qw(Exporter);
11  
12 # Items to export into callers namespace by default. Note: do not export
13 # names by default without a very good reason. Use EXPORT_OK instead.
14 # Do not simply export all your public functions/methods/constants.
15  
16 # This allows declaration use Devel::Monitor ':all';
17 # If you do not need this, moving things directly into @EXPORT or @EXPORT_OK
18 # will save memory.
19  
20 1 our @EXPORT = qw(); #Export by default
21  
22 1 our %EXPORT_TAGS = ( #Export as groups
23 'all' => [
24 qw(monitor
25 print_circular_ref
26 )
27 ]
28 );
29  
30 1 Exporter::export_ok_tags( #Export by request (into @EXPORT_OK)
31 'all');
32  
33 1 our $VERSION = '0.9.0.7';
34  
35 1 ▶use Error qw(:try);
Subroutine BEGIN: called CC=1 SCAR=0.0
36 1 ▶use Scalar::Util qw(isweak);
Subroutine BEGIN: called CC=1 SCAR=0.0
37 1 ▶use Devel::Monitor::Common qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
38 1 ▶use Devel::Monitor::Trace;
Subroutine BEGIN: called CC=1 SCAR=0.0
39 1 ▶use Devel::Monitor::TraceItem;
Subroutine BEGIN: called CC=1 SCAR=0.0
40 1 ▶use Devel::Monitor::Array;
Subroutine BEGIN: called CC=1 SCAR=0.0
41 1 ▶use Devel::Monitor::Hash;
Subroutine BEGIN: called CC=1 SCAR=0.0
42 1 ▶use Devel::Monitor::Scalar;
Subroutine BEGIN: called CC=1 SCAR=0.0
43
44 #Circular references type
45 1 ▶use constant CRT_NONE => undef;
Subroutine BEGIN: called CC=1 SCAR=0.0
46 1 ▶use constant CRT_CIRC_REF => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
47 1 ▶use constant CRT_INTERNAL_CIRC_REF => 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
48 1 ▶use constant CRT_WEAK_CIRC_REF => 3;
Subroutine BEGIN: called CC=1 SCAR=0.0
49  
50 # METH monitor
51 #
52 # DESC Monitoring multiple variables
53 # monitor('name for a' => \$a,
54 # 'name for b' => \$b,
55 # 'name for c' => \$c,
56 # 'name for d' => \@d,
57 # 'name for e' => \%e,
58 # 'name for F' => \&F);
59 # DESC Monitoring single constant variable (FOR INTERNAL USE ONLY)
60 # monitor('name for F' => \&F, 1);
61 # The last flag indicates that it is code reference
62  
63 9 ▶sub monitor {
Subroutine monitor: called CC=10 SCAR=27.1
Pod undocumented
64 9 my $isCode;
65 9 ▶ $isCode = pop if scalar(@_) % 2 != 0;
Branch: 1 branch
BranchTrueFalseTotal
if scalar(@_) % 2 != 0 4 5 9
66 9 my %values = @_;
67 9 foreach my $key (keys %values) {
68 9 my $varRef = $values{$key};
69 9 ▶ if ($varRef) { #If the value is undef
Branch: 1 branch
BranchTrueFalseTotal
if ($varRef) 9 0 9
70 9 _dereference(\$varRef);
71 9 ▶ if ($varRef =~ /HASH/ ) { #An hash object or an hash
Branch: 4 branches
BranchTrueFalseTotal
if ($varRef =~ /HASH/) { } 2 7 9
elsif ($varRef =~ /SCALAR|REF/) { } 4 3 7
elsif ($varRef =~ /ARRAY/) { } 2 1 3
elsif ($varRef =~ /CODE/) { } 1 0 1
72 2 _tieHash($varRef,$key,$isCode);
73 }
74 elsif ($varRef =~ /SCALAR|REF/) {
75 4 _tieScalar($varRef,$key,$isCode);
76 }
77 elsif ($varRef =~ /ARRAY/) {
78 2 _tieArray($varRef,$key,$isCode);
79 }
80 elsif ($varRef =~ /CODE/) {
81 ###########################################################
82 # Info on constants
83 ###########################################################
84 # use constant CONST => [1,2];
85 # print \&CONST."\n";
86 # print &CONST."\n";
87 # print \&CONST()."\n";
88 # print &CONST()."\n";
89 #
90 # CODE(0x8203000)
91 # ARRAY(0x81d4c04)
92 # REF(0x820303c)
93 # ARRAY(0x81d4c04)
94 #
95 # Code Ref
96 # | |
97 # Array Array
98 # | |
99 # +---+ +---+
100 # |1,2| |1,2|
101 # +---+ +---+
102 #
103 ###########################################################
104 # use constant CONST => 'a scalar';
105 # print \&CONST."\n";
106 # print &CONST."\n";
107 # print \&CONST()."\n";
108 # print &CONST()."\n";
109 #
110 # CODE(0x820300c)
111 # a scalar
112 # SCALAR(0x81fb2c4)
113 # a scalar
114 #
115 # Code Scalar
116 # | |
117 # 'a scalar' 'a scalar'
118 #
119 # Instead of :
120 #
121 # Code Ref
122 # | |
123 # Scalar Scalar
124 # | |
125 # 'a scalar' 'a scalar'
126 #
127 ###########################################################
128 1 ▶ if (ref(&$varRef) =~ /ARRAY|HASH/) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref(&$varRef) =~ /ARRAY|HASH/) { } 1 0 1
129 1 _monitorRecursively($key => &$varRef);
130 } else {
131 #_tieScalar($varRef);
132 0 Devel::Monitor::Common::printMsg("Scalar constant $key cannot be monitored\n");
133 }
134 }
135 else {
136 0 my $runPatch = 0;
137 0 ▶ try {
Subroutine __ANON__: not called CC=1 SCAR=6.9
138 0 _tieHash($varRef,$key);
139 0 ▶ } otherwise {
Subroutine __ANON__: not called CC=1 SCAR=6.9
140 ###########################################################
141 # Patch for Error.pm
142 # It seems there is a bug in this module
143 ###########################################################
144 # Example :
145 ###########################################################
146 # #!/usr/bin/perl
147 # use strict;
148 # use warnings;
149 # use Devel::Monitor;
150 # {
151 # my @a = (1,2,3,4);
152 # monitor('a'=>\@a);
153 # print STDERR "Leaving scope\n";
154 # }
155 # print STDERR "Scope left\n";
156 ###########################################################
157 # Output without the patch (Very bad for mod_perl)
158 ###########################################################
159 # MONITOR ARRAY : a
160 # Leaving scope
161 # Scope left
162 # DESTROY ARRAY : a
163 ###########################################################
164 # Output with the patch (Ok)
165 ###########################################################
166 # MONITOR ARRAY : a
167 # Leaving scope
168 # DESTROY ARRAY : a
169 # Scope left
170 ###########################################################
171 0 $runPatch = 1;
172 #try {
173 # _tieArray($varRef,$key);
174 #} otherwise {
175 # Devel::Monitor::Common::printMsg("$varRef($key) cannot be monitored\n");
176 #};
177 0 };
178 0 ▶ if ($runPatch) {
Branch: 1 branch
BranchTrueFalseTotal
if ($runPatch) 0 0 0
179 0 ▶ try {
Subroutine __ANON__: not called CC=1 SCAR=6.9
180 0 _tieArray($varRef,$key);
181 0 ▶ } otherwise {
Subroutine __ANON__: not called CC=1 SCAR=6.9
182 0 Devel::Monitor::Common::printMsg("$varRef($key) cannot be monitored\n");
183 0 };
184 }
185 }
186 }
187 }
188 }
189
190 4 ▶sub _monitorRecursively {
Subroutine _monitorRecursively: called CC=10 SCAR=31.1
191 4 my %values = @_;
192 4 foreach my $key (keys %values) {
193 4 my $varRef = $values{$key};
194 4 ▶ if ($varRef) { #If the value is undef
Branch: 1 branch
BranchTrueFalseTotal
if ($varRef) 4 0 4
195 4 _dereference(\$varRef);
196 4 ▶ if ($varRef =~ /HASH/ ) { #An hash object or an hash
Branch: 4 branches
BranchTrueFalseTotal
if ($varRef =~ /HASH/) { } 0 4 4
elsif ($varRef =~ /SCALAR/) { } 3 1 4
elsif ($varRef =~ /ARRAY/) { } 1 0 1
elsif ($varRef =~ /CODE/) { } 0 0 0
197 HASH_ITEM:
198 0 foreach my $item (keys %$varRef) {
199 #print STDERR "ITEM : ".$varRef->{$item}."\n";
200 0 _monitorRecursively("$key {$item}" => \($varRef->{$item}));
201 }
202 }
203 elsif ($varRef =~ /SCALAR/) {
204 # nothing
205 }
206 elsif ($varRef =~ /ARRAY/) {
207 1 ARRAY_ITEM:
208 my $i = 0;
209 1 foreach my $item (@$varRef) {
210 #print STDERR "ITEM : ".$item."\n";
211 3 _monitorRecursively("$key [$i]" => \$item);
212 3 $i++;
213 }
214 }
215 elsif ($varRef =~ /CODE/) {
216 # nothing
217 }
218 else {
219 0 my $runPatch = 0;
220 0 ▶ try {
Subroutine __ANON__: not called CC=1 SCAR=6.9
221 0 goto HASH_ITEM;
222 0 ▶ } otherwise {
Subroutine __ANON__: not called CC=1 SCAR=6.9
223 0 $runPatch = 1;
224 0 };
225 0 ▶ if ($runPatch) {
Branch: 1 branch
BranchTrueFalseTotal
if ($runPatch) 0 0 0
226 0 ▶ try {
Subroutine __ANON__: not called CC=1 SCAR=6.9
227 0 goto ARRAY_ITEM;
228 0 ▶ } otherwise {
Subroutine __ANON__: not called CC=1 SCAR=0.0
229 #Devel::Monitor::Common::printMsg("$varRef($key) cannot be monitored recursively\n");
230 #we call monitor, this one will print the error
231 0 };
232 }
233 }
234 # Finally, monitor current variable
235 4 monitor($key => $varRef, 1);
236 }
237 }
238 }
239
240 2 ▶sub _tieHash {
Subroutine _tieHash: called CC=3 SCAR=14.8
241 2 my $varRef = shift;
242 2 my $name = shift;
243 2 my $isCode = shift;
244
245 2 ▶ if (not tied %$varRef) {
Branch: 1 branch
BranchTrueFalseTotal
if (not tied %$varRef) { } 2 0 2
246 2 tie %$varRef, 'Devel::Monitor::Hash', $varRef, $name, $isCode;
247 } else {
248 0 my $self = tied %$varRef;
249 #if tied by our Devel::Monitor
250 0 ▶ if (ref($self) =~ /Devel::Monitor/) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref($self) =~ /Devel::Monitor/) { } 0 0 0
251 0 Devel::Monitor::Common::printMsg("Hash from $name is already tied by ".$self->{Devel::Monitor::Common::F_ID()}."\n");
252 } else {
253 0 Devel::Monitor::Common::printMsg("Array from $name is already tied by the ".ref($self)." package\n");
254 }
255 }
256 }
257
258 2 ▶sub _tieArray {
Subroutine _tieArray: called CC=3 SCAR=14.8
259 2 my $varRef = shift;
260 2 my $name = shift;
261 2 my $isCode = shift;
262
263 2 ▶ if (not tied @$varRef) {
Branch: 1 branch
BranchTrueFalseTotal
if (not tied @$varRef) { } 2 0 2
264 2 tie @$varRef, 'Devel::Monitor::Array', $varRef, $name, $isCode;
265 } else {
266 0 my $self = tied @$varRef;
267 #if tied by our Devel::Monitor
268 0 ▶ if (ref($self) =~ /Devel::Monitor/) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref($self) =~ /Devel::Monitor/) { } 0 0 0
269 0 Devel::Monitor::Common::printMsg("Array from $name is already tied by ".$self->{Devel::Monitor::Common::F_ID()}."\n");
270 } else {
271 0 Devel::Monitor::Common::printMsg("Array from $name is already tied by the ".ref($self)." package\n");
272 }
273 }
274 }
275
276 4 ▶sub _tieScalar {
Subroutine _tieScalar: called CC=3 SCAR=14.7
277 4 my $varRef = shift;
278 4 my $name = shift;
279 4 my $isCode = shift;
280
281 4 ▶ if (not tied $$varRef) {
Branch: 1 branch
BranchTrueFalseTotal
if (not tied $$varRef) { } 4 0 4
282 4 ▶ try {
Subroutine __ANON__: called CC=1 SCAR=0.0
283 4 tie $$varRef, 'Devel::Monitor::Scalar', $varRef, $name, $isCode;
284 0 ▶ } otherwise {
Subroutine __ANON__: not called CC=1 SCAR=6.9
285 0 Devel::Monitor::Common::printMsg("Scalar from $name is read-only, monitor skipped\n");
286 4 };
287 } else {
288 0 my $self = tied $$varRef;
289 #if tied by our Devel::Monitor
290 0 ▶ if (ref($self) =~ /Devel::Monitor/) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref($self) =~ /Devel::Monitor/) { } 0 0 0
291 0 Devel::Monitor::Common::printMsg("Scalar from $name is already tied by ".$self->{Devel::Monitor::Common::F_ID()}."\n");
292 } else {
293 0 Devel::Monitor::Common::printMsg("Array from $name is already tied by the ".ref($self)." package\n");
294 }
295 }
296 }
297
298 #Not used
299 # sub unmonitor {
300 # my @varsRef = @_;
301 # foreach my $varRef (@varsRef) {
302 # if ($varRef) {
303 # _dereference(\$varRef);
304 # if ($varRef =~ /HASH/ ) { #An object or an hash
305 # Devel::Monitor::Hash::unmonitor($varRef);
306 # }
307 # elsif ($varRef =~ /SCALAR/) {
308 # Devel::Monitor::Scalar::unmonitor($varRef);
309 # }
310 # elsif ($varRef =~ /ARRAY/) {
311 # Devel::Monitor::Array::unmonitor($varRef);
312 # }
313 # elsif ($varRef =~ /CODE/) {
314 # unmonitor(&$varRef); #TODO : Unmonitor recursively, do not touch scalars
315 # }
316 # else {
317 # my $runPatch = 0;
318 # try {
319 # Devel::Monitor::Hash::unmonitor($varRef);
320 # } otherwise {
321 # $runPatch = 1;
322 # };
323 # if ($runPatch) {
324 # try {
325 # Devel::Monitor::Array::unmonitor($varRef);
326 # } otherwise {
327 # Devel::Monitor::Common::printMsg("$varRef cannot be unmonitored\n");
328 # };
329 # }
330 # }
331 # }
332 # }
333 # }
334
335 13 ▶sub _dereference {
Subroutine _dereference: called CC=2 SCAR=6.9
336 13 my $varRefRef = shift;
337 13 my $type = ref($$varRefRef);
338 #print STDERR "VARIABLE : $$varRefRef\n";
339 #print STDERR "TYPE : $type\n";
340 ##############################################################
341 # You need to dereference, otherwise, you may
342 # get this error : Modification of a read-only value attempted
343 # is you monitor a variable that use a constant by example
344 ##############################################################
345 13 ▶ while ($type =~ /REF/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type =~ /REF/) 1 13 14
346 1 $$varRefRef = $$$varRefRef;
347 1 $type = ref($$varRefRef);
348 #print STDERR "V : $$varRefRef\n";
349 #print STDERR "T : $type\n";
350 }
351 }
352
353 # METH printCircularRef
354 #
355 # DESC Try to find circular references and print it out into STDERR
356  
357 #Little redirect to be "Perl compliant"
358 #TODO : use the underscore syntax
359 13 ▶sub print_circular_ref { return printCircularRef(@_); }
Subroutine print_circular_ref: called CC=1 SCAR=0.0
Pod undocumented
360  
361 158 ▶sub printCircularRef {
Subroutine printCircularRef: called CC=16 SCAR=27.8
Pod undocumented
362 158 my $varRef = shift;
363 158 my $hideWeakenedCircRef = shift; #Boolean
364 158 my $source = shift;
365 158 my $trace = shift; #A array container containing the current trace
366 158 my $weakenedRef = shift; #A array containing the trace to the weakened ref it any
367 158 my $origRef = shift; #Contains original reference to verify circular references
368 158 my $seenRef = shift;
369 158 my $circRefTypesRef = shift;
370
371 #print STDERR "###############################################################\n";
372 #print STDERR "VARIABLE : ".$varRef."\n";
373 #print STDERR "TYPE : ".ref($varRef)."\n";
374 158 my $isFirst = (!$origRef);
375 158 ▶ $trace = Devel::Monitor::Trace->new() if not $trace;
Branch: 1 branch
BranchTrueFalseTotal
unless $trace 13 145 158
376 158 ▶ $weakenedRef = [] if not $weakenedRef;
Branch: 1 branch
BranchTrueFalseTotal
unless $weakenedRef 13 145 158
377 158 ▶ $seenRef = {} if not $seenRef;
Branch: 1 branch
BranchTrueFalseTotal
unless $seenRef 13 145 158
378 158 ▶ $circRefTypesRef = [] if not $circRefTypesRef;
Branch: 1 branch
BranchTrueFalseTotal
unless $circRefTypesRef 13 145 158
379  
380 158 ▶ return undef if not $varRef;
Branch: 1 branch
BranchTrueFalseTotal
unless $varRef 0 158 158
381
382 158 my $isWeak = 0;
383 158 my $simpleSeenRef = {};
384 #Since we dereference scalars, they are not displayed on the final prints
385 158 ▶ while ($varRef =~ /REF/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($varRef =~ /REF/) 70 155 225
386 #print STDERR "DEREFERING $varRef ($$varRef)\n";
387 #print STDERR "Current variable : $varRef from ".\$varRef."\n";
388 70 ▶ if (isweak($$varRef)) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::is_weak($$varRef)) 0 70 70
389 0 $isWeak = 1;
390 #print STDERR "WEAK for $$varRef\n";
391 0 push(@$weakenedRef, $$varRef);
392 }
393 70 _addSeenRef($varRef,$simpleSeenRef);
394 #Exceptional case : $a = \$a or $a = \$b = \$c
395 #TODO : This "if" should not be handled as an exception (At least, we should try)
396 70 ▶ if (exists($simpleSeenRef->{$varRef}) && ($simpleSeenRef->{$varRef} > 1)) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $simpleSeenRef->{$varRef} and $simpleSeenRef->{$varRef} > 1) 3 67 70
Condition66%2/3
exists $simpleSeenRef->{$varRef} and $simpleSeenRef->{$varRef} > 1
!ll&&!rl&&r
0673
MC/DC50%1/2
exists $simpleSeenRef->{$varRef} and $simpleSeenRef->{$varRef} > 1
exists $simpleSeenRef->{$varRef} $simpleSeenRef->{$varRef} > 1
Truth table66%2/3
A and B
A= exists $simpleSeenRef->{$varRef}B= $simpleSeenRef->{$varRef} > 1
ABresult
0X0
100
111
397 3 ▶ if ($isFirst) {
Branch: 1 branch
BranchTrueFalseTotal
if ($isFirst) { } 2 1 3
398 2 _printCircularRefHeader($varRef);
399 2 push(@$circRefTypesRef, CRT_CIRC_REF());
400 2 Devel::Monitor::Common::printMsg("-------------------------------------------------------------------------------\n");
401 2 Devel::Monitor::Common::printMsg("Circular reference on scalar(s) starting at $varRef\n");
402 2 Devel::Monitor::Common::printMsg("-------------------------------------------------------------------------------\n");
403 2 _printCircularRefResults($varRef,$circRefTypesRef);
404 } else {
405 1 push(@$circRefTypesRef, CRT_INTERNAL_CIRC_REF());
406 1 Devel::Monitor::Common::printMsg("-------------------------------------------------------------------------------\n");
407 1 Devel::Monitor::Common::printMsg('Internal circular reference on scalar(s) starting at : '.$trace->getCircularPath()."\n");
408 1 $trace->dump();
409 1 Devel::Monitor::Common::printMsg("-------------------------------------------------------------------------------\n");
410 }
411 3 return undef;
412 }
413
414 67 $varRef = $$varRef;
415 }
416 155 $trace->push($varRef,$source);
417 155 ▶ _addSeenRef($varRef,$seenRef) if $origRef; #We skip the first item which is $origRef
Branch: 1 branch
BranchTrueFalseTotal
if $origRef 144 11 155
418 #print STDERR "--------------------------------------------\n";
419 #print STDERR "Current variable : $varRef from ".\$varRef."\n";
420 155 my $circRefType = _checkCircularRef($varRef,$hideWeakenedCircRef,$trace,$weakenedRef,$origRef,$seenRef);
421 #print STDERR "\$circRefType : $circRefType\n";
422 155 ▶ if ($circRefType) {
Branch: 1 branch
BranchTrueFalseTotal
if ($circRefType) 19 136 155
423 19 $trace->pop();
424 19 push(@$circRefTypesRef, $circRefType);
425 19 return undef; #Don't go any further because we loop
426 }
427 136 ▶ if ($isFirst) {
Branch: 1 branch
BranchTrueFalseTotal
if ($isFirst) 11 125 136
428 11 $origRef = $varRef;
429 11 _printCircularRefHeader($origRef);
430 }
431
432 #print STDERR 'Current trace : '.$trace->getCircularPath()."\n";
433 136 _printCircularRef($varRef,$hideWeakenedCircRef,$source,$trace,$weakenedRef,$origRef,$seenRef,$circRefTypesRef);
434
435 #We go into another branch
436 136 $trace->pop();
437 136 ▶ pop(@$weakenedRef) if $isWeak; # Remove weakened item
Branch: 1 branch
BranchTrueFalseTotal
if $isWeak 0 136 136
438 136 delete($seenRef->{$varRef}); # Remove varRef from "seen" hash
439
440 136 ▶ _printCircularRefResults($origRef,$circRefTypesRef) if $isFirst;
Branch: 1 branch
BranchTrueFalseTotal
if $isFirst 11 125 136
441
442 136 return undef;
443 }
444
445 136 ▶sub _printCircularRef {
Subroutine _printCircularRef: called CC=9 SCAR=24.8
446 136 my $varRef = shift;
447 136 my $hideWeakenedCircRef = shift;
448 136 my $source = shift;
449 136 my $trace = shift;
450 136 my $weakenedRef = shift;
451 136 my $origRef = shift;
452 136 my $seenRef = shift;
453 136 my $circRefTypesRef = shift;
454
455 136 ▶ if ($varRef =~ /HASH/ ) { #An object or an hash
Branch: 3 branches
BranchTrueFalseTotal
if ($varRef =~ /HASH/) { } 14 122 136
elsif ($varRef =~ /SCALAR|CODE/) { } 111 11 122
elsif ($varRef =~ /ARRAY/) { } 11 0 11
456 14 ▶ HASH_ITEM:
Branch: 1 branch
BranchTrueFalseTotal
if tied %$varRef 0 14 14
457 Devel::Monitor::Common::printMsg('Object '.$trace->getCircularPath().' = '.$varRef." is tied. Untie it to check circular references for this object.\n") if tied(%$varRef);
458 14 foreach my $item (keys %$varRef) {
459 23 my $ref = _getVarRef(\($varRef->{$item}));
460 23 printCircularRef($ref,$hideWeakenedCircRef,'{'.$item.'}',$trace,$weakenedRef,$origRef,$seenRef,$circRefTypesRef);
461 }
462 }
463 elsif ($varRef =~ /SCALAR|CODE/) {
464 #No circular references are possible here, so we don't do anything
465 }
466 elsif ($varRef =~ /ARRAY/) {
467 11 ▶ ARRAY_ITEM:
Branch: 1 branch
BranchTrueFalseTotal
if tied @$varRef 0 11 11
468 Devel::Monitor::Common::printMsg('Object '.$trace->getCircularPath().' = '.$varRef." is tied. Untie it to check circular references for this object.\n") if tied(@$varRef);
469 11 ▶ for (my $i=0; $i<scalar(@$varRef); $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < scalar @$varRef) 122 11 133
470 #print STDERR "CURRENT VAR : ".\($varRef->[$i])." ::: ".$varRef->[$i]."\n";
471 122 my $ref = _getVarRef(\($varRef->[$i]));
472 #Devel::Monitor::Common::printMsg('Object at '.$trace->getCircularPath().'['.$i.']'.
473 #" is ARRAY ARRAY ARRAY tied. We cannot check circular references for this object.\n") if $ref =~ /SCALAR/;
474 122 printCircularRef($ref,$hideWeakenedCircRef,'['.$i.']',$trace,$weakenedRef,$origRef,$seenRef,$circRefTypesRef);
475 }
476 } else {
477 #Other objects
478 0 my $runPatch = 0;
479 0 ▶ try {
Subroutine __ANON__: not called CC=1 SCAR=6.9
480 0 goto HASH_ITEM;
481 0 ▶ } otherwise {
Subroutine __ANON__: not called CC=1 SCAR=6.9
482 0 $runPatch = 1;
483 0 };
484 0 ▶ if ($runPatch) {
Branch: 1 branch
BranchTrueFalseTotal
if ($runPatch) 0 0 0
485 0 ▶ try {
Subroutine __ANON__: not called CC=1 SCAR=6.9
486 0 goto ARRAY_ITEM;
487 0 ▶ } otherwise {
Subroutine __ANON__: not called CC=1 SCAR=6.9
488 0 die("Cannot verify circular references for $varRef of type ".ref($varRef)."\n");
489 0 };
490 }
491 }
492 }
493  
494 13 ▶sub _printCircularRefHeader {
Subroutine _printCircularRefHeader: called CC=1 SCAR=0.0
495 13 my $origRef = shift;
496
497 13 Devel::Monitor::Common::printMsg("-------------------------------------------------------------------------------\n");
498 13 Devel::Monitor::Common::printMsg("Checking circular references for $origRef\n");
499 }
500
501 13 ▶sub _printCircularRefResults {
Subroutine _printCircularRefResults: called CC=5 SCAR=16.1
502 13 my ($origRef, $circRefTypesRef) = @_;
503
504 13 my $circRefsCount = 0;
505 13 my $internalCircRefsCount = 0;
506 13 my $weakCircRefsCount = 0;
507 13 foreach my $crt (@$circRefTypesRef) {
508 22 ▶ $weakCircRefsCount++ if $crt == CRT_WEAK_CIRC_REF();
Branch: 1 branch
BranchTrueFalseTotal
if $crt == 3 0 22 22
509 22 ▶ $circRefsCount++ if $crt == CRT_CIRC_REF();
Branch: 1 branch
BranchTrueFalseTotal
if $crt == 1 15 7 22
510 22 ▶ $internalCircRefsCount++ if $crt == CRT_INTERNAL_CIRC_REF();
Branch: 1 branch
BranchTrueFalseTotal
if $crt == 2 7 15 22
511 }
512 13 Devel::Monitor::Common::printMsg("-------------------------------------------------------------------------------\n");
513 13 Devel::Monitor::Common::printMsg("Results for $origRef\n");
514 13 Devel::Monitor::Common::printMsg("Circular reference : $circRefsCount\n");
515 13 Devel::Monitor::Common::printMsg("Internal circular reference : $internalCircRefsCount\n");
516 13 Devel::Monitor::Common::printMsg("Weak circular reference : $weakCircRefsCount\n");
517 13 Devel::Monitor::Common::printMsg("-------------------------------------------------------------------------------\n");
518 }
519
520 # METH _checkCircularRef
521 #
522 # DESC Verify if there is a circular reference on the current variable
523 # RETV Circular Reference Type
524 # One of : CRT_NONE, CRT_CIRC_REF, CRT_WEAK_CIRC_REF, CRT_INTERNAL_CIRC_REF
525  
526 155 ▶sub _checkCircularRef {
Subroutine _checkCircularRef: called CC=17 SCAR=32.7
527 155 my $varRef = shift;
528 155 my $hideWeakenedCircRef = shift;
529 155 my $trace = shift;
530 155 my $weakenedRef = shift;
531 155 my $origRef = shift;
532 155 my $seenRef = shift;
533 155 ▶ if ($varRef) {
Branch: 1 branch
BranchTrueFalseTotal
if ($varRef) 155 0 155
534 #print STDERR "\$varRef : $varRef\n";
535 #print STDERR "\$origRef : $origRef\n";
536 155 ▶ if ($origRef) {
Branch: 1 branch
BranchTrueFalseTotal
if ($origRef) 144 11 155
537 #If we found the original reference
538 144 my $isCircRef = ($varRef eq $origRef);
539 #If we found a reference more than one time, it means we loop infinitely
540 144 ▶ my $isInternalCircRef = (exists($seenRef->{$varRef}) && ($seenRef->{$varRef} > 1));
Condition66%2/3
exists $seenRef->{$varRef} && $seenRef->{$varRef} > 1
!ll&&!rl&&r
013113
MC/DC50%1/2
exists $seenRef->{$varRef} && $seenRef->{$varRef} > 1
exists $seenRef->{$varRef} $seenRef->{$varRef} > 1
Truth table66%2/3
A && B
A= exists $seenRef->{$varRef}B= $seenRef->{$varRef} > 1
ABresult
0X0
100
111
541
542 144 ▶ if ($isCircRef || $isInternalCircRef) {
Branch: 1 branch
BranchTrueFalseTotal
if ($isCircRef or $isInternalCircRef) 19 125 144
Condition100%3/3
$isCircRef or $isInternalCircRef
l!l&&r!l&&!r
136125
MC/DC100%2/2
$isCircRef or $isInternalCircRef
$isCircRef $isInternalCircRef
Truth table100%3/3
A or B
A= $isCircRefB= $isInternalCircRef
ABresult
1X1
011
000
543 19 my $weakenedInCircRefRef = _getWeakenedInCircRef($trace,$weakenedRef);
544 19 my $isWeakenedItems = (scalar(@$weakenedInCircRefRef) > 0);
545 19 ▶ if (!$hideWeakenedCircRef || #If we show everything
Branch: 2 branches
BranchTrueFalseTotal
if (not $hideWeakenedCircRef or $hideWeakenedCircRef and not $isWeakenedItems) { } 19 0 19
elsif ($hideWeakenedCircRef and $isWeakenedItems) { } 0 0 0
Condition16%1/6
not $hideWeakenedCircRef or $hideWeakenedCircRef and not $isWeakenedItems
l!l&&r!l&&!r
1900
$hideWeakenedCircRef and not $isWeakenedItems
!ll&&!rl&&r
000
MC/DC0%0/3
not $hideWeakenedCircRef or $hideWeakenedCircRef and not $isWeakenedItems
not $hideWeakenedCircRef $hideWeakenedCircRef not $isWeakenedItems
Truth table25%1/4
not(A) or B and not(C)
A= not $hideWeakenedCircRefB= $hideWeakenedCircRefC= not $isWeakenedItems
ABCresult
1XX1
0111
00X0
0100
546 ($hideWeakenedCircRef && !$isWeakenedItems)) { #Otherwise, if there is no weak reference
547
548 19 Devel::Monitor::Common::printMsg("-------------------------------------------------------------------------------\n");
549 19 ▶ if ($isCircRef) {
Branch: 2 branches
BranchTrueFalseTotal
if ($isCircRef) { } 13 6 19
elsif ($isInternalCircRef) { } 6 0 6
550 13 Devel::Monitor::Common::printMsg('Circular reference found : '.$trace->getCircularPath()."\n");
551 }
552 elsif ($isInternalCircRef) {
553 6 Devel::Monitor::Common::printMsg('Internal circular reference found : '.$trace->getCircularPath()." on $varRef\n");
554 }
555 19 ▶ if ($isWeakenedItems) {
Branch: 1 branch
BranchTrueFalseTotal
if ($isWeakenedItems) 0 19 19
556 0 Devel::Monitor::Common::printMsg('with weakened reference on : '.join(', ', @$weakenedInCircRefRef)."\n");
557 }
558 19 $trace->dump();
559 19 Devel::Monitor::Common::printMsg("-------------------------------------------------------------------------------\n");
560 19 ▶ return CRT_WEAK_CIRC_REF() if $isWeakenedItems;
Branch: 1 branch
BranchTrueFalseTotal
if $isWeakenedItems 0 19 19
561 19 ▶ return CRT_CIRC_REF() if $isCircRef;
Branch: 1 branch
BranchTrueFalseTotal
if $isCircRef 13 6 19
562 6 ▶ return CRT_INTERNAL_CIRC_REF() if $isInternalCircRef;
Branch: 1 branch
BranchTrueFalseTotal
if $isInternalCircRef 6 0 6
563 0 die("_checkCircularRef : Should not be here (1)\n");
564 }
565 ▶ elsif ($hideWeakenedCircRef && $isWeakenedItems) {
Condition0%0/3
$hideWeakenedCircRef and $isWeakenedItems
!ll&&!rl&&r
000
MC/DC0%0/2
$hideWeakenedCircRef and $isWeakenedItems
$hideWeakenedCircRef $isWeakenedItems
Truth table0%0/3
A and B
A= $hideWeakenedCircRefB= $isWeakenedItems
ABresult
0X0
100
111
566 0 return CRT_WEAK_CIRC_REF();
567 }
568 }
569 }
570 136 return CRT_NONE();
571 }
572 0 die("_checkCircularRef : \$varRef is undefined\n");
573 }
574  
575 214 ▶sub _addSeenRef {
Subroutine _addSeenRef: called CC=2 SCAR=6.9
576 214 my $varRef = shift;
577 214 my $seenRef = shift;
578 #print STDERR "_addSeenRef: $varRef\n";
579 214 ▶ if (exists($seenRef->{$varRef})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $seenRef->{$varRef}) { } 16 198 214
580 16 $seenRef->{$varRef}++;
581 } else {
582 198 $seenRef->{$varRef} = 1;
583 }
584 }
585  
586 145 ▶sub _getVarRef {
Subroutine _getVarRef: called CC=1 SCAR=0.0
587 145 my $varRef = shift;
588 ###########################################################
589 # We cannot use tied objects because it reuse memory space
590 ###########################################################
591 # use Tie::Hash;
592 #
593 # my $self = {'a' => 1,
594 # 'b' => 2};
595 # #monitor('self' => \$self);
596 # tie %$self, 'Tie::StdHash';
597 # print STDERR \($self->{'a'})."\n";
598 # print STDERR \($self->{'b'})."\n";
599 # print STDERR \($self->{'a'}).\($self->{'b'})."\n";
600 # foreach my $key (keys %$self) {
601 # my $keyRef = \$key;
602 # my $value = $self->{$key};
603 # my $valueRef = \($self->{$key});
604 # print STDERR "KEY:$key, KEY REF:$keyRef, VALUE:$value, VALUE REF:$valueRef\n";
605 # }
606 ###########################################################
607 # Output
608 ###########################################################
609 # MONITOR HASH : self
610 # SCALAR(0x8141384)
611 # SCALAR(0x8141384)
612 # SCALAR(0x8141384)SCALAR(0x81413cc)
613 # KEY:a, KEY REF:SCALAR(0x8141420), VALUE:1, VALUE REF:SCALAR(0x824becc)
614 # KEY:b, KEY REF:SCALAR(0x81413cc), VALUE:2, VALUE REF:SCALAR(0x824becc)
615 # DESTROY HASH : self
616 ###########################################################
617 # We see clearly that it reuse memory space instead of
618 # refering to the original values from the untied object
619 ###########################################################
620 145 my $ref;
621 #if ($$varRef &&
622 # ($varRef =~ /SCALAR/) &&
623 # ($$varRef =~ /(ARRAY|HASH)/)) {
624 # $ref = $$varRef;
625 #} else {
626 145 $ref = $varRef;
627 #}
628 145 return $ref;
629 }
630  
631 19 ▶sub _getWeakenedInCircRef {
Subroutine _getWeakenedInCircRef: called CC=5 SCAR=17.1
632 19 my $trace = shift;
633 19 my $weakenedRef = shift;
634  
635 19 my @weakenedInCircRef;
636 19 my $traceItemsRef = $trace->getTraceItems;
637 #The last item represent the circular reference
638 19 my $traceItemCircRef = $traceItemsRef->[$#$traceItemsRef];
639 #for my $i (($#$traceItemsRef-1)..0) {
640 19 ▶ for (my $i=($#$traceItemsRef-1); $i>=0; $i--) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i >= 0) 45 0 45
641 #Get the current item
642 45 my $traceItem = $traceItemsRef->[$i];
643 #print STDERR "traceItem ".$traceItem->getVarRef()."\n";
644 #We verify that the item is a weaken reference or not
645 45 foreach my $weakened (@$weakenedRef) {
646 #print STDERR "weakened ".$weakened."\n";
647 0 ▶ if ($traceItem->getVarRef() eq $weakened) {
Branch: 1 branch
BranchTrueFalseTotal
if ($traceItem->getVarRef eq $weakened) 0 0 0
648 #print STDERR "push\n";
649 0 push(@weakenedInCircRef, $weakened);
650 }
651 }
652 #We finish when we end the circular reference
653 45 ▶ last if ($traceItem->getVarRef() eq $traceItemCircRef->getVarRef());
Branch: 1 branch
BranchTrueFalseTotal
if $traceItem->getVarRef eq $traceItemCircRef->getVarRef 19 26 45
654 }
655 #print STDERR "RETURN ".join(', ', @weakenedInCircRef)."\n";
656 19 return \@weakenedInCircRef;
657 }
658
659 1 1;
660  
661 __END__
↑ Summary Monitor/Array.pm »