No Moose is a Good Moose

Well after my Dice adventure I had a few bookmarked mods (Acme::Dice, Games::Dice::Loaded, Games::Dice::Advanced, Games::Dice, RPG::Dice, Games::Die::Dice and the one with the coolest name Games::Die that I looked at.

Being a good perl programmer I always like to do things the simple way by stealing I mean borrowing the hard work that others have done by consulting CPAN and as one can see from my little list above there are a good deal of them to choose from, one is written with Moose (Games::Dice::Loaded).

All I need is simple dice rolling so the ones that did other things where out, others allow some sort of 'Cheat' factor and I did not like that so they where out as well those that didn't have an OO interface where out.

That left me with 'Game::Die', 'Games::Die::Dice', and ' RPG::Dice'. 'Game::Die' did too little as it was just a instance of a single 'die', so that would mean more work for me.

So between 'Games::Die::Dice', and ' RPG::Dice' I went with the latter as it let me do the very common AD&D game feature of adding a '+' or '-' to a dice roll?

Now comes the 'Moose' part. I would like to be able to create 'Dice rolls' roles for for specific events on classes. For example when a 'Character' gains a level I would just like to have a the same 'new_hp_roll' sub common to all the 'Player Character' roles.

Like the picture below, Moose allows us to turn other Perl modules into Moose modules


So here goes

package Dice;
use Moose;
use MooseX::NonMoose;
extends 'Games::Die::Dice';

And that is about it. With the neat little MooseX::NonMoose you have instant Moose.

use warnings;
use Dice;
use Data::Dumper;
my $throw = Dice->new('3d6');
print scalar($throw->roll());


If I tried this I would end up with this

Single parameters to new() must be a HASH ref at C:\Dwimperl\perl\site\lib\Moose\ line 37
Moose::Exception::_build_trace('Moose::Exception::SingleParamsToNewMustBeHashRef=HASH(0x1a412f4)') called at reader Moose::Exception::trace (defined at C:\Dwimperl\perl\site\lib\Moose\ line 9) line 7

What is going on? Well Moose always wants to have a 'Hashref' on a new (or nothing) but 'Games::Die::Dice' wants a scalar not a hashref. So I am snookered again.

Well not yet you might remember this post where I learned that 'BUILDARGS' though useless in that case, can be applied here. So I added in;

around BUILDARGS => sub {
      my $orig        = shift;
      my $a_moose= shift;
     return $a_moose->$orig(sides=>@_);
So what am I doing here?? Well I looked at the source of Games::Die and noticed that the 'sides' is an attribute

  my $self = {
    sides => $sides,

So all I have to do is return the new '$a_moose' object with the 'sides' attribute as a param, (it is just the '@_' at this stage) and it works.

Now I have a nice little Moose class that I can use where ever I want.

Of course in this case the actual amount of code I saved here is only a few doz of lines of simple code but now I have


So I am happy.

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