Karen,  thanks for the advice!

Let me see if I can get unconfused:

I don't want to pass a type parameter to the role - I want it to construct it! This is why I have a builder. I'd really like it to construct $type on its own, based on package name. What's the best way to do that? Or should I just quit being cute and do something like

with Role => {child => __PACKAGE__};

Come to think of it, it really isn't so bad. :-) Still, I am curious to understand if parameterized roles will support a real builder and how to achieve it.


On Apr 22, 2010, at 4:49 PM, Karen Etheridge wrote:


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, ka...@etheridge.ca GCS C+++$ USL+++$ P+++$ w--- M++ http://etheridge.ca/ PS++ PE-- b++ DI++++ e++ h (-)

Reply via email to