in reply to OO in Perl 5: still inadequate
4. Encapsulation is advisory; subclasses require no implementation knowledge of the superclass.
Actually, that's easy. Just use package variables instead of lexical variables.
package My::Class; use Scalar::Util qw( refaddr ); # package hash for inside-out properties, instead of lexical our %name; sub new { my $class = shift; bless {}, $class; } sub name { my $self = shift; if (@_) { $name{ refaddr $self } = shift } return $name{ refaddr $self }; }
I did a variation on that for my highly experimental Object::LocalVars and called it "outside-in".
Actually, while I haven't tested it, there's probably no reason it couldn't be done today with thread safety and all the rest with Class::InsideOut -- it only needs a hash and it doesn't check to see if it's a package hash or a lexical hash. You'll get thread safety, Storable support, etc.
Equivalent to the example above, but robust:
package My::Class; use Class::InsideOut qw( public register ); public name => our %name; sub new { my $class = shift; register( bless( {}, $class ) ); }
P.S. I did a quick test changing a test-suite property to our instead of my and the test suite ran fine, so I suspect the outside-in variation works fine with Class::InsideOut.
-xdg
Code written by xdg and posted on PerlMonks is public domain. It is provided as is with no warranties, express or implied, of any kind. Posted code may not have been tested. Use of posted code is at your own risk.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: OO in Perl 5: still inadequate
by Aristotle (Chancellor) on Jan 21, 2006 at 15:37 UTC | |
by xdg (Monsignor) on Jan 21, 2006 at 16:08 UTC |