Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

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

by duelafn (Parson)
on Jun 28, 2012 at 14:14 UTC ( [id://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

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (3)
As of 2024-04-19 20:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found