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

    Uhm.

    Wow.

    All I can say is, d’oh. I don’t know why something so obvious never occured to me; sometimes I can’t see the forest for the trees.

    Cool! Thanks for pointing this out.

    Makeshifts last the longest.

      I don’t know why something so obvious never occured to me

      Don't worry about it. <grin> It happens to all of us at some time or another. Inside-out objects are unusual enough still that it takes a little more effort to pick apart the different angles on it.

      In my presentation, I open by stating there are three fundamental concepts for people to take away:

      1. Objects as containers versus objects as indices

      2. Encapsulation via lexical closure

      3. Memory addresses as unique identifiers

      Everything else is just combinations and variations of these three ideas.

      -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.