Skip to content

Instantly share code, notes, and snippets.

@hdp
Forked from EvanCarroll/gist:130258
Created June 15, 2009 19:22
Show Gist options
  • Save hdp/130282 to your computer and use it in GitHub Desktop.
Save hdp/130282 to your computer and use it in GitHub Desktop.
package Class;
use Moose;
use Moose::Util::TypeConstraints;
## Whatever, you get the idea.
sub trim_whitespace { my $str = shift; $str =~ s/^\s+|\s+$//; $str };
sub boolify { my $str = shift; $str =~ /^y/i ? 1 : $str };
## Guess i need to say an OLBool is just a regular bool that coerces, stringifies, and removes padding... JOYZER. Shouldn't the where be at the very least, inherited it is a subtype.
subtype 'OLBool' => as 'Bool' => where { /1|0/ };
coerce 'OLBool'
=> from 'Str' => via { boolify( trim_whitespace($_) ) }
;
has 'foo' => ( isa => 'OLBool', is => 'rw',
coerce => 1, # HTH
);
my $c = Class->new;
printf ( "Should get %s\n", boolify(trim_whitespace('y')) );
$c->foo( 'y' ); ## EXCPETION
die $c->foo;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment