On Dec 21, 2003, at 12:04 PM, christopher j bottaro wrote:

On Sunday 21 December 2003 11:07 am, R. Joseph Newton wrote:
Nope.  Try
$root = undef;

interesting.  so perl is smart enough to know that once root is undef, the
user has no way of reaching any of the nodes in the tree, even if those nodes
still have references to them (i.e. the parent nodes left and right hash
entries).  if thats the case...wow, i'm impressed...=)

Perl uses a reference counting system. That usually does the right thing, like most things Perl. It's not magic, but it sure is handy. Let's look at an example.


If I have an object A, which contains a reference to object B, which contains a reference to C, etc:

A has a B has a C ...

Now if A is the only top level reference in my currently running code (and no other code has references), what happens when we do:

A = undef

Object A's ref count just hit zero, so it's gone. Now when that garbage collection happens, B's count is reduced, since A's reference to it is reclaimed. If it hits zero too, B will be collected. That would reduce C's count... You get the idea.

Basically, you generally only need to worry about Perl's references when you create circular refs. When faced with that, break the chain yourself manually or use weak references.

Does that clear things up?

James

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>




Reply via email to