"Composition_over_inheritance" was the mantra that i learned back in 1998 when i attended my first advanced OO class in college. The bad news is that it took me nearly 10 years to understand. :) The good news is that packages like Moose make all of this easier to implement than not. Need to define an interface?

package My::Abstract::Interface; use Moose::Role; requires qw( foo bar baz qux );
instead of
package My::Abstract::Interface; use Carp; sub foo { croak } sub bar { croak } sub baz { croak } sub qux { croak }
If you think about, this is a form of encapsulation itself -- by making it easier to define a proper abstract interface, coders are more tempted to pick the easier-to-code-and-maintain version. Moose (et. al.) make it very easy to delegate and aggregate, without resorting to inheritance:
#!/usr/bin/env perl package Foo; use Moose; has string => ( is => 'rw', isa => 'Str' ); sub to_string { shift->string } package Bar; use Moose; has _foo => ( is => 'rw', isa => 'Foo', handles => ['string'] ); around BUILDARGS => sub {my($o,$c)=(shift,shift);$c->$o(_foo => Foo->n +ew(@_))}; sub to_string { uc shift->string } package main; my $foo = Foo->new( string => 'hello world' ); print $foo->to_string, $/; my $bar = Bar->new( %$foo ); print $bar->to_string, $/;
For me, this is actually easier than using inheritance with classic Perl OO.

UPDATE:
Changed arguments to Bar constructor. (Was the same as arguments to Foo constructor.)

jeffa

L-LL-L--L-LL-L--L-LL-L--
-R--R-RR-R--R-RR-R--R-RR
B--B--B--B--B--B--B--B--
H---H---H---H---H---H---
(the triplet paradiddle with high-hat)

In reply to Re: OOP: How to (not) lose Encapsulation by jeffa
in thread OOP: How to (not) lose Encapsulation by Arunbear

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.