Commit | Line | Data |
a15dff8d |
1 | |
2 | package Moose::Util::TypeConstraints; |
3 | |
4 | use strict; |
5 | use warnings; |
6 | |
e90c03d0 |
7 | use Carp 'confess'; |
a15dff8d |
8 | use Scalar::Util 'blessed'; |
9 | |
2c0cbef7 |
10 | our $VERSION = '0.07'; |
a15dff8d |
11 | |
4e036ee4 |
12 | use Moose::Meta::TypeConstraint; |
2ca63f5d |
13 | use Moose::Meta::TypeCoercion; |
4e036ee4 |
14 | |
9e93dd19 |
15 | use Sub::Exporter -setup => { |
16 | exports => [qw/ |
17 | type subtype as where message |
18 | coerce from via |
19 | enum |
20 | find_type_constraint |
21 | /], |
22 | groups => { |
23 | default => [':all'] |
2c0cbef7 |
24 | } |
9e93dd19 |
25 | }; |
a15dff8d |
26 | |
182134e8 |
27 | { |
28 | my %TYPES; |
2c0cbef7 |
29 | sub find_type_constraint ($) { |
446e850f |
30 | return $TYPES{$_[0]}->[1] |
31 | if exists $TYPES{$_[0]}; |
32 | return; |
33 | } |
34 | |
35 | sub _dump_type_constraints { |
36 | require Data::Dumper; |
256903b6 |
37 | Data::Dumper::Dumper(\%TYPES); |
446e850f |
38 | } |
39 | |
2c0cbef7 |
40 | sub _create_type_constraint ($$$;$) { |
76d37e5a |
41 | my ($name, $parent, $check, $message) = @_; |
0e6614c3 |
42 | my $pkg_defined_in = scalar(caller(1)); |
43 | ($TYPES{$name}->[0] eq $pkg_defined_in) |
446e850f |
44 | || confess "The type constraint '$name' has already been created " |
0e6614c3 |
45 | if defined $name && exists $TYPES{$name}; |
46 | $parent = find_type_constraint($parent) if defined $parent; |
a27aa600 |
47 | my $constraint = Moose::Meta::TypeConstraint->new( |
48 | name => $name || '__ANON__', |
66811d63 |
49 | parent => $parent, |
76d37e5a |
50 | constraint => $check, |
51 | message => $message, |
4e036ee4 |
52 | ); |
0e6614c3 |
53 | $TYPES{$name} = [ $pkg_defined_in, $constraint ] if defined $name; |
a27aa600 |
54 | return $constraint; |
182134e8 |
55 | } |
182134e8 |
56 | |
2c0cbef7 |
57 | sub _install_type_coercions ($$) { |
a27aa600 |
58 | my ($type_name, $coercion_map) = @_; |
0e6614c3 |
59 | my $type = find_type_constraint($type_name); |
4e036ee4 |
60 | (!$type->has_coercion) |
d46a48f3 |
61 | || confess "The type coercion for '$type_name' has already been registered"; |
a27aa600 |
62 | my $type_coercion = Moose::Meta::TypeCoercion->new( |
63 | type_coercion_map => $coercion_map, |
64 | type_constraint => $type |
65 | ); |
66 | $type->coercion($type_coercion); |
182134e8 |
67 | } |
66811d63 |
68 | |
2c0cbef7 |
69 | sub create_type_constraint_union (@) { |
c07af9d2 |
70 | my (@type_constraint_names) = @_; |
71 | return Moose::Meta::TypeConstraint->union( |
72 | map { |
73 | find_type_constraint($_) |
74 | } @type_constraint_names |
75 | ); |
76 | } |
77 | |
66811d63 |
78 | sub export_type_contstraints_as_functions { |
79 | my $pkg = caller(); |
80 | no strict 'refs'; |
81 | foreach my $constraint (keys %TYPES) { |
0e6614c3 |
82 | *{"${pkg}::${constraint}"} = find_type_constraint($constraint)->_compiled_type_constraint; |
66811d63 |
83 | } |
84 | } |
182134e8 |
85 | } |
a15dff8d |
86 | |
7c13858b |
87 | # type constructors |
a15dff8d |
88 | |
89 | sub type ($$) { |
90 | my ($name, $check) = @_; |
7c13858b |
91 | _create_type_constraint($name, undef, $check); |
a15dff8d |
92 | } |
93 | |
76d37e5a |
94 | sub subtype ($$;$$) { |
95 | unshift @_ => undef if scalar @_ <= 2; |
2c0cbef7 |
96 | goto &_create_type_constraint; |
a15dff8d |
97 | } |
98 | |
4b598ea3 |
99 | sub coerce ($@) { |
66811d63 |
100 | my ($type_name, @coercion_map) = @_; |
7c13858b |
101 | _install_type_coercions($type_name, \@coercion_map); |
182134e8 |
102 | } |
103 | |
76d37e5a |
104 | sub as ($) { $_[0] } |
105 | sub from ($) { $_[0] } |
106 | sub where (&) { $_[0] } |
107 | sub via (&) { $_[0] } |
108 | sub message (&) { $_[0] } |
a15dff8d |
109 | |
2c0cbef7 |
110 | sub enum ($;@) { |
fcec2383 |
111 | my ($type_name, @values) = @_; |
2c0cbef7 |
112 | (scalar @values >= 2) |
113 | || confess "You must have at least two values to enumerate through"; |
fcec2383 |
114 | my $regexp = join '|' => @values; |
115 | _create_type_constraint( |
116 | $type_name, |
117 | 'Str', |
118 | sub { qr/^$regexp$/i } |
119 | ); |
120 | } |
121 | |
a15dff8d |
122 | # define some basic types |
123 | |
f65cb534 |
124 | type 'Any' => where { 1 }; # meta-type including all |
125 | type 'Item' => where { 1 }; # base-type |
a15dff8d |
126 | |
f65cb534 |
127 | subtype 'Undef' => as 'Item' => where { !defined($_) }; |
128 | subtype 'Defined' => as 'Item' => where { defined($_) }; |
a15dff8d |
129 | |
81dc201f |
130 | subtype 'Bool' => as 'Item' => where { !defined($_) || $_ eq "" || "$_" eq '1' || "$_" eq '0' }; |
5204cd52 |
131 | |
5a4c5493 |
132 | subtype 'Value' => as 'Defined' => where { !ref($_) }; |
133 | subtype 'Ref' => as 'Defined' => where { ref($_) }; |
134 | |
135 | subtype 'Str' => as 'Value' => where { 1 }; |
a15dff8d |
136 | |
81dc201f |
137 | subtype 'Num' => as 'Value' => where { Scalar::Util::looks_like_number($_) }; |
d4634ca2 |
138 | subtype 'Int' => as 'Num' => where { "$_" =~ /^-?[0-9]+$/ }; |
81dc201f |
139 | |
140 | subtype 'ScalarRef' => as 'Ref' => where { ref($_) eq 'SCALAR' }; |
451c8248 |
141 | subtype 'ArrayRef' => as 'Ref' => where { ref($_) eq 'ARRAY' }; |
142 | subtype 'HashRef' => as 'Ref' => where { ref($_) eq 'HASH' }; |
e9ec68d6 |
143 | subtype 'CodeRef' => as 'Ref' => where { ref($_) eq 'CODE' }; |
144 | subtype 'RegexpRef' => as 'Ref' => where { ref($_) eq 'Regexp' }; |
a15dff8d |
145 | |
146 | # NOTE: |
147 | # blessed(qr/.../) returns true,.. how odd |
e9ec68d6 |
148 | subtype 'Object' => as 'Ref' => where { blessed($_) && blessed($_) ne 'Regexp' }; |
a15dff8d |
149 | |
02a0fb52 |
150 | subtype 'Role' => as 'Object' => where { $_->can('does') }; |
151 | |
a15dff8d |
152 | 1; |
153 | |
154 | __END__ |
155 | |
156 | =pod |
157 | |
158 | =head1 NAME |
159 | |
e522431d |
160 | Moose::Util::TypeConstraints - Type constraint system for Moose |
a15dff8d |
161 | |
162 | =head1 SYNOPSIS |
163 | |
164 | use Moose::Util::TypeConstraints; |
165 | |
2c0cbef7 |
166 | type 'Num' => where { Scalar::Util::looks_like_number($_) }; |
a15dff8d |
167 | |
2c0cbef7 |
168 | subtype 'Natural' |
169 | => as 'Num' |
a15dff8d |
170 | => where { $_ > 0 }; |
171 | |
2c0cbef7 |
172 | subtype 'NaturalLessThanTen' |
173 | => as 'Natural' |
79592a54 |
174 | => where { $_ < 10 } |
175 | => message { "This number ($_) is not less than ten!" }; |
6b8bd8d3 |
176 | |
2c0cbef7 |
177 | coerce 'Num' |
178 | => from 'Str' |
d6e2d9a1 |
179 | => via { 0+$_ }; |
98aae381 |
180 | |
2c0cbef7 |
181 | enum 'RGBColors' => qw(red green blue); |
a15dff8d |
182 | |
183 | =head1 DESCRIPTION |
184 | |
e522431d |
185 | This module provides Moose with the ability to create type contraints |
186 | to be are used in both attribute definitions and for method argument |
187 | validation. |
188 | |
6ba6d68c |
189 | =head2 Important Caveat |
190 | |
191 | This is B<NOT> a type system for Perl 5. These are type constraints, |
192 | and they are not used by Moose unless you tell it to. No type |
193 | inference is performed, expression are not typed, etc. etc. etc. |
194 | |
195 | This is simply a means of creating small constraint functions which |
a7d0cd00 |
196 | can be used to simplify your own type-checking code. |
6ba6d68c |
197 | |
2c0cbef7 |
198 | =head2 Slightly Less Important Caveat |
199 | |
200 | It is almost always a good idea to quote your type and subtype names. |
201 | This is to prevent perl from trying to create the call as an indirect |
202 | object call. This issue only seems to come up when you have a subtype |
203 | the same name as a valid class, but when the issue does arise it tends |
204 | to be quite annoying to debug. |
205 | |
206 | So for instance, this: |
207 | |
208 | subtype DateTime => as Object => where { $_->isa('DateTime') }; |
209 | |
210 | will I<Just Work>, while this: |
211 | |
212 | use DateTime; |
213 | subtype DateTime => as Object => where { $_->isa('DateTime') }; |
214 | |
215 | will fail silently and cause many headaches. The simple way to solve |
216 | this, as well as future proof your subtypes from classes which have |
217 | yet to have been created yet, is to simply do this: |
218 | |
219 | use DateTime; |
220 | subtype 'DateTime' => as Object => where { $_->isa('DateTime') }; |
221 | |
6ba6d68c |
222 | =head2 Default Type Constraints |
e522431d |
223 | |
e522431d |
224 | This module also provides a simple hierarchy for Perl 5 types, this |
225 | could probably use some work, but it works for me at the moment. |
226 | |
227 | Any |
f65cb534 |
228 | Item |
5a4c5493 |
229 | Bool |
f65cb534 |
230 | Undef |
231 | Defined |
5a4c5493 |
232 | Value |
233 | Num |
234 | Int |
235 | Str |
236 | Ref |
237 | ScalarRef |
451c8248 |
238 | ArrayRef |
239 | HashRef |
5a4c5493 |
240 | CodeRef |
241 | RegexpRef |
242 | Object |
243 | Role |
e522431d |
244 | |
6ba6d68c |
245 | Suggestions for improvement are welcome. |
2c0cbef7 |
246 | |
247 | B<NOTE:> The C<Undef> type constraint does not work correctly |
248 | in every occasion, please use it sparringly. |
e522431d |
249 | |
a15dff8d |
250 | =head1 FUNCTIONS |
251 | |
182134e8 |
252 | =head2 Type Constraint Registry |
253 | |
254 | =over 4 |
255 | |
256 | =item B<find_type_constraint ($type_name)> |
257 | |
6ba6d68c |
258 | This function can be used to locate a specific type constraint |
259 | meta-object. What you do with it from there is up to you :) |
182134e8 |
260 | |
c07af9d2 |
261 | =item B<create_type_constraint_union (@type_constraint_names)> |
262 | |
263 | Given a list of C<@type_constraint_names>, this will return a |
264 | B<Moose::Meta::TypeConstraint::Union> instance. |
265 | |
182134e8 |
266 | =item B<export_type_contstraints_as_functions> |
267 | |
6ba6d68c |
268 | This will export all the current type constraints as functions |
269 | into the caller's namespace. Right now, this is mostly used for |
270 | testing, but it might prove useful to others. |
271 | |
182134e8 |
272 | =back |
273 | |
a15dff8d |
274 | =head2 Type Constraint Constructors |
275 | |
6ba6d68c |
276 | The following functions are used to create type constraints. |
277 | They will then register the type constraints in a global store |
278 | where Moose can get to them if it needs to. |
a15dff8d |
279 | |
6ba6d68c |
280 | See the L<SYNOPOSIS> for an example of how to use these. |
a15dff8d |
281 | |
6ba6d68c |
282 | =over 4 |
a15dff8d |
283 | |
6ba6d68c |
284 | =item B<type ($name, $where_clause)> |
a15dff8d |
285 | |
6ba6d68c |
286 | This creates a base type, which has no parent. |
a15dff8d |
287 | |
79592a54 |
288 | =item B<subtype ($name, $parent, $where_clause, ?$message)> |
182134e8 |
289 | |
6ba6d68c |
290 | This creates a named subtype. |
d6e2d9a1 |
291 | |
79592a54 |
292 | =item B<subtype ($parent, $where_clause, ?$message)> |
182134e8 |
293 | |
6ba6d68c |
294 | This creates an unnamed subtype and will return the type |
295 | constraint meta-object, which will be an instance of |
296 | L<Moose::Meta::TypeConstraint>. |
a15dff8d |
297 | |
fcec2383 |
298 | =item B<enum ($name, @values)> |
299 | |
2c0cbef7 |
300 | This will create a basic subtype for a given set of strings. |
301 | The resulting constraint will be a subtype of C<Str> and |
302 | will match any of the items in C<@values>. See the L<SYNOPSIS> |
303 | for a simple example. |
304 | |
305 | B<NOTE:> This is not a true proper enum type, it is simple |
306 | a convient constraint builder. |
307 | |
6ba6d68c |
308 | =item B<as> |
a15dff8d |
309 | |
6ba6d68c |
310 | This is just sugar for the type constraint construction syntax. |
a15dff8d |
311 | |
6ba6d68c |
312 | =item B<where> |
a15dff8d |
313 | |
6ba6d68c |
314 | This is just sugar for the type constraint construction syntax. |
76d37e5a |
315 | |
316 | =item B<message> |
317 | |
318 | This is just sugar for the type constraint construction syntax. |
a15dff8d |
319 | |
6ba6d68c |
320 | =back |
a15dff8d |
321 | |
6ba6d68c |
322 | =head2 Type Coercion Constructors |
a15dff8d |
323 | |
6ba6d68c |
324 | Type constraints can also contain type coercions as well. In most |
325 | cases Moose will run the type-coercion code first, followed by the |
326 | type constraint check. This feature should be used carefully as it |
327 | is very powerful and could easily take off a limb if you are not |
328 | careful. |
a15dff8d |
329 | |
6ba6d68c |
330 | See the L<SYNOPOSIS> for an example of how to use these. |
a15dff8d |
331 | |
6ba6d68c |
332 | =over 4 |
a15dff8d |
333 | |
6ba6d68c |
334 | =item B<coerce> |
a15dff8d |
335 | |
6ba6d68c |
336 | =item B<from> |
a15dff8d |
337 | |
6ba6d68c |
338 | This is just sugar for the type coercion construction syntax. |
339 | |
340 | =item B<via> |
a15dff8d |
341 | |
6ba6d68c |
342 | This is just sugar for the type coercion construction syntax. |
a15dff8d |
343 | |
344 | =back |
345 | |
346 | =head1 BUGS |
347 | |
348 | All complex software has bugs lurking in it, and this module is no |
349 | exception. If you find a bug please either email me, or add the bug |
350 | to cpan-RT. |
351 | |
a15dff8d |
352 | =head1 AUTHOR |
353 | |
354 | Stevan Little E<lt>stevan@iinteractive.comE<gt> |
355 | |
356 | =head1 COPYRIGHT AND LICENSE |
357 | |
358 | Copyright 2006 by Infinity Interactive, Inc. |
359 | |
360 | L<http://www.iinteractive.com> |
361 | |
362 | This library is free software; you can redistribute it and/or modify |
363 | it under the same terms as Perl itself. |
364 | |
81dc201f |
365 | =cut |