This is a much better Perl OO primer than the one I previously linked: https://www.perl.com/article/25/2013/5/20/Old-School-Object-Oriented-Perl/ . (I am glad to see that it sends parameters to functions as a hashtable by reference instead of by value).

But I forgot to mention one important detail. Huge really. If the inherited (child) class has its own constructor (overriding - or more brutally, overwriting - the parent's constructor) then you must call in it the parent's constructor yourself before you do any more initialisation. Like so:

package Child; use parent 'Parent'; sub new { my ($class, $params) = @_; # call parent's constructor first with the given parameters # the parent should ideally filter out the parameters # that do not concern it and ignore them rather than complaining a +bout illegal params my $self = $class->SUPER::new($params); # at this point $self has all Parent's initialisation # (whatever was done and set in its constructor). # Now do local initialisation on $self which is Parent's self (a { +} in most cases) ... $self->{child_extra_property_1} = 12; # for example ... # and bless to Child - actually that's a re-bless because calling # Parent's constructor blessed $self to be an object of class 'Par +ent' # so now make it of class 'Child' bless $self => $class; return $self; }

bw, bliako


In reply to Re^4: Mass Class Confusion - Who calls what how? by bliako
in thread Mass Class Confusion - Who calls what how? by holandes777

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.