Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

Re^4: Moose "unions" and inheritance

by remiah (Hermit)
on Dec 01, 2012 at 00:22 UTC ( #1006541=note: print w/replies, xml ) Need Help??

in reply to Re^3: Moose "unions" and inheritance
in thread Moose "unions" and inheritance

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;

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1006541]
and one hand claps...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (8)
As of 2018-06-22 10:28 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (124 votes). Check out past polls.