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":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.