updating copyright dates
[gitmo/Moose.git] / lib / Moose / Cookbook / Snack / Types.pod
1
2 =pod
3
4 =head1 NAME
5
6 Moose::Cookbook::Snack::Types - Snippets of code for using Types and Type Constraints
7
8 =head1 SYNOPSIS
9
10   package Point;
11   use Moose;
12         
13   has 'x' => (isa => 'Int', is => 'ro' );
14   has 'y' => (isa => 'Int', is => 'rw' );
15   
16   package main;
17
18   my $point = eval {
19     Point->new(x => 'fifty', y => 'fourty');
20   };
21
22   if($@) {
23     print "Oops: $@";
24   }
25
26   my $point;
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);
32   } else {
33     print "Value: $xval is not an " . $xtype_constraint->name . "\n";
34   }
35
36
37 =head1 DESCRIPTION
38
39 This is the Point example from (L<Moose::Cookbook::Recipe1>) with added
40 type checking. 
41
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.
46
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.
49
50 =head1 SEE ALSO
51
52 =over 4
53
54 =item L<Moose::Cookbook::Recipe1>
55
56 =item L<Moose::Utils::TypeConstraints>
57
58 =item L<Moose::Meta::Attribute>
59
60 =back
61
62 =head1 AUTHOR
63
64 Jess Robinson <cpan@desert-island.me.uk>
65
66 =head1 COPYRIGHT AND LICENSE
67
68 Copyright 2006-2008 by Infinity Interactive, Inc.
69
70 L<http://www.iinteractive.com>
71
72 This library is free software; you can redistribute it and/or modify
73 it under the same terms as Perl itself.
74
75 =cut