A constructor is something that allocates a new object and returns a reference to it. If your "new" returns three (references to) objects, it is more like a factory, although since it didn't delegate the actual construction to anyone, it also includes the constructor. Confusing? Good, because this is not how you'd usually want to do this. :-)

As have already figured out in your Q5, what you usually want to do is create something that contains three objects. Very typically this is a hash in Perl, especially if each element object fulfills a role. So Human->new() may call Arm->new() and Leg->new() twice each; and put the resulting members in its fresh new $self->{right_arm}, $self->{left_arm}, $self->{right_leg}, and $self->{left_leg}.

You would then access the nested objects via indirection, which is a bit slower but not significantly so for most purposes. For example:

$person = Human->new(); $person->{right_arm}->grasp("banana"); # not through accessor $person->get_right_arm->grasp("banana"); # through accessor # or, you might write accessors like this too if you think they'd be u +seful: $person->arm("left")->grasp("orange");

What you should write depends on what you really need. What do your objects represent?


In reply to Re: On Perl Objects! by gaal
in thread On Perl Objects! by newbio

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.