Generally speaking your best bet is to use something like Moose's concept of defaults/builders. Here's an example of how you can implement something very similar using only core modules:

use v5.14; use warnings; package Horse { # A little bit of metadata about the class. The constructor # uses this to find out what attributes the class supports. sub _attributes { my $class = shift; return ( qw( name colour ) ); } # A fairly standard constructor, similar to MooseX::StrictConstruc +tor sub new { my $class = shift; my $self = bless {}, $class; my %params = @_==1 ? %{$_[0]} : @_; $self->{$_} = delete($params{$_}) for $class->_attributes; die "Unknown parameter(s)... @{[ sort keys %params ]}" if keys %params; return $self; } # Accessors for our attributes, with lazy defaults sub name { $_[0]->{name} //= $_[0]->_build_name } sub colour { $_[0]->{colour} //= $_[0]->_build_colour } # Here are the default values for the attributes sub _build_name { "Cloppity" } sub _build_colour { "brown" } # This is for debugging use JSON::PP; sub dump { my $self = shift; my $class = ref $self; print JSON::PP->new->pretty(1)->canonical(1)->encode({ __CLASS__ => $class, map(+($_ => $self->$_), $class->_attributes), }); } } my $ed = Horse->new( name => "Mr Ed" ); $ed->dump; package Unicorn { use parent -norequire, "Horse"; # Add "horn" to the list of supported attributes. sub _attributes { my $class = shift; return ( $class->SUPER::_attributes(@_), qw( horn ), ); } # Accessor and default value for "horn" attribute. sub horn { $_[0]->{horn} //= $_[0]->_build_horn } sub _build_horn { "medium" } # override the default colour from Horse sub _build_colour { "lavender" } } my $ts = Unicorn->new( name => "Twilight Sparkle" ); $ts->dump; # Note that this dies because of the typo! my $pp = Unicorn->new( naem => "Pinkie Pie" );

In reply to Re: Point me in the right direction with OO inheritance and static variables by tobyink
in thread Point me in the right direction with OO inheritance and static variables by Amblikai

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.