sub name {
my $self = shift;
if (not defined $self->{name}) {
$self->{name} = $self->_build_name();
}
return $self->{name};
}
####
sub name { $_[0]->{name} //= $_[0]->_build_name }
sub colour { $_[0]->{colour} //= $_[0]->_build_colour }
sub owner { $_[0]->{owner} } # this one has no default
sub height { $_[0]->{height} //= 2.5 } # another way to provide a default
...;
####
my $aj = Horse->new(name => "Applejack");
my $metres = $aj->height( in => "metres" );
my $inches = $aj->height( in => "inches" );
my $hands = $aj->height( in => "hands" );
my $silly = $aj->height( in => "lightyears" );