Visitor.pm

filter: Statementstmt 86.7% 209 / 241 Branchbran 71.6% 53 / 74 Conditioncond 44.1% 19 / 43 MC/DCmcdc 33.3% 9 / 27 Subroutinesub 92.6% 38 / 41 Podpod 71.4% 20 / 28 totaltotal 76.6% 348 / 454 CC 60 SCAR 26.2
CC
60
Cov
78%
retain_magic
79.6
trace
12.0
visit
10.5
CRAP 13.8
↑ Summary Visitor/Callback.pm »
1 package Data::Visitor; # git description: v0.31-4-g7498abb
2 9 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 # ABSTRACT: Visitor style traversal of Perl data structures
4  
5 9 our $VERSION = '0.32';
6 9 ▶use Scalar::Util qw/blessed refaddr reftype weaken isweak/;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 9 ▶use overload ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 9 ▶use Symbol ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 9 ▶use Tie::ToObject;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 9 ▶no warnings 'recursion';
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 9 ▶use namespace::clean -except => 'meta';
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 # the double not makes this no longer undef, so exempt from useless constant warnings in older perls
17 9 ▶use constant DEBUG => not not our $DEBUG || $ENV{DATA_VISITOR_DEBUG};
Subroutine BEGIN: called CC=2 SCAR=7.6
Condition33%1/3
our $DEBUG || $ENV{'DATA_VISITOR_DEBUG'}
l!l&&r!l&&!r
009
MC/DC0%0/2
our $DEBUG || $ENV{'DATA_VISITOR_DEBUG'}
our $DEBUG $ENV{'DATA_VISITOR_DEBUG'}
Truth table33%1/3
A || B
A= our $DEBUGB= $ENV{'DATA_VISITOR_DEBUG'}
ABresult
1X1
011
000
18  
19 0 ▶use constant HAS_DATA_ALIAS => eval { +require Data::Alias; 1 };
Subroutine BEGIN: called CC=1 SCAR=0.1
20  
21 9 has tied_as_objects => (
22 isa => "Bool",
23 is => "rw",
24 );
25  
26 # currently broken
27 9 has weaken => (
28 isa => "Bool",
29 is => "rw",
30 default => 0,
31 );
32  
33 0 ▶sub trace {
Subroutine trace: not called CC=3 SCAR=24.8
Pod documented
34 0 my ( $self, $category, @msg ) = @_;
35  
36 0 our %DEBUG;
37  
38 0 ▶ if ( $DEBUG{$category} or !exists($DEBUG{$category}) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Data::Visitor::DEBUG{$category} or not exists $Data::Visitor::DEBUG{$category}) 0 0 0
Condition0%0/3
$Data::Visitor::DEBUG{$category} or not exists $Data::Visitor::DEBUG{$category}
l!l&&r!l&&!r
000
MC/DC0%0/2
$Data::Visitor::DEBUG{$category} or not exists $Data::Visitor::DEBUG{$category}
$Data::Visitor::DEBUG{$category} not exists $Data::Visitor::DEBUG{$category}
Truth table0%0/3
A or not(B)
A= $Data::Visitor::DEBUG{$category}B= not exists $Data::Visitor::DEBUG{$category}
ABresult
1X1
011
000
39 $self->_print_trace("$self: " . join("",
40 ( " " x ( $self->{depth} - 1 ) ),
41 0 ( join(" ", "$category:", map { overload::StrVal($_) } @msg) ),
42 ));
43 }
44 }
45  
46 0 ▶sub _print_trace {
Subroutine _print_trace: not called CC=1 SCAR=6.9
47 0 my ( $self, @msg ) = @_;
48 0 warn "@msg\n";
49 }
50  
51 367 ▶sub visit {
Subroutine visit: called CC=10 SCAR=23.5
Pod documented
52 367 my $self = shift;
53  
54 367 local $self->{depth} = (($self->{depth}||0) + 1) if DEBUG;
55 367 ▶ my $seen_hash = local $self->{_seen} = ($self->{_seen} || {}); # delete it after we're done with the whole visit
Condition100%2/2
$self->{'_seen'} || {}
l!l
29275
MC/DC100%1/1
$self->{'_seen'} || {}
$self->{'_seen'}
Truth table100%2/2
A
A= $self->{'_seen'}
Aresult
11
00
56  
57 367 my @ret;
58  
59 367 foreach my $data ( @_ ) {
60 367 $self->trace( flow => visit => $data ) if DEBUG;
61  
62 367 ▶ if ( my $refaddr = ref($data) && refaddr($data) ) { # only references need recursion checks
Branch: 1 branch
BranchTrueFalseTotal
if (my $refaddr = ref $data && builtin::refaddr($data)) 175 192 367
Condition66%2/3
ref $data && builtin::refaddr($data)
!ll&&!rl&&r
1920175
MC/DC50%1/2
ref $data && builtin::refaddr($data)
ref $data builtin::refaddr($data)
Truth table66%2/3
A && B
A= ref $dataB= builtin::refaddr($data)
ABresult
0X0
100
111
63 175 ▶ $seen_hash->{weak} ||= isweak($data) if $self->weaken;
Branch: 1 branch
BranchTrueFalseTotal
if $self->weaken 0 175 175
Condition0%0/3
$seen_hash->{"weak"} ||= builtin::is_weak($data)
l!l&&r!l&&!r
000
MC/DC0%0/2
$seen_hash->{"weak"} ||= builtin::is_weak($data)
$seen_hash->{"weak"} builtin::is_weak($data)
Truth table0%0/3
A ||= B
A= $seen_hash->{"weak"}B= builtin::is_weak($data)
ABresult
1X1
011
000
64  
65 175 ▶ if ( exists $seen_hash->{$refaddr} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $seen_hash->{$refaddr}) { } 12 163 175
66 12 $self->trace( mapping => found_mapping => from => $data, to => $seen_hash->{$refaddr} ) if DEBUG;
67 12 push @ret, $self->visit_seen( $data, $seen_hash->{$refaddr} );
68 12 next;
69 } else {
70 163 $self->trace( mapping => no_mapping => $data ) if DEBUG;
71 }
72 }
73  
74 355 ▶ if ( defined wantarray ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined wantarray) { } 300 55 355
75 300 push @ret, scalar($self->visit_no_rec_check($data));
76 } else {
77 55 $self->visit_no_rec_check($data);
78 }
79 }
80  
81 367 ▶ return ( @_ == 1 ? $ret[0] : @ret );
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 ? : 367 0 367
82 }
83  
84 3 ▶sub visit_seen {
Subroutine visit_seen: called CC=1 SCAR=0.0
Pod documented
85 3 my ( $self, $data, $result ) = @_;
86 3 return $result;
87 }
88  
89 0 ▶sub _get_mapping {
Subroutine _get_mapping: not called CC=1 SCAR=6.9
90 0 my ( $self, $data ) = @_;
91 0 $self->{_seen}{ refaddr($data) };
92 }
93  
94 193 ▶sub _register_mapping {
Subroutine _register_mapping: called CC=2 SCAR=7.0
95 193 my ( $self, $data, $new_data ) = @_;
96 193 ▶ return $new_data unless ref $data;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $data 0 193 193
97 193 $self->trace( mapping => register_mapping => from => $data, to => $new_data, in => (caller(1))[3] ) if DEBUG;
98 193 $self->{_seen}{ refaddr($data) } = $new_data;
99 }
100  
101 355 ▶sub visit_no_rec_check {
Subroutine visit_no_rec_check: called CC=3 SCAR=11.0
Pod documented
102 355 my ( $self, $data ) = @_;
103  
104 355 ▶ if ( blessed($data) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (builtin::blessed($data)) { } 35 320 355
elsif (ref $data) { } 128 192 320
105 35 return $self->visit_object($_[1]);
106 } elsif ( ref $data ) {
107 128 return $self->visit_ref($_[1]);
108 }
109  
110 192 return $self->visit_value($_[1]);
111 }
112  
113 10 ▶sub visit_object {
Subroutine visit_object: called CC=2 SCAR=6.9
Pod documented
114 10 my ( $self, $object ) = @_;
115 10 $self->trace( flow => visit_object => $object ) if DEBUG;
116  
117 10 ▶ if ( not defined wantarray ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined wantarray) { } 5 5 10
118 5 $self->_register_mapping( $object, $object );
119 5 $self->visit_value($_[1]);
120 5 return;
121 } else {
122 5 return $self->_register_mapping( $object, $self->visit_value($_[1]) );
123 }
124 }
125  
126 137 ▶sub visit_ref {
Subroutine visit_ref: called CC=3 SCAR=11.2
Pod documented
127 137 my ( $self, $data ) = @_;
128  
129 137 local $self->{depth} = (($self->{depth}||0) + 1) if DEBUG;
130  
131 137 $self->trace( flow => visit_ref => $data ) if DEBUG;
132  
133 137 my $reftype = reftype $data;
134  
135 137 ▶ $reftype = "SCALAR" if $reftype =~ /^(?:REF|LVALUE|VSTRING)$/;
Branch: 1 branch
BranchTrueFalseTotal
if $reftype =~ /^(?:REF|LVALUE|VSTRING)$/ 0 137 137
136  
137 137 ▶ my $method = $self->can(lc "visit_$reftype") || "visit_value";
Condition50%1/2
$self->can(lc "visit_$reftype") || 'visit_value'
l!l
1370
MC/DC0%0/1
$self->can(lc "visit_$reftype") || 'visit_value'
$self->can(lc "visit_$reftype")
Truth table50%1/2
A
A= $self->can(lc "visit_$reftype")
Aresult
11
00
138  
139 137 return $self->$method($_[1]);
140 }
141  
142 38 ▶sub visit_value {
Subroutine visit_value: called CC=1 SCAR=0.0
Pod documented
143 38 my ( $self, $value ) = @_;
144 38 $self->trace( flow => visit_value => $value ) if DEBUG;
145 38 return $value;
146 }
147  
148 61 ▶sub visit_hash {
Subroutine visit_hash: called CC=3 SCAR=11.0
Pod documented
149 61 my ( $self, $hash ) = @_;
150  
151 61 local $self->{depth} = (($self->{depth}||0) + 1) if DEBUG;
152  
153 61 ▶ if ( defined(tied(%$hash)) and $self->tied_as_objects ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined tied %$hash and $self->tied_as_objects) { } 5 56 61
Condition100%3/3
defined tied %$hash and $self->tied_as_objects
!ll&&!rl&&r
5515
MC/DC100%2/2
defined tied %$hash and $self->tied_as_objects
defined tied %$hash $self->tied_as_objects
Truth table100%3/3
A and B
A= defined tied %$hashB= $self->tied_as_objects
ABresult
0X0
100
111
154 5 return $self->visit_tied_hash(tied(%$hash), $_[1]);
155 } else {
156 56 return $self->visit_normal_hash($_[1]);
157 }
158 }
159  
160 56 ▶sub visit_normal_hash {
Subroutine visit_normal_hash: called CC=2 SCAR=6.9
Pod undocumented
161 56 my ( $self, $hash ) = @_;
162  
163 56 ▶ if ( defined wantarray ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined wantarray) { } 51 5 56
164 51 my $new_hash = {};
165 51 $self->_register_mapping( $hash, $new_hash );
166  
167 51 %$new_hash = $self->visit_hash_entries($_[1]);
168  
169 51 return $self->retain_magic( $_[1], $new_hash );
170 } else {
171 5 $self->_register_mapping($hash, $hash);
172 5 $self->visit_hash_entries($_[1]);
173 5 return;
174 }
175 }
176  
177 5 ▶sub visit_tied_hash {
Subroutine visit_tied_hash: called CC=3 SCAR=11.0
Pod undocumented
178 5 my ( $self, $tied, $hash ) = @_;
179  
180 5 ▶ if ( defined wantarray ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined wantarray) { } 4 1 5
181 4 my $new_hash = {};
182 4 $self->_register_mapping( $hash, $new_hash );
183  
184 4 ▶ if ( blessed(my $new_tied = $self->visit_tied($_[1], $_[2])) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed(my $new_tied = $self->visit_tied($_[1], $_[2]))) { } 4 0 4
185 4 $self->trace( data => tying => var => $new_hash, to => $new_tied ) if DEBUG;
186 4 tie %$new_hash, 'Tie::ToObject', $new_tied;
187 4 return $self->retain_magic($_[2], $new_hash);
188 } else {
189 0 return $self->visit_normal_hash($_[2]);
190 }
191 } else {
192 1 $self->_register_mapping($hash, $hash);
193 1 $self->visit_tied($_[1], $_[2]);
194 1 return;
195 }
196 }
197  
198 56 ▶sub visit_hash_entries {
Subroutine visit_hash_entries: called CC=3 SCAR=11.0
Pod documented
199 56 my ( $self, $hash ) = @_;
200  
201 56 ▶ if ( not defined wantarray ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined wantarray) { } 5 51 56
202 5 $self->visit_hash_entry( $_, $hash->{$_}, $hash ) for keys %$hash;
203 } else {
204 51 return map { $self->visit_hash_entry( $_, $hash->{$_}, $hash ) } keys %$hash;
205 }
206 }
207  
208 120 ▶sub visit_hash_entry {
Subroutine visit_hash_entry: called CC=2 SCAR=6.9
Pod documented
209 120 my ( $self, $key, $value, $hash ) = @_;
210  
211 120 $self->trace( flow => visit_hash_entry => key => $key, value => $value ) if DEBUG;
212  
213 120 ▶ if ( not defined wantarray ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined wantarray) { } 3 117 120
214 3 $self->visit_hash_key($key,$value,$hash);
215 3 $self->visit_hash_value($_[2],$key,$hash);
216 } else {
217 return (
218 117 $self->visit_hash_key($key,$value,$hash),
219 $self->visit_hash_value($_[2],$key,$hash),
220 );
221 }
222 }
223  
224 118 ▶sub visit_hash_key {
Subroutine visit_hash_key: called CC=1 SCAR=0.0
Pod documented
225 118 my ( $self, $key, $value, $hash ) = @_;
226 118 $self->visit($key);
227 }
228  
229 120 ▶sub visit_hash_value {
Subroutine visit_hash_value: called CC=1 SCAR=0.0
Pod documented
230 120 my ( $self, $value, $key, $hash ) = @_;
231 120 $self->visit($_[1]);
232 }
233  
234 23 ▶sub visit_array {
Subroutine visit_array: called CC=3 SCAR=11.0
Pod documented
235 23 my ( $self, $array ) = @_;
236  
237 23 ▶ if ( defined(tied(@$array)) and $self->tied_as_objects ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined tied @$array and $self->tied_as_objects) { } 4 19 23
Condition66%2/3
defined tied @$array and $self->tied_as_objects
!ll&&!rl&&r
1904
MC/DC50%1/2
defined tied @$array and $self->tied_as_objects
defined tied @$array $self->tied_as_objects
Truth table66%2/3
A and B
A= defined tied @$arrayB= $self->tied_as_objects
ABresult
0X0
100
111
238 4 return $self->visit_tied_array(tied(@$array), $_[1]);
239 } else {
240 19 return $self->visit_normal_array($_[1]);
241 }
242 }
243  
244 19 ▶sub visit_normal_array {
Subroutine visit_normal_array: called CC=2 SCAR=6.9
Pod undocumented
245 19 my ( $self, $array ) = @_;
246  
247 19 ▶ if ( defined wantarray ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined wantarray) { } 16 3 19
248 16 my $new_array = [];
249 16 $self->_register_mapping( $array, $new_array );
250  
251 16 @$new_array = $self->visit_array_entries($_[1]);
252  
253 16 return $self->retain_magic( $_[1], $new_array );
254 } else {
255 3 $self->_register_mapping( $array, $array );
256 3 $self->visit_array_entries($_[1]);
257  
258 3 return;
259 }
260 }
261  
262 4 ▶sub visit_tied_array {
Subroutine visit_tied_array: called CC=3 SCAR=11.0
Pod undocumented
263 4 my ( $self, $tied, $array ) = @_;
264  
265 4 ▶ if ( defined wantarray ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined wantarray) { } 3 1 4
266 3 my $new_array = [];
267 3 $self->_register_mapping( $array, $new_array );
268  
269 3 ▶ if ( blessed(my $new_tied = $self->visit_tied($_[1], $_[2])) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed(my $new_tied = $self->visit_tied($_[1], $_[2]))) { } 3 0 3
270 3 $self->trace( data => tying => var => $new_array, to => $new_tied ) if DEBUG;
271 3 tie @$new_array, 'Tie::ToObject', $new_tied;
272 3 return $self->retain_magic($_[2], $new_array);
273 } else {
274 0 return $self->visit_normal_array($_[2]);
275 }
276 } else {
277 1 $self->_register_mapping( $array, $array );
278 1 $self->visit_tied($_[1], $_[2]);
279  
280 1 return;
281 }
282 }
283  
284 19 ▶sub visit_array_entries {
Subroutine visit_array_entries: called CC=3 SCAR=11.0
Pod documented
285 19 my ( $self, $array ) = @_;
286  
287 19 ▶ if ( not defined wantarray ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined wantarray) { } 3 16 19
288 3 $self->visit_array_entry( $array->[$_], $_, $array ) for 0 .. $#$array;
289 } else {
290 16 return map { $self->visit_array_entry( $array->[$_], $_, $array ) } 0 .. $#$array;
291 }
292 }
293  
294 25 ▶sub visit_array_entry {
Subroutine visit_array_entry: called CC=1 SCAR=0.0
Pod documented
295 25 my ( $self, $value, $index, $array ) = @_;
296 25 $self->visit($_[1]);
297 }
298  
299 11 ▶sub visit_scalar {
Subroutine visit_scalar: called CC=3 SCAR=11.0
Pod documented
300 11 my ( $self, $scalar ) = @_;
301  
302 11 ▶ if ( defined(tied($$scalar)) and $self->tied_as_objects ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined tied $$scalar and $self->tied_as_objects) { } 4 7 11
Condition66%2/3
defined tied $$scalar and $self->tied_as_objects
!ll&&!rl&&r
704
MC/DC50%1/2
defined tied $$scalar and $self->tied_as_objects
defined tied $$scalar $self->tied_as_objects
Truth table66%2/3
A and B
A= defined tied $$scalarB= $self->tied_as_objects
ABresult
0X0
100
111
303 4 return $self->visit_tied_scalar(tied($$scalar), $_[1]);
304 } else {
305 7 return $self->visit_normal_scalar($_[1]);
306 }
307 }
308  
309 7 ▶sub visit_normal_scalar {
Subroutine visit_normal_scalar: called CC=2 SCAR=6.9
Pod undocumented
310 7 my ( $self, $scalar ) = @_;
311  
312 7 ▶ if ( defined wantarray ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined wantarray) { } 6 1 7
313 6 my $new_scalar;
314 6 $self->_register_mapping( $scalar, \$new_scalar );
315  
316 6 $new_scalar = $self->visit( $$scalar );
317  
318 6 return $self->retain_magic($_[1], \$new_scalar);
319 } else {
320 1 $self->_register_mapping( $scalar, $scalar );
321 1 $self->visit( $$scalar );
322 1 return;
323 }
324  
325 }
326  
327 4 ▶sub visit_tied_scalar {
Subroutine visit_tied_scalar: called CC=3 SCAR=11.0
Pod undocumented
328 4 my ( $self, $tied, $scalar ) = @_;
329  
330 4 ▶ if ( defined wantarray ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined wantarray) { } 3 1 4
331 3 my $new_scalar;
332 3 $self->_register_mapping( $scalar, \$new_scalar );
333  
334 3 ▶ if ( blessed(my $new_tied = $self->visit_tied($_[1], $_[2])) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed(my $new_tied = $self->visit_tied($_[1], $_[2]))) { } 3 0 3
335 3 $self->trace( data => tying => var => $new_scalar, to => $new_tied ) if DEBUG;
336 3 tie $new_scalar, 'Tie::ToObject', $new_tied;
337 3 return $self->retain_magic($_[2], \$new_scalar);
338 } else {
339 0 return $self->visit_normal_scalar($_[2]);
340 }
341 } else {
342 1 $self->_register_mapping( $scalar, $scalar );
343 1 $self->visit_tied($_[1], $_[2]);
344 1 return;
345 }
346 }
347  
348 34 ▶sub visit_code {
Subroutine visit_code: called CC=1 SCAR=0.0
Pod documented
349 34 my ( $self, $code ) = @_;
350 34 $self->visit_value($_[1]);
351 }
352  
353 6 ▶sub visit_glob {
Subroutine visit_glob: called CC=3 SCAR=11.0
Pod documented
354 6 my ( $self, $glob ) = @_;
355  
356 6 ▶ if ( defined(tied(*$glob)) and $self->tied_as_objects ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined tied *$glob and $self->tied_as_objects) { } 4 2 6
Condition66%2/3
defined tied *$glob and $self->tied_as_objects
!ll&&!rl&&r
204
MC/DC50%1/2
defined tied *$glob and $self->tied_as_objects
defined tied *$glob $self->tied_as_objects
Truth table66%2/3
A and B
A= defined tied *$globB= $self->tied_as_objects
ABresult
0X0
100
111
357 4 return $self->visit_tied_glob(tied(*$glob), $_[1]);
358 } else {
359 2 return $self->visit_normal_glob($_[1]);
360 }
361 }
362  
363 2 ▶sub visit_normal_glob {
Subroutine visit_normal_glob: called CC=6 SCAR=19.9
Pod undocumented
364 2 my ( $self, $glob ) = @_;
365  
366 2 ▶ if ( defined wantarray ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined wantarray) { } 2 0 2
367 2 my $new_glob = Symbol::gensym();
368 2 $self->_register_mapping( $glob, $new_glob );
369  
370 9 ▶ no warnings 'misc'; # Undefined value assigned to typeglob
Subroutine BEGIN: called CC=1 SCAR=0.0
371 2 ▶ *$new_glob = $self->visit( *$glob{$_} || next ) for qw/SCALAR ARRAY HASH/;
Condition100%2/2
*$glob{$_} || (next)
l!l
42
MC/DC100%1/1
*$glob{$_} || (next)
*$glob{$_}
Truth table100%2/2
A
A= *$glob{$_}
Aresult
11
00
372  
373 2 return $self->retain_magic($_[1], $new_glob);
374 } else {
375 0 $self->_register_mapping( $glob, $glob );
376 0 ▶ $self->visit( *$glob{$_} || next ) for qw/SCALAR ARRAY HASH/;
Condition0%0/2
*$glob{$_} || (next)
l!l
00
MC/DC0%0/1
*$glob{$_} || (next)
*$glob{$_}
Truth table0%0/2
A
A= *$glob{$_}
Aresult
11
00
377 0 return;
378 }
379 }
380  
381 4 ▶sub visit_tied_glob {
Subroutine visit_tied_glob: called CC=3 SCAR=11.0
Pod undocumented
382 4 my ( $self, $tied, $glob ) = @_;
383  
384 4 ▶ if ( defined wantarray ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined wantarray) { } 3 1 4
385 3 my $new_glob = Symbol::gensym();
386 3 $self->_register_mapping( $glob, \$new_glob );
387  
388 3 ▶ if ( blessed(my $new_tied = $self->visit_tied($_[1], $_[2])) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed(my $new_tied = $self->visit_tied($_[1], $_[2]))) { } 3 0 3
389 3 $self->trace( data => tying => var => $new_glob, to => $new_tied ) if DEBUG;
390 3 tie *$new_glob, 'Tie::ToObject', $new_tied;
391 3 return $self->retain_magic($_[2], $new_glob);
392 } else {
393 0 return $self->visit_normal_glob($_[2]);
394 }
395 } else {
396 1 $self->_register_mapping( $glob, $glob );
397 1 $self->visit_tied($_[1], $_[2]);
398 1 return;
399 }
400 }
401  
402 88 ▶sub retain_magic {
Subroutine retain_magic: called CC=13 SCAR=43.8
Pod documented
403 88 my ( $self, $proto, $new ) = @_;
404  
405 88 ▶ if ( blessed($proto) and !blessed($new) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($proto) and not builtin::blessed($new)) 9 79 88
Condition66%2/3
builtin::blessed($proto) and not builtin::blessed($new)
!ll&&!rl&&r
7909
MC/DC50%1/2
builtin::blessed($proto) and not builtin::blessed($new)
builtin::blessed($proto) not builtin::blessed($new)
Truth table66%2/3
A and not(B)
A= builtin::blessed($proto)B= not builtin::blessed($new)
ABresult
0X0
100
111
406 9 $self->trace( data => blessing => $new, ref $proto ) if DEBUG;
407 9 bless $new, ref $proto;
408 }
409  
410 88 my $seen_hash = $self->{_seen};
411 88 ▶ if ( $seen_hash->{weak} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($seen_hash->{"weak"}) 0 88 88
412 #if ("$]" >= '5.022') {
413 # TODO: Data::Alias does not work on recent perls, but there is built-in aliasing support now.
414 # e.g. see what Var::Pairs 0.003004 did.
415 #}
416 0 ▶ if (HAS_DATA_ALIAS) {
Branch: 1 branch
BranchTrueFalseTotal
if (&HAS_DATA_ALIAS()) { } 0 0 0
417 0 my @weak_refs;
418 0 foreach my $value ( Data::Alias::deref($proto) ) {
419 0 ▶ if ( ref $value and isweak($value) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value and builtin::is_weak($value)) 0 0 0
Condition0%0/3
ref $value and builtin::is_weak($value)
!ll&&!rl&&r
000
MC/DC0%0/2
ref $value and builtin::is_weak($value)
ref $value builtin::is_weak($value)
Truth table0%0/3
A and B
A= ref $valueB= builtin::is_weak($value)
ABresult
0X0
100
111
420 0 push @weak_refs, refaddr $value;
421 }
422 }
423  
424 0 ▶ if ( @weak_refs ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@weak_refs) 0 0 0
425 0 my %targets = map { refaddr($_) => 1 } @{ $self->{_seen} }{@weak_refs};
426 0 foreach my $value ( Data::Alias::deref($new) ) {
427 0 ▶ if ( ref $value and $targets{refaddr($value)}) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value and $targets{builtin::refaddr($value)}) 0 0 0
Condition0%0/3
ref $value and $targets{builtin::refaddr($value)}
!ll&&!rl&&r
000
MC/DC0%0/2
ref $value and $targets{builtin::refaddr($value)}
ref $value $targets{builtin::refaddr($value)}
Truth table0%0/3
A and B
A= ref $valueB= $targets{builtin::refaddr($value)}
ABresult
0X0
100
111
428 0 ▶ push @{ $seen_hash->{weakened} ||= [] }, $value; # keep a ref around
Condition0%0/2
$seen_hash->{"weakened"} ||= []
l!l
00
MC/DC0%0/1
$seen_hash->{"weakened"} ||= []
$seen_hash->{"weakened"}
Truth table0%0/2
A
A= $seen_hash->{"weakened"}
Aresult
11
00
429 0 weaken($value);
430 }
431 }
432 }
433 }
434 else {
435 0 die "Found a weak reference, but Data::Alias is not installed. You must install Data::Alias in order for this to work.";
436 }
437 }
438  
439 # FIXME real magic, too
440  
441 88 return $new;
442 }
443  
444 17 ▶sub visit_tied {
Subroutine visit_tied: called CC=1 SCAR=0.0
Pod documented
445 17 my ( $self, $tied, $var ) = @_;
446 17 $self->trace( flow => visit_tied => $tied ) if DEBUG;
447 17 $self->visit($_[1]); # as an object eventually
448 }
449  
450 9 ▶__PACKAGE__->meta->make_immutable if __PACKAGE__->meta->can("make_immutable");
Branch: 1 branch
BranchTrueFalseTotal
if __PACKAGE__->meta->can("make_immutable") 9 0 9
451  
452 9 __PACKAGE__;
453  
454 __END__
↑ Summary Visitor/Callback.pm »