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

Re: moose role that uses a class that does the role

by duelafn (Priest)
on Jun 28, 2012 at 14:14 UTC ( #978945=note: print w/ replies, xml ) Need Help??


in reply to moose role that uses a class that does the role

I would guess that you really want to use requires in your role to require certain functionality of the consumer of your role (in this case Greeting) possibly combined with normal inheritance. For example:

package Hello; use Moose::Role; use namespace::autoclean; # Insist that someone who "with"s us, has a greet method requires "greet"; sub hello_world { my $self = shift; $self->greet("hello world\n"); } 1;

Greeting:

package Greeting; use Moose; use namespace::autoclean; with 'Hello'; sub greet { my ($self, @stuff) = @_; print @stuff; } __PACKAGE__->meta->make_immutable; 1;

Greeting2:

package Greeting2; use Moose; use namespace::autoclean; with 'Hello'; sub greet { my ($self, @stuff) = @_; print map uc($_), @stuff; } __PACKAGE__->meta->make_immutable; 1;

Greeting3 (uses the greet from Greeting):

package Greeting3; use Moose; use namespace::autoclean; extends 'Greeting'; # already "with" Hello __PACKAGE__->meta->make_immutable; 1;

Now your test script produces:

Greeting=HASH(0x2b411f8) does Hello hello world Greeting2=HASH(0x1a541f8) does Hello HELLO WORLD Greeting3=HASH(0x15c01f8) does Hello hello world

Update: Added script output. Also, I second tospo's suggestion to move all of the common functionality to the Hello role

Good Day,
    Dean


Comment on Re: moose role that uses a class that does the role
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (3)
As of 2014-10-20 23:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (93 votes), past polls