I'd recommend using a separate class for tie from the one used for object methods. I wrote Win32::TieRegistry using one package for both tieing and blessing, and it mostly worked nice with a bunch of cases of:
my $self= shift(@_);
$self= tied(%$self) if tied(%$self);
But I'll probably be converting to using Win32::TieRegistry for the object methods and Win32::TieRegistry::Key for the tied hashes. Although this allows some efficiency improvements, the main reason for my desire to change is "bug"s in Perl's destructor semantics.
Prior to Perl5.6, the main bug was that any object stored in a global variable won't be destroyed until the "global destruction" phase of the Perl interpreter shutdown procedure. Unfortunately, during global destruction, all objects are destroyed in an arbitrary order.
This means that the real object might be destroyed before the "pretend" object that is tied to the real object. This means that tied(%$pretend) properly detects that the pretend object is tied and returns the object that it is tied to. But that (real) object has been destroyed so it is returned as undef, which you cannot distinguish from tied returning undef because %$pretend isn't tied.
So your DESTROY will think that $pretend isn't tied and try to treat it like the real object. Doing, for example, $pretend->{handle} will trigger a tied fetch which will call FETCH on the underlying object which is now undef so we get the equivalent of
undef->FETCH("handle"), which generates a nasty error.
It appears that Perl5.6 made this situation even worse so that it happens in more cases. I'm in the process of debugging that so I can "fix" Win32::TieRegistry so that it doesn't complain in spite of this "bug" in Perl.
So having a Win32::TieRegistry::DESTROY separate from Win32::TieRegistry::Key::DESTROY can be very handy.
I think it was a poor design that tie and bless use different constructors (TIEHASH vs. new) but the same destructor. The tie destructors should be DESTROYHASH or UNTIEHASH, etc.
Now, I think the way you are doing this is beyond what I did with Win32::TieRegistry, that is, a "self-tie" where you don't have two objects, one tied to the other, but instead have a single object that is tied to itself. This is supposed to work but has always made me nervous. Well, it was recently badly broken and I don't think it has been fixed yet. It will probably eventually be fixed, but I still suggest you just avoid doing that.
-
tye
(but my friends call me "Tye") |