Hi,
I am trying to create a role where an attribute is required. To be
more specific, I have a method in a Moose::Role that needs to have an
attribute set. I'm sure there is a way to do this but I can't seem to
find it in the docs. In the example below I'd like to make sure that
the Manager Role can only be applied if the pen attribute is set. With
the code below, any attempt to create an Employee causes an exception
unless the pen is given to the constructor but I only want to set pen
for Managers not all Employees.
Can anyone suggest a way to do this?
Thanx,
Dp.
package Employee::Manager;
use Moose::Role;
has 'pen' =>
is => 'rw',
isa => 'Bool',
required => 1,
);
sub signs_cheques {
my $self = shift;
print $self->name;
};
1;
package Employee;
use Moose;
with 'Employee::Manager';
has 'name' =>
is => 'rw',
isa => 'Str',
);
1;
== in a script not too far away ===
use Employee;
my $brent = Employee->new(name => 'David Brent', pen => 1);
Employee::Manager->meta->apply($brent); # ok
my $tim = Employee->new(name => 'Tim Canterbury');
Employee::Manager->meta->apply($tim); # Boom!!