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

VERSION

10       version 2.2013
11

SYNOPSIS

13         package Point;
14         use Moose;
15
16         has 'x' => ( isa => 'Int', is => 'ro' );
17         has 'y' => ( isa => 'Int', is => 'rw' );
18
19         package main;
20         use Try::Tiny;
21
22         my $point = try {
23             Point->new( x => 'fifty', y => 'forty' );
24         }
25         catch {
26             print "Oops: $_";
27         };
28
29         my $point;
30         my $xval             = 'forty-two';
31         my $xattribute       = Point->meta->find_attribute_by_name('x');
32         my $xtype_constraint = $xattribute->type_constraint;
33
34         if ( $xtype_constraint->check($xval) ) {
35             $point = Point->new( x => $xval, y => 0 );
36         }
37         else {
38             print "Value: $xval is not an " . $xtype_constraint->name . "\n";
39         }
40

DESCRIPTION

42       This is the Point example from
43       Moose::Cookbook::Basics::Point_AttributesAndSubclassing with type
44       checking added.
45
46       If we try to assign a string value to an attribute that is an "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.
50
51       We use Try::Tiny to catch this error message.
52
53       Later, we get the Moose::Meta::TypeConstraint object from a
54       Moose::Meta::Attribute and use the Moose::Meta::TypeConstraint to check
55       a value directly.
56

SEE ALSO

58       Moose::Cookbook::Basics::Point_AttributesAndSubclassing
59       Moose::Util::TypeConstraints
60       Moose::Meta::Attribute
61

AUTHORS

63       ·   Stevan Little <stevan.little@iinteractive.com>
64
65       ·   Dave Rolsky <autarch@urth.org>
66
67       ·   Jesse Luehrs <doy@tozt.net>
68
69       ·   Shawn M Moore <code@sartak.org>
70
71       ·   יובל קוג'מן (Yuval Kogman) <nothingmuch@woobling.org>
72
73       ·   Karen Etheridge <ether@cpan.org>
74
75       ·   Florian Ragwitz <rafl@debian.org>
76
77       ·   Hans Dieter Pearcey <hdp@weftsoar.net>
78
79       ·   Chris Prather <chris@prather.org>
80
81       ·   Matt S Trout <mst@shadowcat.co.uk>
82
84       This software is copyright (c) 2006 by Infinity Interactive, Inc.
85
86       This is free software; you can redistribute it and/or modify it under
87       the same terms as the Perl 5 programming language system itself.
88
89
90
91perl v5.32.0                      2020-07-28  Moose::Cookbook::Snack::Types(3)
Impressum