# 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";
####
'TestRole' requires the method 'test_method' to be implemented by 'Consumer'
##
##
handles => {
test_method => 'elements',
add_something => 'push',
},
##
##
sub add_something {
my ($self, %args) = @_;
# manually push here...
}
sub test_method {
# add the 'elements' code here.
}