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")

In reply to (tye)Re: DESTROY for tied classes by tye
in thread DESTROY for tied classes by MeowChow

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.