Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
bless do { my %self = (%{Colour->new($colour)}, %{Age->new($age)}); \% +self }, $class;
So, you break encapsulation, and will depend on how things are implemented. If you're willing to break encapsulation, why even bother with OO?
The other solution is to go the inside-out object route. If your class doesn't care about the contents of the reference, then all is fine and dandy.
Inside-out objects aren't a silver bullet, and won't save the naive programmer.
package Colour { use Scalar::Util 'refaddr'; my %colour; sub new { my $o = bless \do{my $var}, $_[0]; $colour{refaddr $o} = $_[1]; $o; } sub colour {$colour{refaddr $_[0]}} }; package Age { use Scalar::Util 'refaddr'; my %age; sub new { my $o = bless \do{my $var}, $_[0]; $age{refaddr $o} = $_[1]; $o; } sub age {$age{refaddr $_[0]}} }; package Colour_and_Age { our @ISA = qw[Age Colour]; use Scalar::Util 'refaddr'; sub new { my ($class, $colour, $age) = @_; ... Now what? ... } }
Packages Colour and Age are written as inside-out objects, but because the same method constructs and initializes the object, the author of Colour_and_Age is screwed. He doesn't even have the luxury of breaking encapsulation.

Now, if one separates object construction and object initialization, for instance:

use Scalar::Util 'refaddr'; package Colour { use Scalar::Util 'refaddr'; my %colour; sub new {bless \do{my $var}, $_[0]} sub init {$colour{refaddr $_[0]} = $_[1]; $_[0]} sub colour {$colour{refaddr $_[0]}} }; package Age { use Scalar::Util 'refaddr'; my %age; sub new {bless \do{my $var}, $_[0]} sub init {$age{refaddr $_[0]} = $_[1]; $_[0]} sub age {$age{refaddr $_[0]}} };
then one can use multiple inheritance without any problem:
package Colour_and_Age { use Scalar::Util 'refaddr'; our @ISA = qw[Age Colour]; sub new {bless \do{my $var}, $_[0]} sub init ( my ($self, $colour, $age) = @_; $self->Colour::init($colour); $self->Age::init($age); $self; } }; my $o = Colour_and_Age::->new->init(red => 42);
It only takes a few more keystrokes to create your classes that way, and you aren't robbing any users from using your classes with multiple inheritance. Also note you don't need inside-out objects to separate constructions and initialization.

In reply to Re^8: Perl "new" command by JavaFan
in thread Perl "new" command by akagrawal3

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, 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, 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.
  • Log In?

    What's my password?
    Create A New User
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others examining the Monastery: (7)
    As of 2018-10-19 10:48 GMT
    Find Nodes?
      Voting Booth?
      When I need money for a bigger acquisition, I usually ...

      Results (107 votes). Check out past polls.