Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

Hi Monks!

I just ran into a problem that to me looks like Moose Handles and Roles don't work together well. Please take a look at the following role, class and test program:

# TestRole role: package TestRole; use Moose::Role; requires 'test_method'; 1; # Consumer class: package Consumer; use Moose; with 'TestRole'; has 'test_attribute' => ( traits => ['Array'], is => 'rw', isa => 'ArrayRef[Str]', default => sub { [] }, handles => { test_method => 'elements', add_something => 'push', }, ); 1; # tester.pl: use Data::Dumper; use Consumer; my $c = Consumer->new(); $c->add_something("123"); my @arr = $c->test_method(); print Dumper(\@arr) . "\n";

Now when I try to run this it tells me:

'TestRole' requires the method 'test_method' to be implemented by 'Con +sumer'

From reading the following part of the Moose manual about roles I understand that this is the desired behaviour. "It should be noted that this does not include any method modifiers or generated attribute methods (which is consistent with role composition)." But I don't understand the motivation for it. Why shouldn't I be able to define the handles as required by the role? I want to use the Role as a handle itself in another class to delegate all those methods, including the attribute handles. Why isn't that allowed? Does that mean my only option is to manually write subs for the handles (e.g. convert

handles => { test_method => 'elements', add_something => 'push', },
into
sub add_something { my ($self, %args) = @_; # manually push here... } sub test_method { # add the 'elements' code here. }
Thanks for your help!

In reply to Moose Handles and Roles by elTriberium

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (2)
As of 2024-04-26 03:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found