1 package Moose::Cookbook::Snack::Types;
3 # ABSTRACT: Snippets of code for using Types and Type Constraints
15 has 'x' => ( isa => 'Int', is => 'ro' );
16 has 'y' => ( isa => 'Int', is => 'rw' );
22 Point->new( x => 'fifty', y => 'forty' );
29 my $xval = 'forty-two';
30 my $xattribute = Point->meta->find_attribute_by_name('x');
31 my $xtype_constraint = $xattribute->type_constraint;
33 if ( $xtype_constraint->check($xval) ) {
34 $point = Point->new( x => $xval, y => 0 );
37 print "Value: $xval is not an " . $xtype_constraint->name . "\n";
42 This is the Point example from
43 L<Moose::Cookbook::Basics::Point_AttributesAndSubclassing> with type checking
46 If we try to assign a string value to an attribute that is an C<Int>,
47 Moose will die with an explicit error message. The error will include
48 the attribute name, as well as the type constraint name and the value
49 which failed the constraint check.
51 We use L<Try::Tiny> to catch this error message.
53 Later, we get the L<Moose::Meta::TypeConstraint> object from a
54 L<Moose::Meta::Attribute> and use the L<Moose::Meta::TypeConstraint>
55 to check a value directly.
61 =item L<Moose::Cookbook::Basics::Point_AttributesAndSubclassing>
63 =item L<Moose::Utils::TypeConstraints>
65 =item L<Moose::Meta::Attribute>