1Moose::Cookbook::Snack:U:sTeyrpeCso(n3t)ributed Perl DocMuomoesnet:a:tCiooonkbook::Snack::Types(3)
2
3
4

NAME

6       Moose::Cookbook::Snack::Types - Snippets of code for using Types and
7       Type Constraints
8

SYNOPSIS

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

DESCRIPTION

37       This is the Point example from (Moose::Cookbook::Recipe1) with added
38       type checking.
39
40       If we try to assign a string value to an attribute that is defined as
41       being of type Int, Moose will die with an explicit error message saying
42       which attribute failed which type constaint with which value. The eval
43       example catches this message and displays it.
44
45       The second example fetches the type constraint instance and asks it to
46       check the value we are about to set, before we try and set it.
47

SEE ALSO

49       Moose::Cookbook::Recipe1
50       Moose::Utils::TypeConstraints
51       Moose::Meta::Attribute
52

AUTHOR

54       Jess Robinson <cpan@desert-island.me.uk>
55
57       Copyright 2006, 2007 by Infinity Interactive, Inc.
58
59       <http://www.iinteractive.com>
60
61       This library is free software; you can redistribute it and/or modify it
62       under the same terms as Perl itself.
63
64
65
66perl v5.8.8                       2007-09-06  Moose::Cookbook::Snack::Types(3)
Impressum