http://www.perlmonks.org?node_id=915620

Boldra has asked for the wisdom of the Perl Monks concerning the following question:

I want a role to consume another role, and provide an attribute that the consumed role requires, but Moose won't see the attribute provided by the first role.

An example:

package Role::Logger; use Moose::Role; requires qw<smeg>; package Role::App; use Moose::Role; has smeg => ( is => 'rw' ); with 'Role::Logger'; package App::FixIt; use Moose; with "Role::App"; sub run { print "smeg=" . shift->smeg; } package main; use Moose; App::FixIt->new( { smeg => 1 } )->run;

This code returns the error 'Role::App' requires the method 'smeg' to be implemented by 'App::FixIt'. But Role::App doesn't have any requirements, it's Role::Logger which requires smeg, and smeg is provided by Role::App.

The Moose documentation to required attributes warns that a requirement met by an attribute in a consuming class must be met before the role is consumed, so:

package Role::App; use Moose::Role; requires 'gazpacho'; package App::FixIt; use Moose; with Role::App; has gazpacho => ( is => 'rw' );
breaks because the requirement is met after the role is consumed. Here, swapping the order of with and has solves the problem. OTOH, requirement met by a subroutine would also be ok, because the subroutine is defined before with imposes the required constraints at runtime.

In summary: If a role requires a method and the requirement is met by:
a sub in a consuming classNo problem
a sub in a consuming roleNo problem
an attribute in a consuming classbe careful of the has/with order
an attribute in a consuming rolecan't be done?

Thanks for your thoughts!