
WeakRef::Auto - Automatically makes references weaken

This document describes WeakRef::Auto version 0.02.

use WeakRef::Auto;
autoweaken my $ref; # $ref is always weaken
$ref = \$var; # $ref is weak
sub MyNode::new{
# ...
autoweaken $self->{parent}; # parent is always weaken
return $self;
}

This module provides autoweaken(), which keeps references weaken.

Turns $var into auto-weaken variables, and keeps the values weak references. If $var already has a reference, it is weaken on the spot.
$var can be an element of hashes or arrays.

autoweaken() is "\$" (i.e. autoweakn($var) actually means &autoweaken(\$var)), you'd better load this module in compile-time, using use WeakRef::Auto directive.
autoweaken() does not work with tied variables, because autoweaken-ness is attached to the variable, not to the value referred by the variable, and tied variables interact with their objects by values, not variables, as the following shows:
my $impl = tie my %hash, 'Tie::StdHash';
autoweaken $hash{foo};
# $hash{foo} seems autoweaken. Really?
# Actually, $hash{foo} is linked to $impl->{foo} through FETCH()/STORE() methods,
# but there is no way to detect the relationship.
Patches are welcome.

Perl 5.8.1 or later.

Scalar::Util for a description of weak references.

Goro Fuji <gfuji(at)cpan.org>.

Copyright (c) 2008, Goro Fuji <gfuji(at)cpan.org>. Some rights reserved.
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.