Skip to content

Instantly share code, notes, and snippets.

@sestaton
Last active August 29, 2015 14:20
Show Gist options
  • Save sestaton/2e5c3198641808910105 to your computer and use it in GitHub Desktop.
Save sestaton/2e5c3198641808910105 to your computer and use it in GitHub Desktop.
small test for creating custom moose types
package test::role;
use Moose::Role;
use Moose::Util::TypeConstraints;
subtype 'ModNum'
=> as 'Num'
=> where { /\_/ || /\d+/ };
coerce 'ModNum',
from 'Str',
via { $_ =~ s/\_//g; 0+$_ };
package myclass;
use Moose;
with 'test::role';
has 'mynum' => (
is => 'ro',
isa => 'ModNum'
);
package main;
use myclass;
use 5.010;
use strict;
use warnings;
my $cl = myclass->new( mynum => 1_0 );
say $cl->mynum; # 10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment