InsideOut objects using tied hashes
Robert Rothenberg
robrwo at gmail.com
Wed Feb 15 13:04:26 GMT 2006
Hello,
I've hacked together a distribution for implementing inside out objects
using tied hashes:
http://search.cpan.org/~rrwo/Class-Tie-InsideOut-0.03/
I'm curious as to your opinions on the package.
The core of it is a module Tie::InsideOut which ties hash keys to hashes in
the caller's namespace.
The Tie::InsideOut module also handles cleanup of data when untied. And the
tied objects are clonable.
Class::Tie::Inside has a simple constructor,
sub new {
my $class = shift || __PACKAGE__;
my $self = { };
tie %$self, 'Tie::InsideOut';
bless $self, $class;
}
(The actual constructor is a little more complicated, so that inheritance
works.)
One then uses the syntax for "traditional" Perl objects:
sub method {
my $self = shift;
return $self->{Field};
}
which will only work if there's a %Field hash in the class namespace.
Refactoring an existing class to an inside out class is (ideally) a matter
of adding Class::Tie::InsideOut to the @ISA list and adding global hashes
named after the field names.
The downside is that your don't really get encapsulation, since the hashes
are "our" variables and child classes can sill access fields. Which negates
the benefits of inside-out classes.
So I'm curious as to how to whether caller(1) or some other method can be
used to reliably indicate which class a method accessing a key is from.
Regards,
Rob
More information about the london.pm
mailing list