Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
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:>*

In reply to Re^4: Object Constructors - Advice of experienced OO Perl Monks by Perl Mouse
in thread Object Constructors - Advice of experienced OO Perl Monks by ghenry

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (2)
As of 2022-10-02 10:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My preferred way to holiday/vacation is:











    Results (8 votes). Check out past polls.

    Notices?