Elizabeth Mattijsen wrote:Alternately, again _if_ you have blessed objects and you _do_ need to fix objects up after they have been cloned (hence the name "fixup" for this functionality), you can have a method of your own devise called on each instantiated object automagically after Perl has cloned it.That's incomplete, because you won't call DESTROY on those fixedup/possessed objects. I suppose that you should check whether the user of your module provides the fixup callback and if it does DESTROY all objects (or make it configurable).
Actually, these are both configurable seperately. You can specify whether all objects have DESTROY called on them (including the cloned ones), or only the original ones. Default is only original ones.
And you can specify a fixup routine for cloned objects. Of which the default is none.
However, this only works on blessed objects, not on unblessed ones. On the other hand, you could decide to bless your SV to a special class just to get this functionality.You mean double bless? My objects are already "blessed", but they don't use bless:
sv_setref_pv(ST(0), "Example::CLONE", (void*)RETVAL);
Hmmm... didn't realize you can create blessed objects from XS. But yes, the actual bookkeeping routine could be made available externally, something like:
Thread::Bless->register( $object );
would that help?
How about making your module more flexible and in addition to the overriden bless function provide a new_fixup methods, so one can choose to run new_fixup on already "blessed"/sv_setref_pv'ed object to store a weak copy of it? this can be called from the normal new().
Whether or not fixup routines are called, is determined by package, not by a single object. You _do_ need to register each single object though (which is done automatically if you use "bless").
So the only remaining nice-to-have issue is to do COW for a better performance.
You could do that by having the fixup routine set a flag in/for the object that it has been COWed. Then, whenever you need to make changes, you check that flag and when it's set, _then_ you can do the real fixup.
Liz
