Wise ones...

I have been playing with ideas I have read in relation to InsideOut - hiding object data inside of a lexical block within a class. I have been trying to bind the data even more tightly to the methods. However, since DESTROY() needs to be called for proper housekeeping, I am finding it difficult to accomplish.

I have searched the tomes here, but have not been successful in finding what I am looking for. Feel free to point out where I have missed :)

What I have

I know that this is not complete - I mean look at the names of the variable ;). Bugs with data checks aside... (thanks diotalevi ;)

use strict; use warnings; package testhiding; sub new { bless [], shift; } { my %foo = (); my %bar = (); sub foo { my $self = shift; my $new = shift; my $old = $foo{$self}; $foo{$self} = $new if defined($new); $old; } sub bar { my $self = shift; my $new = shift; my $old = $bar{$self}; $bar{$self} = $new if defined($new); $old; } sub DESTROY { my $self = shift; delete $_->{$self} for (\%foo, \%bar); } } 1;

What I would like

use strict; use warnings; package testhiding; sub new { bless [], shift; } { my %foo = (); sub foo { my $self = shift; my $new = shift; my $old = $foo{$self}; $foo{$self} = $new if defined($new); $old; } } { my %bar = (); sub bar { my $self = shift; my $new = shift; my $old = $bar{$self}; $bar{$self} = $new if defined($new); $old; } } # XXX - Where does this go then... sub DESTROY { my $self = shift; delete $_->{$self} for (\%foo, \%bar); } 1;

I know why DESTROY does not work, that is not the problem. I would like to make DESTROY work somehow. The structure to allow that while also allowing the tighter coupling of the data to the methods is where I am getting stuck.

Any ideas?

Update: Lack of data checking and bugs in the code acked. Since that isn't the intent of the question, take all code as a sample, not as should be written, etc. :)

In reply to InsideOut - even tighter coupling by MidLifeXis

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.