3 package Tie::RefHash::Weak;
4 use base qw/Tie::RefHash Exporter/;
9 use warnings::register;
13 use B qw/svref_2object CVf_CLONED/;
16 our @EXPORT_OK = qw 'fieldhash fieldhashes';
17 our %EXPORT_TAGS = ( all => \@EXPORT_OK );
19 use Scalar::Util qw/weaken reftype/;
20 use Variable::Magic qw/wizard cast getdata/;
22 my $wiz = wizard free => \&_clear_weakened_sub, data => \&_add_magic_data;
24 sub _clear_weakened_sub {
25 my ( $key, $objs ) = @_;
27 foreach my $self ( grep { defined } @{ $objs || [] } ) {
28 eval { $self->_clear_weakened($key) }; # support subclassing
33 my ( $key, $objects ) = @_;
38 my ( $self, $key ) = @_;
40 $self->DELETE( $key );
47 # make sure we use the same function that RefHash is using for ref keys
48 my $kstr = Tie::RefHash::refaddr($k);
51 weaken( $entry->[0] );
55 if ( reftype $k eq 'CODE' ) {
56 unless ( svref_2object($k)->CvFLAGS & CVf_CLONED ) {
57 warnings::warnif("Non closure code references never get garbage collected: $k");
59 $objects = &getdata ( $k, $wiz )
60 or &cast( $k, $wiz, ( $objects = [] ) );
63 $objects = &getdata( $k, $wiz )
64 or &cast( $k, $wiz, ( $objects = [] ) );
67 @$objects = grep { defined } @$objects;
69 unless ( grep { $_ == $s } @$objects ) {
71 weaken($objects->[-1]);
74 $s->[0]{$kstr} = $entry;
84 tie %{$_[0]}, __PACKAGE__;
89 tie %{$_}, __PACKAGE__ for @_;
101 Tie::RefHash::Weak - A Tie::RefHash subclass with weakened references in the keys.
105 use Tie::RefHash::Weak;
106 tie my %h, 'Tie::RefHash::Weak';
110 use Tie::RefHash::Weak 'fieldhash';
116 $h{\$val} = "bar"; # key is weak ref
118 print join(", ", keys %h); # contains \$val, returns regular reference
120 # $val goes out of scope, refcount goes to zero
121 # weak references to \$val are now undefined
123 keys %h; # no longer contains \$val
125 # see also Tie::RefHash
129 The L<Tie::RefHash> module can be used to access hashes by reference. This is
130 useful when you index by object, for example.
132 The problem with L<Tie::RefHash>, and cross indexing, is that sometimes the
133 index should not contain strong references to the objecs. L<Tie::RefHash>'s
134 internal structures contain strong references to the key, and provide no
135 convenient means to make those references weak.
137 This subclass of L<Tie::RefHash> has weak keys, instead of strong ones. The
138 values are left unaltered, and you'll have to make sure there are no strong
139 references there yourself.
143 For compatibility with L<Hash::Util::FieldHash>, this module will, upon
144 request, export the following two functions. You may also write
145 C<use Tie::RefHash::Weak ':all'>.
149 =item fieldhash %hash
151 This ties the hash and returns a reference to it.
153 =item fieldhashes \%hash1, \%hash2 ...
155 This ties each hash that is passed to it as a reference. It returns the
156 list of references in list context, or the number of hashes in scalar
163 L<Tie::RefHash> version 1.32 and above have correct handling of threads (with
164 respect to changing reference addresses). If your module requires
165 Tie::RefHash::Weak to be thread aware you need to depend on both
166 L<Tie::RefHash::Weak> and L<Tie::RefHash> version 1.32 (or later).
168 Version 0.02 and later of Tie::RefHash::Weak depend on a thread-safe version of
169 Tie::RefHash anyway, so if you are using the latest version this should already
170 be taken care of for you.
172 =head1 5.10.0 COMPATIBILITY
174 Due to a minor change in Perl 5.10.0 a bug in the handling of magic freeing was
175 uncovered causing segmentation faults.
177 This has been patched but not released yet, as of 0.08.
181 You can use an LVALUE reference (such as C<\substr ...>) as a hash key, but
182 due to a bug in perl (see
183 L<http://rt.perl.org/rt3/Public/Bug/Display.html?id=46943>) it might not be
184 possible to weaken a reference to it, in which case the hash element will
185 never be deleted automatically.
189 Yuval Kogman <nothingmuch@woobling.org>
191 some maintenance by Hans Dieter Pearcey <hdp@pobox.com>
193 =head1 COPYRIGHT & LICENSE
195 Copyright (c) 2004 Yuval Kogman. All rights reserved
196 This program is free software; you can redistribute
197 it and/or modify it under the same terms as Perl itself.
201 L<Tie::RefHash>, L<Class::DBI> (the live object cache),
202 L<mg.c/Perl_magic_killbackrefs>