blib/lib/Tie/RefHash.pm

filter: Statementstmt 90.4% 85 / 94 Branchbran 76.9% 20 / 26 Conditioncond 42.8% 6 / 14 MC/DCmcdc 25.0% 2 / 8 Subroutinesub 94.7% 18 / 19 Podpod 0.0% 0 / 2 totaltotal 80.3% 131 / 163 CC 21 SCAR 17.6
CC
21
Cov
83%
CLONE
30.0
BEGIN
3.1
DELETE
3.0
CRAP 5.8
↑ Summary
1 package Tie::RefHash; # git description: v1.40-9-g23812d9
2 # ABSTRACT: Use references as hash keys
3  
4 3 our $VERSION = '1.41';
5  
6 #pod =head1 SYNOPSIS
7 #pod
8 #pod require 5.004;
9 #pod use Tie::RefHash;
10 #pod tie HASHVARIABLE, 'Tie::RefHash', LIST;
11 #pod tie HASHVARIABLE, 'Tie::RefHash::Nestable', LIST;
12 #pod
13 #pod untie HASHVARIABLE;
14 #pod
15 #pod =head1 DESCRIPTION
16 #pod
17 #pod This module provides the ability to use references as hash keys if you
18 #pod first C<tie> the hash variable to this module. Normally, only the
19 #pod keys of the tied hash itself are preserved as references; to use
20 #pod references as keys in hashes-of-hashes, use Tie::RefHash::Nestable,
21 #pod included as part of Tie::RefHash.
22 #pod
23 #pod It is implemented using the standard perl TIEHASH interface. Please
24 #pod see the C<tie> entry in perlfunc(1) and perltie(1) for more information.
25 #pod
26 #pod The Nestable version works by looking for hash references being stored
27 #pod and converting them to tied hashes so that they too can have
28 #pod references as keys. This will happen without warning whenever you
29 #pod store a reference to one of your own hashes in the tied hash.
30 #pod
31 #pod =head1 EXAMPLE
32 #pod
33 #pod use Tie::RefHash;
34 #pod tie %h, 'Tie::RefHash';
35 #pod $a = [];
36 #pod $b = {};
37 #pod $c = \*main;
38 #pod $d = \"gunk";
39 #pod $e = sub { 'foo' };
40 #pod %h = ($a => 1, $b => 2, $c => 3, $d => 4, $e => 5);
41 #pod $a->[0] = 'foo';
42 #pod $b->{foo} = 'bar';
43 #pod for (keys %h) {
44 #pod print ref($_), "\n";
45 #pod }
46 #pod
47 #pod tie %h, 'Tie::RefHash::Nestable';
48 #pod $h{$a}->{$b} = 1;
49 #pod for (keys %h, keys %{$h{$a}}) {
50 #pod print ref($_), "\n";
51 #pod }
52 #pod
53 #pod =head1 THREAD SUPPORT
54 #pod
55 #pod L<Tie::RefHash> fully supports threading using the C<CLONE> method.
56 #pod
57 #pod =head1 STORABLE SUPPORT
58 #pod
59 #pod L<Storable> hooks are provided for semantically correct serialization and
60 #pod cloning of tied refhashes.
61 #pod
62 #pod =head1 AUTHORS
63 #pod
64 #pod Gurusamy Sarathy <gsar@activestate.com>
65 #pod
66 #pod Tie::RefHash::Nestable by Ed Avis <ed@membled.com>
67 #pod
68 #pod =head1 SEE ALSO
69 #pod
70 #pod perl(1), perlfunc(1), perltie(1)
71 #pod
72 #pod =cut
73  
74 3 ▶use Tie::Hash;
Subroutine BEGIN: called CC=1 SCAR=0.0
75 3 our @ISA = qw(Tie::Hash);
76 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
77 3 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
78  
79 # Tie::RefHash::Weak (until at least 0.09) assumes we define a refaddr()
80 # function, so just import the one from Scalar::Util
81 3 ▶use Scalar::Util qw(refaddr);
Subroutine BEGIN: called CC=1 SCAR=0.0
82  
83 3 ▶BEGIN {
Subroutine BEGIN: called CC=3 SCAR=11.2
84 # determine whether we need to take care of threads
85 3 ▶ use Config ();
Subroutine BEGIN: called CC=1 SCAR=0.0
86 3 my $usethreads = $Config::Config{usethreads}; # && exists $INC{"threads.pm"}
87 3 ▶ *_HAS_THREADS = $usethreads ? sub () { 1 } : sub () { 0 };
Branch: 1 branch
BranchTrueFalseTotal
$usethreads ? : 0 3 3
88 3 ▶ *_HAS_WEAKEN = defined(&Scalar::Util::weaken) ? sub () { 1 } : sub () { 0 };
Branch: 1 branch
BranchTrueFalseTotal
defined &Scalar::Util::weaken ? : 3 0 3
89 }
90  
91 3 my (@thread_object_registry, $count); # used by the CLONE method to rehash the keys after their refaddr changed
92  
93 9 ▶sub TIEHASH {
Subroutine TIEHASH: called CC=2 SCAR=7.1
94 9 my $c = shift;
95 9 my $s = [];
96 9 bless $s, $c;
97 9 ▶ while (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 9 9
98 0 $s->STORE(shift, shift);
99 }
100  
101 9 if (_HAS_THREADS ) {
102  
103 if ( _HAS_WEAKEN ) {
104 # remember the object so that we can rekey it on CLONE
105 push @thread_object_registry, $s;
106 # but make this a weak reference, so that there are no leaks
107 Scalar::Util::weaken( $thread_object_registry[-1] );
108  
109 if ( ++$count > 1000 ) {
110 # this ensures we don't fill up with a huge array dead weakrefs
111 @thread_object_registry = grep defined, @thread_object_registry;
112 Scalar::Util::weaken( $_ ) for @thread_object_registry;
113 $count = 0;
114 }
115 } else {
116 $count++; # used in the warning
117 }
118 }
119  
120 9 return $s;
121 }
122  
123 3 my $storable_format_version = join("/", __PACKAGE__, "0.01");
124  
125 4 ▶sub STORABLE_freeze {
Subroutine STORABLE_freeze: called CC=2 SCAR=6.9
Pod undocumented
126 4 my ( $self, $is_cloning ) = @_;
127 4 my ( $refs, $reg ) = @$self;
128 4 ▶ return ( $storable_format_version, [ values %$refs ], $reg || {} );
Condition100%2/2
$reg || {}
l!l
22
MC/DC100%1/1
$reg || {}
$reg
Truth table100%2/2
A
A= $reg
Aresult
11
00
129 }
130  
131 4 ▶sub STORABLE_thaw {
Subroutine STORABLE_thaw: called CC=2 SCAR=7.0
Pod undocumented
132 4 my ( $self, $is_cloning, $version, $refs, $reg ) = @_;
133 4 ▶ Carp::croak "incompatible versions of Tie::RefHash between freeze and thaw"
Branch: 1 branch
BranchTrueFalseTotal
unless $version eq $storable_format_version 0 4 4
134 unless $version eq $storable_format_version;
135  
136 4 @$self = ( {}, $reg );
137 4 $self->_reindex_keys( $refs );
138 }
139  
140 0 ▶sub CLONE {
Subroutine CLONE: not called CC=5 SCAR=34.0
141 0 my $pkg = shift;
142  
143 0 ▶ if ( $count and not _HAS_WEAKEN ) {
Branch: 1 branch
BranchTrueFalseTotal
if () 0 0 0
Condition0%0/2
$count and
!ll
00
MC/DC0%0/1
$count and
$count
Truth table0%0/2
A
A= $count
Aresult
00
11
144 0 warn "Tie::RefHash is not threadsafe without Scalar::Util::weaken";
145 }
146  
147 # when the thread has been cloned all the objects need to be updated.
148 # dead weakrefs are undefined, so we filter them out
149 0 ▶ @thread_object_registry = grep defined && do { $_->_reindex_keys; 1 }, @thread_object_registry;
Condition0%0/3
defined $_ && do { $_->_reindex_keys; 1 }
!ll&&!rl&&r
000
MC/DC0%0/2
defined $_ && do { $_->_reindex_keys; 1 }
defined $_ do { $_->_reindex_keys; 1 }
Truth table0%0/3
A && B
A= defined $_B= do { $_->_reindex_keys; 1 }
ABresult
0X0
100
111
150 0 Scalar::Util::weaken( $_ ) for @thread_object_registry;
151 0 $count = 0; # we just cleaned up
152 }
153  
154 4 ▶sub _reindex_keys {
Subroutine _reindex_keys: called CC=2 SCAR=7.0
155 4 my ( $self, $extra_keys ) = @_;
156 # rehash all the ref keys based on their new StrVal
157 4 ▶ %{ $self->[0] } = map +(refaddr($_->[0]) => $_), (values(%{ $self->[0] }), @{ $extra_keys || [] });
Condition50%1/2
$extra_keys || []
l!l
40
MC/DC0%0/1
$extra_keys || []
$extra_keys
Truth table50%1/2
A
A= $extra_keys
Aresult
11
00
158 }
159  
160 625 ▶sub FETCH {
Subroutine FETCH: called CC=3 SCAR=11.0
161 625 my($s, $k) = @_;
162 625 ▶ if (ref $k) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $k) { } 22 603 625
163 22 my $kstr = refaddr($k);
164 22 ▶ if (defined $s->[0]{$kstr}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $s->[0]{$kstr}) { } 20 2 22
165 20 $s->[0]{$kstr}[1];
166 }
167 else {
168 2 undef;
169 }
170 }
171 else {
172 603 $s->[1]{$k};
173 }
174 }
175  
176 95 ▶sub STORE {
Subroutine STORE: called CC=2 SCAR=6.9
177 95 my($s, $k, $v) = @_;
178 95 ▶ if (ref $k) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $k) { } 8 87 95
179 8 $s->[0]{refaddr($k)} = [$k, $v];
180 }
181 else {
182 87 $s->[1]{$k} = $v;
183 }
184 95 $v;
185 }
186  
187 64 ▶sub DELETE {
Subroutine DELETE: called CC=3 SCAR=11.1
188 64 my($s, $k) = @_;
189 (ref $k)
190 ? (delete($s->[0]{refaddr($k)}) || [])->[1]
191 64 ▶ : delete($s->[1]{$k});
Branch: 1 branch
BranchTrueFalseTotal
ref $k ? : 2 62 64
Condition50%1/2
delete $s->[0]{builtin::refaddr($k)} || []
l!l
20
MC/DC0%0/1
delete $s->[0]{builtin::refaddr($k)} || []
delete $s->[0]{builtin::refaddr($k)}
Truth table50%1/2
A
A= delete $s->[0]{builtin::refaddr($k)}
Aresult
11
00
192 }
193  
194 132 ▶sub EXISTS {
Subroutine EXISTS: called CC=2 SCAR=6.9
195 132 my($s, $k) = @_;
196 132 ▶ (ref $k) ? exists($s->[0]{refaddr($k)}) : exists($s->[1]{$k});
Branch: 1 branch
BranchTrueFalseTotal
ref $k ? : 3 129 132
197 }
198  
199 594 ▶sub FIRSTKEY {
Subroutine FIRSTKEY: called CC=1 SCAR=0.0
200 594 my $s = shift;
201 594 keys %{$s->[0]}; # reset iterator
202 594 keys %{$s->[1]}; # reset iterator
203 594 $s->[2] = 0; # flag for iteration, see NEXTKEY
204 594 $s->NEXTKEY;
205 }
206  
207 1173 ▶sub NEXTKEY {
Subroutine NEXTKEY: called CC=3 SCAR=11.0
208 1173 my $s = shift;
209 1173 my ($k, $v);
210 1173 ▶ if (!$s->[2]) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($s->[2]) 610 563 1173
211 610 ▶ if (($k, $v) = each %{$s->[0]}) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$s[0];}) { } 16 594 610
212 16 return $v->[0];
213 }
214 else {
215 594 $s->[2] = 1;
216 }
217 }
218 1157 return each %{$s->[1]};
219 }
220  
221 2 ▶sub CLEAR {
Subroutine CLEAR: called CC=1 SCAR=0.0
222 2 my $s = shift;
223 2 $s->[2] = 0;
224 2 %{$s->[0]} = ();
225 2 %{$s->[1]} = ();
226 }
227  
228 package # hide from PAUSE
229 Tie::RefHash::Nestable;
230 3 our @ISA = 'Tie::RefHash';
231  
232 45 ▶sub STORE {
Subroutine STORE: called CC=3 SCAR=11.0
233 45 my($s, $k, $v) = @_;
234 45 ▶ if (ref($v) eq 'HASH' and not tied %$v) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $v eq "HASH" and not tied %$v) 1 44 45
Condition66%2/3
ref $v eq "HASH" and not tied %$v
!ll&&!rl&&r
4401
MC/DC50%1/2
ref $v eq "HASH" and not tied %$v
ref $v eq "HASH" not tied %$v
Truth table66%2/3
A and not(B)
A= ref $v eq "HASH"B= not tied %$v
ABresult
0X0
100
111
235 1 my @elems = %$v;
236 1 tie %$v, ref($s), @elems;
237 }
238 45 $s->SUPER::STORE($k, $v);
239 }
240  
241 3 1;
242  
243 __END__
↑ Summary