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

I'm working on a module that creates a HASH that won't increment the reference count of references that are stored inside it. This can be used to store objects, but without interfere in the DESTROY mechanism, since the references in this HASH won't count.

But now I have a problem, how to know if a reference was cleaned/destroied? I need to know that to automatically remove references/objects from the HASH and free 100% of the memory that should be free after clean that variable.

Note that the behavior of a SCALAR that holds a reference after have it cleaned is strange:

$hash_nor_ref->{ref} = {} ; #will have REFCNT = 0 on clean $hash_nor_ref->{ref} = [] ; #will have REFCNT = 0 on clean $hash_nor_ref->{ref} = \'' ; #will have REFCNT = 1 on clean $hash_nor_ref->{ref} = bless({}) ; #before destroy have a reference to + a blessed HASH, after destroy it will have a reference to a SCALAR, +with REFCNT = 1.
So, there is a better way to know if the reference is dead, some flag, internal command different than SvREFCNT()?

If is not possible to have a better way to know that I will need to map the behaviors of dead references and work over that, but I don't think that this will work very well.

By the way, you can take a look in what was already done: Hash-NoRef-0.01. (Use it as a beta version)

Graciliano M. P.
"Creativity is the expression of the liberty".

  • Comment on Playing with the Reference Count of variables (to create a POOL of objects wihtout mess with DESTROY).
  • Download Code

Replies are listed 'Best First'.
Re: Playing with the Reference Count of variables (to create a POOL of objects wihtout mess with DESTROY).
by perrin (Chancellor) on Apr 16, 2004 at 21:21 UTC
    I implemented one of these in the latest Class::DBI using Scalar::Utils::weaken(). When an object has been cleaned up, it just looks undefined. My purge code looks like this:

    sub purge_dead_from_object_index { delete @Live_Objects{ grep !defined $Live_Objects{$_}, keys %L +ive_Object s }; }
Re: Playing with the Reference Count of variables (to create a POOL of objects wihtout mess with DESTROY).
by Steve_p (Priest) on Apr 16, 2004 at 21:04 UTC

    Each SV* has a sv_refcnt field. So, to get the reference count, you can just check ref->sv_refcnt. My guess, however, is that SvREFCNT() is a safer wrapper to accessing the field directly.

    The module Internals has a way of getting and messing with reference counts as well, if that helps you out.

Re: Playing with the Reference Count of variables (to create a POOL of objects wihtout mess with DESTROY).
by BUU (Prior) on Apr 16, 2004 at 21:29 UTC
    Perhaps I'm exposing my ignorance, but I don't understand why you wish to implement such complicated VooDoo. You're going to be maintaining a hash containing references to all your objects anyways right? Therefor none of the objects can go out of scope till this hash does, as they're always going to have at least one ref count, right?

    Am I just completely missing what your trying to do?
      Perhaps I'm exposing my ignorance, but I don't understand why you wish to implement such complicated VooDoo.

      It can be useful in maintaining object caches. If you want to only ever have one object of a specific value extant at any one time then a hash is an obvious choice, but you don't want the values in the hash interfering with the object collected when it falls out of scope.