in reply to Q on $var = \$var;

You can use weak references and get around this problem:
use Scalar::Util 'weaken'; # ... $self->{next} = (\$self == \$next) ? weaken(\$self) : \$next;

Jeff japhy Pinyan, P.L., P.M., P.O.D, X.S.: Perl, regex, and perl hacker
How can we ever be the sold short or the cheated, we who for every service have long ago been overpaid? ~~ Meister Eckhart

Replies are listed 'Best First'.
Re^2: Q on $var = \$var;
by ysth (Canon) on Apr 29, 2005 at 04:07 UTC
    Assigning a weakref yields a regular ref, and I think your test isn't quite right. Try (assuming $self/$next are references):
    $self->{next} = $next; weaken($self->{next}) if $self == $next;
    But that doesn't help with a longer circular chain; you need to pick the one element of the chain that has an external reference and weaken the preceding element's pointer to it.