Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re^2: Garbage collected class attributes with inside out classes.

by kyle (Abbot)
on Mar 01, 2007 at 18:05 UTC ( [id://602733]=note: print w/replies, xml ) Need Help??


in reply to Re: Garbage collected class attributes with inside out classes.
in thread Garbage collected class attributes with inside out classes.

Well, you just might get rid of the extra variable.

Yes. I actually mentioned this in an earlier draft, but I took it out. If you have some regular attribute stored in a hash, and you're guaranteed that it always has a value (and typically that's true), you can use that to tell when you're the last object to be destroyed.

What I don't like about that method is that it ties proper destruction to something that's not otherwise related to it. Say there are many attributes, and you pick one to be your surrogate counter. A programmer who comes to look later might wonder why that one? If the attribute you choose changes (its name, or the fact that it always gets a value), the destructor has to change too. You can (and should) make all that explicit in comments to avoid confusion, but having a separate counter is self documenting.

  • Comment on Re^2: Garbage collected class attributes with inside out classes.

Replies are listed 'Best First'.
Re^3: Garbage collected class attributes with inside out classes.
by Anno (Deacon) on Mar 01, 2007 at 18:46 UTC
    Those are valid concerns, at least in the context of a one-off implementation. If you think of it as (say) an additional feature of Class::Std, the choice of the "representative attribute" would be abstract (probably the first, or last, attribute declared in the class) and rather obvious. The ugliness would be hidden in the general destructor.

    Come to think of it, there's a problem with classes that don't have (their own) object attributes, but still want destructible class data. Normal garbage collection would likely ignore such a class, so it can't be used as a trigger. You'd need a dummy attribute or some such.

    Your more intricate first implementation would still work, if only because the class attributes are implemented as object attributes, so the case doesn't arise.

    Unrelatedly, I think your writeup would be easier to read if you swapped the two implementations, so the simple one comes first and can serve as an introduction.

    Anno

Log In?
Username:
Password:

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

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

    No recent polls found