Hi PerlMonks,

My question concerns tied object classes. I would like to know if there is a way to prevent the DESTROY method from being called twice (once for the object, and once for the underlying hash that is tied) for objects such as the one I've created below. I know I can check "tied $this" within DESTROY, but this is truly a vile hack of a solution. Essentially what I would like to know is if there is a way to seperate the "magical" destructor for the tied object from the class destructor:

package SillyClass; require Tie::Hash; @ISA = (Tie::Hash); sub new { my $proto = shift; my $class = ref ($proto) || $proto; my $this = bless { }, $class; tie %{$this}, $class; return $this; } sub TIEHASH { my $self = shift; my $node = { color => 'blue', age => 12, pet => 'dog', }; return bless $node, $self; } sub DESTROY { my $self = shift; print "Calling DESTROY by $self\n"; } #------end class definition------ package main; my $fred = SillyClass->new(); undef $fred; ## DESTROY gets called twice, once by "$fred" and by + "tied $fred"
You may notice that this a wierd way of building a tied object. I don't know if this is common programming practice (specifically, the tie %{$this}, $class; line right within the object constructor), but it seems to work quite well. I build classes this way in order to allow access to an object's attributes through the object->{attribute} notation, while reserving the ability to mediate this access in the same way that attribute accessor/mutator methods would. Thus, I retain the straightforward hash-key object attribute syntax while also enjoying all the "object-oriented encapsulation" benefits of the the method call syntax.

So I suppose I'm also asking another question: is there anything "wrong" about doing things in this way in the first place? Thanks very much for your help.


In reply to 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.