Frustrated Moose

Well leaving off from my last post I was a little frustrated with Moose but not as much as our Moose friend below.


So after a good night of sleep and a large pot of coffee I stumbled on a Moose solution.

Seems some-one else must of had the same problem, some time before my entry into the Moose space, and a quick search of the MooseX namespace and I found 'MooseX::Role::BuildInstanceOf'* which looked to be what I wanted. So here we go

Well in my creator class I started from scratch again with

 package RPG::ADD::Creator;
 use Moose;
 with 'MooseX::Role::BuildInstanceOf' => {
		target => 'RPG::ADD::Creator::Strength',
		prefix => 'Strength',
 	        constructor => 'new',
               inherited_args  => [{roll=>'strength'}],};
and in the ability area I added in my delegation like this

has 'strength' =>(
        is           =>'rw',
        isa         =>'AbilityRoll',
And in my 'Strength' class I do this

package RPG::ADD::Creator::Strength;
  use Moose;
  has 'roll' =>(
        is    =>'rw',
So then when I give this a try

my $str = RPG::ADD::Creator->new({strength=>9,

print "strength=".$str->strength()."\n";
I get this

and as a bonus the 'AbilityRoll' type still works

Attribute (strength) does not pass the type constraint because: AbilityRoll must be between '3' and '18' not '2' at ...

So what did I do here? Well with my 'BuildInstanceOf'' I created an instance of a my 'Strength' class in my present class that I can access with 'Strength' and I was even able to pass to it my value for 'roll' into may class.

* Just as a side note. I had to add a little patch to this mod to stop 'Class::MOP::load_class deprecation warnings'.

Leave a comment

About byterock

user-pic Long time Perl guy, a few CPAN mods allot of work on DBD::Oracle and a few YAPC presentations