in reply to Catching an object going out of scope

In the absence of weak refs, you can use the equivalent of soft references:

my $TEMPID; my %TEMPS; package My::Class::Temps; sub lookup ($) { $TEMPS{+shift} } sub new { my $self = {}; $TEMPS{ self->{ID} = ++$TEMPID } = $self; $self; }

Now you can store $self->{ID} and use lookup() to find the corresponding object. Such storage of a unique ID doesn't increase reference counts, so it's rather like a weak reference, albeit not nearly as convenient.

    -- Chip Salzenberg, Free-Floating Agent of Chaos

Replies are listed 'Best First'.
Re: Re: Catching an object going out of scope
by Masem (Monsignor) on Nov 13, 2001 at 23:53 UTC
    I like this idea nicely, as the weak ref solutions previousy suggested only work under 5.6 and I'd like to keep a 5.005 solution usable.

    Extending this, I would suggest that I can now put in my DESTROY function for the Temps, not only a delete $TEMPS{ $self->{ID} };, but also something that goes back to the factory class that that ID is gone, though this step isn't really necessary.

    I believe that the only limitation here is that I couldn't have more than (whatever the maximum possible positive value is for an integer for perl & OS) iterators at the same time, but I very much that would be a problem.

    -----------------------------------------------------
    Dr. Michael K. Neylon - mneylon-pm@masemware.com || "You've left the lens cap of your mind on again, Pinky" - The Brain
    "I can see my house from here!"
    It's not what you know, but knowing how to find it if you don't know that's important