You are getting an error because the role is attempting to create an
attribute that doesn't exist, because you don't pass a type parameter to
the role. You should mark that parameter as required, or check that it
exists first, e.g. one of:
parameter 'type' =>(
isa => 'Str',
required => 1,
builder => '_type',
);
or:
if (my $type = $p->type)
{
has $type =>(is => 'rw');
}
You could also use a default instead of a builder:
parameter 'type' =>(
isa => 'Str',
default => 'human',
);
The error message certainly isn't clear, and it suggests that the parameter
object does not exist at all in the parameterized role when you do not pass
a parameter (which does not parallel normal Moose behaviour -- an attribute
metaobject always exists on the object, even if one constructs an object
with no values).
On Thu, Apr 22, 2010 at 04:31:20PM -0700, Kate Yoak wrote:
> Here is the most straightforward approach that fails with the error
> above (the error occurs while trying to use Me):
>
> package Role;
> use MooseX::Role::Parameterized;
>
> sub _type{ 'human'; }
>
> parameter 'type' =>(
> isa => 'Str',
> required => 0,
> builder => '_type',
> );
> role{
> my $p = shift;
> my $type = $p->type;
> has $type =>(is => 'rw');
> };
>
> 1;
>
> package Me;
> use Moose;
> with 'Role' => { };
> 1;
>
--
New and stirring things are belittled because if they
are not belittled, the humiliating question arises,
"Why then are you not taking part in them?" - H.G.Wells
. . . . .
Karen Etheridge, [email protected] GCS C+++$ USL+++$ P+++$ w--- M++
http://etheridge.ca/ PS++ PE-- b++ DI++++ e++ h(-)