in reply to Point me in the right direction with OO inheritance and static variables
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" );
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Point me in the right direction with OO inheritance and static variables
by AppleFritter (Vicar) on Sep 02, 2014 at 10:54 UTC | |
|
Re^2: Point me in the right direction with OO inheritance and static variables
by Amblikai (Scribe) on Sep 02, 2014 at 11:41 UTC | |
by tobyink (Canon) on Sep 02, 2014 at 12:03 UTC | |
by Amblikai (Scribe) on Sep 02, 2014 at 16:18 UTC | |
by tobyink (Canon) on Sep 02, 2014 at 19:11 UTC | |
by Amblikai (Scribe) on Sep 10, 2014 at 09:46 UTC | |
| |
by tangent (Parson) on Sep 02, 2014 at 19:10 UTC |