in reply to DESTROY for tied classes

In your constructor, you bless a hash into the current class, storing it in $this. Then, you tie the dereferenced hash into the current class.

I think that's double-blessing. Good for the pious, but tricky for programs. Here's my suggestion:

sub new { my $proto = shift; my $class = ref ($proto) || $proto; my %this; return tie(%this, $class); }
Let's make things a little more explicit by tieing a hash blessed into a different class. Here's the new class and a slightly different constructor:
package FunClass; sub new { my $class = shift; my $self = {}; bless($self, $class); } sub DESTROY { my $self = shift; print "Destroying $self!\n"; } sub new { my $proto = shift; my $class = ref ($proto) || $proto; my $this = FunClass->new(); tie %{$this}, $class; return $this; }
And the results?
Destroying FunClass=HASH(0x80f173c)! Calling DESTROY by SillyClass=HASH(0x80f600c)

Replies are listed 'Best First'.
(tye)Re: DESTROY for tied classes
by tye (Sage) on Dec 05, 2000 at 06:45 UTC

    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")
Re: Re: DESTROY for tied classes
by MeowChow (Vicar) on Dec 05, 2000 at 06:24 UTC
    Thanks for the help. Next time, I'll count my blessings :)