in reply to Re: Reference Count
in thread Reference Count

The "weak" references from Scalar::Util are a great thing to know about. I was trying to create a reference that wasn't counted, and that's exactly what the weaken() function does.

The only problem is that during processing, variables may fall out of scope and get undefined before their time is up. That's the reason I was trying to construct a cache. The cost of constructing an object is expensive enough (RDBMS) that keeping it around for a while is a good idea, even if it has temporarily fallen out of use.

mojotoad does have a point. This is precisely what Perl does with its own garbage collection routines. I don't intend to replace this for everything, just delay it for those objects which need to be preserved.

It isn't always practical to pass around all the data you need from function to function, especially if they are unrelated. The cache will serve to link together records that are not explicitly linked, but implicitly, something that isn't always clear in advance.

What I'm really trying to do is load any given DB row once, and work with it until it "expires", which is usually at the end of the transaction, though for some pseudo-static data, this could be considerably longer. Perl treats all objects as being equal, and discards them when it gets around to it. I'm looking for some degree of control over the process.

Having a refrerence count isn't vital, but sure would come in handy when trying to prune the cache.

Replies are listed 'Best First'.
Re: Re^2: Reference Count
by Anonymous Monk on Nov 19, 2002 at 23:14 UTC
    Weak references are still what I would recommend.

    You want a double data structure. One cache with weak references which you use when accessing possibly cached objects. And one data structure to keep references to objects that you want to keep from expiring. Flush the second one as appropriate, access through the first. Perl's GC does everything else for you.