I said Simpleton not Singlton!!

In my last post I said I was going to look at my 'Character Creation' part of my application and since then I have been doing a little digging and I believe that Moose will help me out here.


So I thought I might just want only one of these for my app and looked at 'MooseX::Singleton' but with a round of sober second thought I dropped that idea. Seems if I did that then I will have only one 'Instance' about which is good for say my DB connection but not so good if I want more than 1 user creating character's at a time. But at least I did see how Moose will help me.

As my BPM model indicates there are at maximum only 3 choices that must be made in character creation.

  1. Choose Race
  2. Choose Class
  3. Choose Alignment

So I would like to design my little 'Class' to work so you can go back and forth though the three stages above and if the case warrants it skip a stage if that is the case. For example an Ability rolls of 5 and or less on Intelligence means you can only be a 'Fighter' no mater what race you choose.

So how to build something like this?? Well I of course start with a new class with the standard ability attributes that must be between 3~18, I will need one for 'Race' as well and one for 'Class' but that one should take into account that there are multi-class characters out there, fortunately humans always starts with only 1 'Class', so I can have an array-ref for that one.

So there looks like a number of hurdles to overcome. I will go for the easy one the need to have my ability scores all between 3~18.

Fortunately in Moose we can create our own types. So All I need to do is create this little package

package RPG::ADD::Util::Types;
  use Moose::Util::TypeConstraints;

 subtype 'AbilityRoll',
      as 'Int',
      where { $_ > 2 and $_ < 19 },
      message { "AbilityRoll must be between '3' and '18' not '$_'" };

So all I did was create a new type 'AbilityRoll' which is an 'Int' and has to be greater than 2 and less than 19 and I can even add in a custom message. Note I now using my RPG::ADD namespace.

Now in my 'Creator' class I just use it in the 'isa' like I use 'Int' in the past.

package RPG::ADD::Character::Creator;
 use Moose;
 use RPG::ADD::Util::Types;
 has 'constitution' =>(
	is		=>'ro',
	isa		=>'AbilityRoll',
and If I did this

use RPG::ADD::Character::Creator;
use Data::Dumper;
my $str = RPG::ADD::Character::Creator->new({

I get this

Attribute (intelligence) does not pass the type constraint because: AbilityRoll must be between '3' and '18' not '1' at xxx:\Dwimperl\perl\site\lib\Moose\Exception.pm line 37
So Moose fixes another little problem for me.

Though it does bring to mind why even do this as perl is suppose to be a type-less language? Well we are really just validating out entry here ;)

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