Thanks for good example and clear explanation...
So, all methods of Person using name attribute get troubled.
At first I thought "around" at SecretAgent will work somehow, but "name" is already overrided and it is "ArrayRef" at Person's introduce_yourself method.
This is really caution for me.
{
package Person;
use Moose;
has name => (is => 'ro', isa => 'Str');
sub introduce_yourself {
my $self = shift;
printf("My name is %s\n", $self->name);
}
}
{
package SecretAgent;
use Moose;
extends 'Person';
# secret agents have many aliases
has '+name' => (isa => 'ArrayRef');
=pod
#override and disable Person's introduce yourself
override introduce_yourself => sub {
my $self = shift;
my @names = @{ $self->name };
my $name = $names[ rand @names ];
my $surname = (split / /, $name)[-1];
printf("The name's %s, %s\n", $surname, $name);
}
=cut
#cant' "around" because name is also arrayref at Person
around 'introduce_yourself' => sub {
my $coderef=shift;
my $self=shift;
my @names= @{$self->name};
my $name = $names[ rand @names ];
print "in around, name=$name\n";
$self->$coderef($name); #oh, what is passing $name to introduc
+e_yourself?
#silly me.
}
}
my $bond = SecretAgent->new(
name => ['James Bond', 'Burt Saxby', 'David Somerset'],
);
$bond->introduce_yourself;