1- package Tie::RefHash ; # git description: Tie-RefHash-1.39-10-g2cfa4bd
1+ package Tie::RefHash ; # git description: v1.40-9-g23812d9
22# ABSTRACT: Use references as hash keys
33
4- our $VERSION = ' 1.40 ' ;
4+ our $VERSION = ' 1.41 ' ;
55
66# pod =head1 SYNOPSIS
77# pod
@@ -76,36 +76,18 @@ our @ISA = qw(Tie::Hash);
7676use strict;
7777use Carp ();
7878
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+ use Scalar::Util qw( refaddr) ;
82+
7983BEGIN {
80- local $@ ;
8184 # determine whether we need to take care of threads
8285 use Config ();
8386 my $usethreads = $Config::Config {usethreads }; # && exists $INC{"threads.pm"}
8487 *_HAS_THREADS = $usethreads ? sub () { 1 } : sub () { 0 };
85- *_HAS_SCALAR_UTIL = eval { require Scalar::Util; 1 } ? sub () { 1 } : sub () { 0 };
8688 *_HAS_WEAKEN = defined (&Scalar::Util::weaken) ? sub () { 1 } : sub () { 0 };
8789}
8890
89- BEGIN {
90- # create a refaddr function
91-
92- local $@ ;
93-
94- if ( _HAS_SCALAR_UTIL ) {
95- *refaddr = sub { goto \&Scalar::Util::refaddr }
96- } else {
97- require overload;
98-
99- *refaddr = sub {
100- if ( overload::StrVal($_ [0]) =~ / \( 0x ([a-zA-Z0-9]+) \) $ /x ) {
101- return $1 ;
102- } else {
103- die " couldn't parse StrVal: " . overload::StrVal($_ [0]);
104- }
105- };
106- }
107- }
108-
10991my (@thread_object_registry , $count ); # used by the CLONE method to rehash the keys after their refaddr changed
11092
11193sub TIEHASH {
@@ -127,6 +109,7 @@ sub TIEHASH {
127109 if ( ++$count > 1000 ) {
128110 # this ensures we don't fill up with a huge array dead weakrefs
129111 @thread_object_registry = grep defined , @thread_object_registry ;
112+ Scalar::Util::weaken( $_ ) for @thread_object_registry ;
130113 $count = 0;
131114 }
132115 } else {
@@ -164,19 +147,20 @@ sub CLONE {
164147 # when the thread has been cloned all the objects need to be updated.
165148 # dead weakrefs are undefined, so we filter them out
166149 @thread_object_registry = grep defined && do { $_ -> _reindex_keys; 1 }, @thread_object_registry ;
150+ Scalar::Util::weaken( $_ ) for @thread_object_registry ;
167151 $count = 0; # we just cleaned up
168152}
169153
170154sub _reindex_keys {
171155 my ( $self , $extra_keys ) = @_ ;
172156 # rehash all the ref keys based on their new StrVal
173- %{ $self -> [0] } = map +(Scalar::Util:: refaddr($_ -> [0]) => $_ ), (values (%{ $self -> [0] }), @{ $extra_keys || [] });
157+ %{ $self -> [0] } = map +(refaddr($_ -> [0]) => $_ ), (values (%{ $self -> [0] }), @{ $extra_keys || [] });
174158}
175159
176160sub FETCH {
177161 my ($s , $k ) = @_ ;
178162 if (ref $k ) {
179- my $kstr = Scalar::Util:: refaddr($k );
163+ my $kstr = refaddr($k );
180164 if (defined $s -> [0]{$kstr }) {
181165 $s -> [0]{$kstr }[1];
182166 }
@@ -192,7 +176,7 @@ sub FETCH {
192176sub STORE {
193177 my ($s , $k , $v ) = @_ ;
194178 if (ref $k ) {
195- $s -> [0]{Scalar::Util:: refaddr($k )} = [$k , $v ];
179+ $s -> [0]{refaddr($k )} = [$k , $v ];
196180 }
197181 else {
198182 $s -> [1]{$k } = $v ;
@@ -203,13 +187,13 @@ sub STORE {
203187sub DELETE {
204188 my ($s , $k ) = @_ ;
205189 (ref $k )
206- ? (delete ($s -> [0]{Scalar::Util:: refaddr($k )}) || [])-> [1]
190+ ? (delete ($s -> [0]{refaddr($k )}) || [])-> [1]
207191 : delete ($s -> [1]{$k });
208192}
209193
210194sub EXISTS {
211195 my ($s , $k ) = @_ ;
212- (ref $k ) ? exists ($s -> [0]{Scalar::Util:: refaddr($k )}) : exists ($s -> [1]{$k });
196+ (ref $k ) ? exists ($s -> [0]{refaddr($k )}) : exists ($s -> [1]{$k });
213197}
214198
215199sub FIRSTKEY {
@@ -268,7 +252,7 @@ Tie::RefHash - Use references as hash keys
268252
269253=head1 VERSION
270254
271- version 1.40
255+ version 1.41
272256
273257=head1 SYNOPSIS
274258
@@ -343,7 +327,7 @@ Tie::RefHash::Nestable by Ed Avis <ed@membled.com>
343327
344328=head1 CONTRIBUTORS
345329
346- =for stopwords Yuval Kogman Karen Etheridge Florian Ragwitz Jerry D. Hedden
330+ =for stopwords Yuval Kogman Karen Etheridge Florian Ragwitz Lukas Mai Jerry D. Hedden tusooa
347331
348332=over 4
349333
@@ -361,8 +345,16 @@ Florian Ragwitz <rafl@debian.org>
361345
362346=item *
363347
348+ Lukas Mai <lukasmai.403@gmail.com>
349+
350+ =item *
351+
364352Jerry D. Hedden <jdhedden@cpan.org>
365353
354+ =item *
355+
356+ tusooa <tusooa@kazv.moe>
357+
366358=back
367359
368360=head1 COPYRIGHT AND LICENCE
0 commit comments