Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

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;


Comment on Re^4: Moose "unions" and inheritance
Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1006541]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (5)
As of 2015-07-04 05:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (57 votes), past polls