1MooX::Types::MooseLike(U3s)er Contributed Perl DocumentatMiooonX::Types::MooseLike(3)
2
3
4
6 MooX::Types::MooseLike - some Moosish types and a type builder
7
9 package MyApp::Types;
10 use MooX::Types::MooseLike;
11 use base qw(Exporter);
12 our @EXPORT_OK = ();
13
14 # Define some types
15 my $defs = [{
16 name => 'MyType',
17 test => sub { predicate($_[0]) },
18 message => sub { "$_[0] is not the type we want!" }
19 },
20 {
21 name => 'VarChar',
22 test => sub {
23 my ($value, $param) = @_;
24 length($value) <= $param;
25 },
26 message => sub { "$_[0] is too large! It should be less than or equal to $_[1]." }
27 }];
28
29 # Make the types available - this adds them to @EXPORT_OK automatically.
30 MooX::Types::MooseLike::register_types($defs, __PACKAGE__);
31
32 ...
33
34 # Somewhere in code that uses the type
35 package MyApp::Foo;
36 use Moo;
37 use MyApp::Types qw(MyType VarChar);
38
39 has attribute => (
40 is => 'ro',
41 isa => MyType,
42 );
43
44 has string => (
45 is => 'ro',
46 isa => VarChar[25]
47 );
48
50 This module provides a possibility to build your own set of Moose-like
51 types. These custom types can then be used to describe fields in Moo-
52 based classes.
53
54 See MooX::Types::MooseLike::Base for a list of available base types.
55 Its source also provides an example of how to build base types, along
56 with both parameterizable and non-parameterizable.
57
59 register_types
60 register_types( types, package, moose_namespace )
61
62 Install the given types within the package. This makes the types
63 automatically exportable by adding them to @EXPORT_OK of the package.
64 Types are expected to be an array ref where every type is of the
65 following format:
66
67 {
68 name => 'MyType',
69 test => sub { check_the_value_somehow($_[0]) },
70 message => sub { "$_[0] is not the type we want!" },
71 subtype_of => 'SomeParentType', # Optional
72 from => 'Some::Parent::CoolTypes', # Optional
73 parameterizable => sub { ... }, # Optional
74 inflate => sub { ... }, # Optional
75 }
76
77 A type can be declared with a reference (subtype_of) to some previously
78 declared type. In this case the new type will inherit the behaviour of
79 the referenced type.
80
81 The referenced type can come either from the same package or from a
82 third party package:
83
84 MooX::Types::MooseLike::register_types([{
85 name => 'GreaterThan10',
86 subtype_of => 'Int',
87 from => 'MooX::Types::MooseLike::Base',
88 test => sub { $_[0] > 10 },
89 message => sub { 'not greater than 10' },
90 }], __PACKAGE__);
91
92 MooX::Types::MooseLike::register_types([{
93 name => 'Between10And20',
94 subtype_of => 'GreaterThan10',
95 from => __PACKAGE__,
96 test => sub { $_[0] < 20 },
97 message => sub { 'not an integer between 10 and 20' },
98 }], __PACKAGE__);
99
100 MooX::Types::MooseLike::register_types([{
101 name => 'Between10And30',
102 subtype_of => GreaterThan10(),
103 test => sub { $_[0] < 30 },
104 message => sub { 'not an integer between 10 and 30' },
105 }], __PACKAGE__);
106
107 exception_message
108 exception_message( value, part_of_the_exception_string )
109
110 Helper function to be used in a type definition:
111
112 {
113 ...
114 message => sub { return exception_message($_[0], 'a HashRef' },
115 ...
116 }
117
118 In the event of <value> mismatching the type constraints it produces
119 the message:
120
121 "<value> is not a HashRef!"
122
123 inflate_type
124 inflate_type( coderef )
125
126 Inflates the type to a Moose type. Requires Moose.
127
129 MooX::Types::MooseLike::Numeric - an example of building subtypes.
130
131 MooX::Types::SetObject - an example of building parameterized types.
132
133 MooX::Types::MooseLike::Email, MooX::Types::MooseLike::DateTime
134
135 Type::Tiny - another implementation of type constraints. Compatible
136 with Moo, Moose and Mouse.
137
139 mateu - Mateu X. Hunter (cpan:MATEU) <hunter@missoula.org>
140
142 mst - Matt S. Trout (cpan:MSTROUT) <mst@shadowcat.co.uk>
143
144 Mithaldu - Christian Walde (cpan:MITHALDU)
145 <walde.christian@googlemail.com>
146
147 Matt Phillips (cpan:MATTP) <mattp@cpan.org>
148
149 Arthur Axel fREW Schmidt (cpan:FREW) <frioux@gmail.com>
150
151 Toby Inkster (cpan:TOBYINK) <tobyink@cpan.org>
152
153 Graham Knop (cpan:HAARG) <haarg@cpan.org>
154
155 Dmitry Matrosov (cpan:AMIDOS) <amidos@amidos.ru>
156
158 Copyright (c) 2011-2015 the MooX::Types::MooseLike "AUTHOR" and
159 "CONTRIBUTORS" as listed above.
160
162 This library is free software and may be distributed under the same
163 terms as perl itself.
164
165
166
167perl v5.36.0 2022-07-22 MooX::Types::MooseLike(3)