http://qs321.pair.com?node_id=375297


in reply to Why is a hash the default "object" in oo perl?

Looks like you've independently discovered Abigail's favorite type of Inside-out objects. It's the only way you can subclass without caring about the base class implementation. They are a pretty good idea.

I use inside-out objects for my database persistence module, because it also helps with synchronization. Each object corresponds to a row in some table. So instead of using the pointer address as the key to the instance data hash, I use the primary key from the database. This is a big win, because there may be many objects instantiated that correspond to the same row in the database. When one gets updated, it's updated in the central hash and any other objects corresponding to that row can see it. If I had used blessed hashrefs as objects, it would have been a big pain to push these updates out to all the necessary hashrefs.

What your implementation is missing, however, is a DESTROY method. When doing these inside-out objects, your %agedata hash keeps filling up. When an object is destroyed, you never clean up after yourself. For persistent environments like mod_perl, this can lead to quite a memory leak in the instance data hashes. So be sure to add:

sub DESTROY { delete $agedata{+shift}; }
.. and clear out all instance data hashes.

blokhead