Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Cloning (weak) references

by Tomte (Priest)
on Mar 03, 2005 at 15:19 UTC ( [id://436239]=perlquestion: print w/replies, xml ) Need Help??

Tomte has asked for the wisdom of the Perl Monks concerning the following question:

In comp.lang.perl.misc it was asked (message-ID: <1109848552.496818.242290@o13g2000cwo.googlegroups.com>)

I've tried various cloning modules to clone a structure containing weak references.
use Storable 'dclone'; use Scalar::Util qw(weaken isweak); my $x = 99; my $y = [ \$x ] ; weaken $y->[0]; $z = dclone $y; print "y: ", isweak $y->[0]; print "z: ", isweak $z->[0];
This prints: y: 1 z: I've tried it using the Clone module and Clone::PP. Clone::PP does the same as Storable. Clone gives a segmentation fault. I'm using Perl 5.8.0 on Linux 2.4.20-8
my reply is
After a little bit of thought I'd say that's normal, to be expected behaviour: You're not cloning the weakend reference, but the the data it references, the reference in z[0], poiting to the clone of $x, is a completly new one, the data is cloned, not the reference. I may misunderstand the behaviour though...hmmm kind regards, Tom

Now I'm curious: Am I right? And if I'm right: Is it possible to clone references (Update preserving their "weakend-state")?

regards,
tomte


An intellectual is someone whose mind watches itself.
-- Albert Camus

Replies are listed 'Best First'.
Re: Cloning (weak) references
by hardburn (Abbot) on Mar 03, 2005 at 15:27 UTC

    I think you're correct. To fix, the clone operation would have to ensure the ref count stays the same, but I don't think that is the right thing to do in the general case. Cloning means you have an extra something that is making a reference to your data, and that extra something means an additional ref count.

    "There is no shame in being self-taught, only in not trying to learn in the first place." -- Atrus, Myst: The Book of D'ni.

Re: Cloning (weak) references
by simonm (Vicar) on Mar 03, 2005 at 22:15 UTC
    You could easily extend Clone::PP to do this, by adding a line to the elsif ($ref_type eq 'REF' ... section, something like weaken( $copy = $source ) if isweak( $source );.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://436239]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (4)
As of 2024-03-28 20:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found