Fix all references to cookbook recipes
[gitmo/Moose.git] / lib / Moose / Cookbook / Snack / Types.pod
CommitLineData
bcbaa845 1
2=pod
3
4=head1 NAME
5
6Moose::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
5cfe3805 39This is the Point example from (L<Moose::Cookbook::Basics::Recipe1>) with added
bcbaa845 40type checking.
41
42If we try to assign a string value to an attribute that is defined as
43being of type Int, Moose will die with an explicit error message
44saying which attribute failed which type constaint with which
45value. The eval example catches this message and displays it.
46
47The second example fetches the type constraint instance and asks it to
48check the value we are about to set, before we try and set it.
49
50=head1 SEE ALSO
51
52=over 4
53
5cfe3805 54=item L<Moose::Cookbook::Basics::Recipe1>
bcbaa845 55
56=item L<Moose::Utils::TypeConstraints>
57
58=item L<Moose::Meta::Attribute>
59
60=back
61
62=head1 AUTHOR
63
64Jess Robinson <cpan@desert-island.me.uk>
65
66=head1 COPYRIGHT AND LICENSE
67
778db3ac 68Copyright 2006-2008 by Infinity Interactive, Inc.
bcbaa845 69
70L<http://www.iinteractive.com>
71
72This library is free software; you can redistribute it and/or modify
73it under the same terms as Perl itself.
74
5cfe3805 75=cut