In short, you can't:
#!/usr/bin/env perl -l
{
package My::Role;
use Moose::Role;
use Memoize;
memoize('foo');
sub foo { __PACKAGE__ }
}
{
package My::Class;
use Moose;
with 'My::Role';
}
print My::Class->foo;
If you comment out the 'memoize(foo)' line, it works fine. This is because
methods/subs not defined in the role itself will not be composed into your
class. memoize() rewrites the method and thus the 'foo' method is defined in
the Memoize module.
This clearly is not a bug, but it's a surprising behavior. I'd hate to have to
put the memoize(foo) call into all classes which consume the role. Thus, it
would be nice if there was either a special case (yuck) for Memoize or that
Moose provides something similar.
Side note: for Moose::Manual::Role, 'brake' is misspelled as 'break' in the Car
example. I know why it was done this way, but a better example would be nice :)
Cheers,
Ovid
--
Buy the book - http://www.oreilly.com/catalog/perlhks/
Tech blog - http://use.perl.org/~Ovid/journal/
Twitter - http://twitter.com/OvidPerl
Official Perl 6 Wiki - http://www.perlfoundation.org/perl6