MidLifeXis has asked for the wisdom of the Perl Monks concerning the following question:
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 :)
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;
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. :)
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: InsideOut - even tighter coupling
by demerphq (Chancellor) on Oct 10, 2003 at 17:58 UTC | |
by MidLifeXis (Monsignor) on Oct 10, 2003 at 18:04 UTC | |
|
Re: InsideOut - even tighter coupling
by diotalevi (Canon) on Oct 10, 2003 at 17:51 UTC | |
by MidLifeXis (Monsignor) on Oct 10, 2003 at 18:01 UTC | |
|
Re: InsideOut - even tighter coupling
by yosefm (Friar) on Oct 13, 2003 at 07:39 UTC | |
by linux454 (Pilgrim) on Oct 13, 2003 at 14:13 UTC | |
by Aristotle (Chancellor) on Oct 13, 2003 at 17:45 UTC | |
by MidLifeXis (Monsignor) on Oct 15, 2003 at 17:10 UTC |