Search code examples
perlinheritancemoose

Moose Parent and Child with same attribute names


This question is in regards to Perl and Moose inheritance.

Suppose I have a Parent Class and a Baby Class. Baby 'extends' Parent. If both classes have the attribute 'name', how does the Baby access the parent's name without getting its own name?

package Baby;
use Moose;
extends 'Parent';

has 'name', is => 'ro', isa => 'Str', default => 'Baby';

Note: The Parent Class is essentially the same with name default => 'Parent'.

So the question is, inside my program how would I access the Parent's name?

For example:

...
$baby = Baby->new();
say "my name is " . $baby->name();
say "my Parent's name is " . ???? 

Thanks!


Solution

  • You don't. You haven't made a new attribute; you've just overriden the one from the parent class. The names you've chosen for your example classes are really unfortunate, because in the real world a baby "has-a" parent, but by using inheritance you've created a situation where Baby "is-a" Parent.