Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

weak references and dclone (via WeakRef and Storable)

1 view
Skip to first unread message

Paul GABORIT

unread,
Feb 14, 2003, 7:50:29 AM2/14/03
to

I use weak references in a complex object. I want to clone my object by
preserving its weak references.

Simple example :
----------------------------------------
use WeakRef;
use Storable qw{dclone};

my $v1 = {};
$v1->{a} = $v1;
weaken($v1->{a});

my $v2 = dclone($v1);

print "v1: $v1\n";
print "v1->{a}: $v1->{a}\n";
print "v2: $v2\n";
print "v2->{a}: $v2->{a}\n";
print "Ok1!\n" if isweak($v1->{a});
print "Ok2!\n" if isweak($v2->{a});
----------------------------------------

This script does not print "Ok2!". The clone thus forgot the weakness of its
reference.

How can I make a correct clone ?

Thank you.

--
Paul Gaborit - <http://www.enstimac.fr/~gaborit/>
Perl en français - <http://www.enstimac.fr/Perl/>
Remove '.OOO' from e-mail address - Supprimez '.OOO' de l'adresse e-mail

Rafael Garcia-Suarez

unread,
Feb 14, 2003, 9:56:33 AM2/14/03
to
Paul GABORIT wrote in comp.lang.perl.misc :

>
> I use weak references in a complex object. I want to clone my object by
> preserving its weak references.
>
> Simple example :
> ----------------------------------------
> use WeakRef;
> use Storable qw{dclone};

Storable apparently doesn't know about weak refs.
I'm not sure whether this qualifies as a bug, because persistent data
containing weak references makes my brain hurt.

Anyway, the solutions are :
* traverse the cloned structure and mark the relevant refs as weak
* patch Storable to handle weak refs
* write another generic cloning method

HTH

--
For some reason most people seem to be born without the part of the brain
that understands pointers. -- Joel Spolsky

0 new messages