Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save jjn1056/489956 to your computer and use it in GitHub Desktop.
Save jjn1056/489956 to your computer and use it in GitHub Desktop.
use MooseX::Declare;
role MyParameterizableRole(Str :$prefix = "test")
{
requires 'mymethod';
around mymethod($aaa) {
return $prefix .":". $self->$orig($aaa);
}
}
role MyMethodTrait {
has repeat => (is=>'ro', isa=>'Int', required=>1, default=>2);
around wrap(ClassName $class: $code, %options) {
my $method_obj;
$method_obj = $class->$orig(
sub {
my ($self, $arg) = @_;
$self->$code("$arg:" x $method_obj->repeat );
}, %options,
);
return $method_obj
}
}
class MyClass {
method mymethod(Str $message)
does MyMethodTrait(repeat=>5) {
return $message;
}
with 'MyParameterizableRole' => {prefix=>'hello'};
}
my $aaa = MyClass->new;
warn $aaa->mymethod('world');
## output is "hello:world:world:world:world:world:"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment