http://qs321.pair.com?node_id=497910


in reply to Re: Object Constructors - Advice of experienced OO Perl Monks
in thread Object Constructors - Advice of experienced OO Perl Monks

What's MI?

Btw, I can't see why you can't initialize your own object from constructor. One of the definitions I found about this is: "A constructor is responsible for preparing the object for action, and in particular establishing initial values for all its data, i.e. its data members. Although it plays a special role, the constructor is just another member function, and in particular can be passed information via its argument list that can be used to initialise it" (here).


Igor S. Lopes - izut
surrender to perl. your code, your rules.
  • Comment on Re^2: Object Constructors - Advice of experienced OO Perl Monks

Replies are listed 'Best First'.
Re^3: Object Constructors - Advice of experienced OO Perl Monks
by ides (Deacon) on Oct 06, 2005 at 15:01 UTC

    MI == multiple inheritance

    What he is basically saying is instead of having something like:

    sub new { my ($class, %args) = @_; my $self = bless( {}, $class ); foreach my $k ( keys(%args) ) { $self{$k} = $args{$k}; } return( $self ); }

    do something like:

    sub new { my ($class, %args) = @_; my $self = bless( {}, $class ); $self->init(%args); return( $self ); } sub init { my ($self, %args) = @_; foreach my $k ( keys(%args) ) { $$self{$k} = $args{$k}; } # anyother initialization here }

    This makes a clean separation between the construction and the initialization with no real downside.

    Frank Wiles <frank@wiles.org>
    http://www.wiles.org

      No, you didn't understand me. Sure, the code looks cleaner, but you still have the same problem. The problem is that your initialization happens with whatever your constructor is returning. So, if you are inheriting from two classes, the first constructor is going to return object1. The second constructor is going to return object2. Now, what you finally want is a single object - most likely object1 or object2. If you separate initialization from construction (and then I mean a logical separation - not a mere separation in your source code), you can just discard one of the objects (or rather, not even calling the constructor of those classes).
      package MultiColoured; our @ISA = qw /Red White Blue/; sub new {my $self = Red->new; bless $self, shift} sub init { my $self = shift; $self->Red::init(...); $self->White::init(...); $self->Blue::init(...); } ... package main; my $obj = MultiColoured->new->init(...);

      That doesn't mean MI is always possible. If in the above example Red assumes objects are hashrefs, and Blue assumes arrayrefs, MI isn't going to work. But if all the classes assume hashrefs (as most objects do), and you don't have clashes with the hashkeys, this is going to work.

      But if you need to call the constructor to initialize the object, MI is going to fail.

      Perl --((8:>*

        We are arguing about taste here :) I dislike the MI approach, and really prefer initialize the object inside constructor to be sure my object will be instantiated correctly.

        Thinking about MI, your scheme is correct.


        Igor S. Lopes - izut
        surrender to perl. your code, your rules.