Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Allowing object destruction even with an

by Fastolfe (Vicar)
on Sep 08, 2000 at 01:36 UTC ( [id://31490]=perlquestion: print w/replies, xml ) Need Help??

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

Frequently when I'm creating an object, I will store a reference to this object in a list, so that my package can keep track of what objects are out there for the purposes of iterating through them later. Unfortunately, this prevents my objects from being implicitely destroyed if they are deleted or go out of scope, since that 1 reference in my list will always be around.

Is there any way I can "ignore" this 2nd reference to my objects so that I can use an implicit DESTROY function, which presumably would automatically drop the reference from my internal list?

If that's not possible, is there a better way of keeping track of instances of objects out there?

  • Comment on Allowing object destruction even with an

Replies are listed 'Best First'.
Re (tilly) 1: Allowing object destruction even with an
by tilly (Archbishop) on Sep 08, 2000 at 01:45 UTC
    If you are on 5.6 you can try the WeakRef module.

    The second thing that you can do is have your constructor return a wrapper that is a reference to the real object. This wrapper will then go away properly and you can have it properly destroy the underlying argument in its DESTROY method. This does force an extra layer of indirection though.

      Yes! WeakRef is exactly what I needed. I can essentially weaken the reference in the @MASTER_LIST and maybe even forego the DESTROY function entirely, making sure to occasionally clean out the undef's in the list. Thanks.
RE: Allowing object destruction even with an
by BlaisePascal (Monk) on Sep 08, 2000 at 05:50 UTC
    Check out the question I wrote about Flyweight Objects and garbage collection to see an implementation of a "flyweight class". I got the idea from Damian Conway, who suggested it as a method of data hiding.

    It does what you are doing -- stores the object in a list -- but instead of returning a blessed reference to the object, it returns a blessed index into the list. Add a DESTROY that undefs the entry in the list, and you get the behavior you want.

Re: Allowing object destruction even with an
by Fastolfe (Vicar) on Sep 08, 2000 at 01:42 UTC
    For clarification, this "internal" list is within the package itself. Thus, I'd have something like this:
    package MyObject; my @MASTER_LIST; sub new { my $pkg = shift; my $self = {}; bless $self, $pkg; push(@MASTER_LIST, $self); return $self; }
(crazyinsomniac) Re: Allowing object destruction even with an
by crazyinsomniac (Prior) on Sep 08, 2000 at 01:46 UTC
    oops, thought i only hit preview

    Hey,

    If I were you I'd make your external reference a reference to a temporary var, and then destroy your internal reference, and then the temporary one.

    I really don't have a clue but hey, I gave it a shot.

    Check out the following, might help:

    perlref - Perl references
    perldsc - Perl data structures intro
    perllol - Perl data structures: lists of lists
    perltoot - Perl OO tutorial
    perlobj - Perl objects
    perltie - Perl objects hidden behind simple variables
    perlbot - Perl OO tricks and examples

    "cRaZy is co01, but sometimes cRaZy is cRaZy".
                                                          - crazyinsomniac

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://31490]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (3)
As of 2024-04-16 20:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found