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.


In reply to Re: OO in Perl 5: still inadequate by xdg
in thread OO in Perl 5: still inadequate by Aristotle

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.