X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FMoose%2FManual%2FRoles.pod;h=009a2ea8771542f1a807c7a83b42337d976fcdf1;hb=topic%2Fdocument-instance-role-application;hp=51cf93db6ead051178670b0f6d7f54e1ea5d26b1;hpb=ee05962b4ec91f86a0dc19ceddb869ca4e609a67;p=gitmo%2FMoose.git diff --git a/lib/Moose/Manual/Roles.pod b/lib/Moose/Manual/Roles.pod index 51cf93d..009a2ea 100644 --- a/lib/Moose/Manual/Roles.pod +++ b/lib/Moose/Manual/Roles.pod @@ -326,4 +326,45 @@ normal Moose role combination system. We recommend using this function to apply roles to an object. This is what Moose uses internally when you call C. +=head2 Handling required attributes for roles. + +Application of some roles will require additional parameters being specified to +satisfy them, for example: + + { + package Car; + use Moose; + } + { + package Breakable; + use Moose::Role; + + has 'breakable_parts' => ( is => 'ro', required => 1 ); + } + + my $car = Car->new; + + # next line dies with: Attribute (breakable_parts) is required + apply_all_roles( $car, 'Breakable' ); + +This will require passing the additional parameters at application time as +follows: + + apply_all_roles( $car, 'Breakable' => { + rebless_params => { + # Parameters to 'Breakable' + breakable_parts => [qw( tires wheels windscreen )], + } + }); + +Obviously, this interface is better simplified as a method on C: + + sub make_breakable { + my ( $self, %params ) = @_; + apply_all_roles($self, 'Breakable', { rebless_params => \%params }); + } + + my $car = Car->new(); + $car->make_breakable( breakable_parts => [qw( tires wheels windscreen )] ); + =cut