Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: Object Constructors - Advice of experienced OO Perl Monks

by Perl Mouse (Chaplain)
on Oct 06, 2005 at 12:20 UTC ( #497895=note: print w/replies, xml ) Need Help??


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

IMO, constructors should only construct the object, and not initiate them. If you do so, multiple inheritance becomes even more a pain than it already is.

Suppose for instance there are two classes which you didn't write yourself (get them from CPAN, from a coworker, elsewhere). Both of them initiate the object returned from the constructor in the constructor themselves. Perhaps one of the classes uses a blessed hashref as object, the other the inside-out objects from Damians book. If the classes don't initiate the object in the constructor, MI is possible. If they do, MI is either impossible, or it's going to be hard.

Perl --((8:>*
  • Comment on Re: Object Constructors - Advice of experienced OO Perl Monks

Replies are listed 'Best First'.
Re^2: Object Constructors - Advice of experienced OO Perl Monks
by izut (Chaplain) on Oct 06, 2005 at 13:05 UTC

    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.

      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:>*

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://497895]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (2)
As of 2022-08-17 03:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?