Er, i hate to say this but i dont think thats a great idea unless you have seriously good reasons.

The way to do this is to make sure the reference that is blessed is not part of the cyclic structure. Consider the following code:

use strict; use warnings; sub MyBinTree::insert { my ($self,$string,$node)=@_; my $ins_ref; unless ($node) { unless ($self->{root}) { $ins_ref=\$self->{root} } else { $node=$self->{root}; } } unless ($ins_ref) { if ($string lt $node->{val}) { if ($node->{left}) { return $self->insert($string,$node->{left}) } else { $ins_ref=\$node->{left}; } } elsif ($string gt $node->{val}) { if ($node->{right}) { return $self->insert($string,$node->{right}) } else { $ins_ref=\$node->{right}; } } else { return $node; } } $$ins_ref={val=>$string,parent=>$node}; return $$ins_ref } sub MyBinTree::new { my ($class)=@_; my $self= bless { },$class; print "Created new $self\n"; return $self; } sub MyBinTree::_traverse { my ($self,$node,$sub)=@_; if ($node->{left}) { $self->_traverse($node->{left},$sub); } { local $_=$node; $sub->(); } if ($node->{right}) { $self->_traverse($node->{right},$sub); } } sub MyBinTree::traverse { my ($self,$sub)=@_; $self->_traverse($self->{root},$sub) if $self->{root}; } sub MyBinTree::DESTROY { my $self=shift; print "Destroying new $self\n"; $self->traverse(sub { delete $_->{parent} }); print "Destroyed\n"; } { my $container=MyBinTree->new(); $container->insert($_) for (my @words=qw(foo bar baz bop fop cool)); $container->traverse(sub { print $_->{val},"\n" }); print "Exiting scope.\n"; }

Which outputs:

Created new MyBinTree=HASH(0x1acef24) bar baz bop cool foo fop Exiting scope. Destroying new MyBinTree=HASH(0x1acef24) Destroyed

The internal data structure is entirely self referential and cyclic. Yet it destroys just fine, and with no weakref.

---
demerphq


In reply to Re^2: Is there a penalty for namespace use? by demerphq
in thread Is there a penalty for namespace use? by zentara

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.