6 Moose::Cookbook::Snack::Types - Snippets of code for using Types and Type Constraints
13 has 'x' => (isa => 'Int', is => 'ro' );
14 has 'y' => (isa => 'Int', is => 'rw' );
19 Point->new(x => 'fifty', y => 'fourty');
27 my $xval = 'fourty-two';
28 my $xattribute = Point->meta->find_attribute_by_name('x');
29 my $xtype_constraint = $xattribute->type_constraint;
30 if($xtype_constraint->check($xval)) {
31 $point = Point->new(x => $xval, y => 0);
33 print "Value: $xval is not an " . $xtype_constraint->name . "\n";
39 This is the Point example from (L<Moose::Cookbook::Basics::Recipe1>) with added
42 If we try to assign a string value to an attribute that is defined as
43 being of type Int, Moose will die with an explicit error message
44 saying which attribute failed which type constaint with which
45 value. The eval example catches this message and displays it.
47 The second example fetches the type constraint instance and asks it to
48 check the value we are about to set, before we try and set it.
54 =item L<Moose::Cookbook::Basics::Recipe1>
56 =item L<Moose::Utils::TypeConstraints>
58 =item L<Moose::Meta::Attribute>
64 Jess Robinson <cpan@desert-island.me.uk>
66 =head1 COPYRIGHT AND LICENSE
68 Copyright 2006-2008 by Infinity Interactive, Inc.
70 L<http://www.iinteractive.com>
72 This library is free software; you can redistribute it and/or modify
73 it under the same terms as Perl itself.