Last active
August 29, 2015 14:20
-
-
Save sestaton/2e5c3198641808910105 to your computer and use it in GitHub Desktop.
small test for creating custom moose types
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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