2 package Moose::Util::TypeConstraints;
8 use Sub::Name 'subname';
9 use Scalar::Util 'blessed';
11 our $VERSION = '0.02';
13 use Moose::Meta::TypeConstraint;
17 my $pkg = shift || caller();
18 return if $pkg eq ':no_export';
20 foreach my $export (qw(type subtype as where coerce from via)) {
21 *{"${pkg}::${export}"} = \&{"${export}"};
27 sub find_type_constraint { $TYPES{$_[0]} }
29 sub create_type_constraint {
30 my ($name, $parent, $constraint) = @_;
31 (not exists $TYPES{$name})
32 || confess "The type constraint '$name' has already been created";
33 $parent = find_type_constraint($parent) if defined $parent;
34 $TYPES{$name} = Moose::Meta::TypeConstraint->new(
37 constraint => $constraint,
41 sub find_type_coercion {
42 my $type_name = shift;
43 $TYPES{$type_name}->coercion_code;
46 sub register_type_coercion {
47 my ($type_name, $type_coercion) = @_;
48 my $type = $TYPES{$type_name};
49 (!$type->has_coercion)
50 || confess "The type coercion for '$type_name' has already been registered";
51 $type->set_coercion_code($type_coercion);
54 sub export_type_contstraints_as_functions {
57 foreach my $constraint (keys %TYPES) {
58 *{"${pkg}::${constraint}"} = $TYPES{$constraint}->constraint_code;
65 my ($name, $check) = @_;
66 create_type_constraint($name, undef, $check);
71 my ($name, $parent, $check) = @_;
72 create_type_constraint($name, $parent, $check);
75 my ($parent, $check) = @_;
76 $parent = find_type_constraint($parent);
77 return Moose::Meta::TypeConstraint->new(
86 my ($type_name, @coercion_map) = @_;
88 while (@coercion_map) {
89 my ($constraint_name, $action) = splice(@coercion_map, 0, 2);
90 my $constraint = find_type_constraint($constraint_name)->constraint_code;
92 || confess "Could not find the type constraint ($constraint_name)";
93 push @coercions => [ $constraint, $action ];
95 register_type_coercion($type_name, sub {
97 foreach my $coercion (@coercions) {
98 my ($constraint, $converter) = @$coercion;
99 if (defined $constraint->($thing)) {
101 return $converter->($thing);
109 sub from ($) { $_[0] }
110 sub where (&) { $_[0] }
111 sub via (&) { $_[0] }
113 # define some basic types
115 type Any => where { 1 };
117 type Value => where { !ref($_) };
118 type Ref => where { ref($_) };
120 subtype Int => as Value => where { Scalar::Util::looks_like_number($_) };
121 subtype Str => as Value => where { !Scalar::Util::looks_like_number($_) };
123 subtype ScalarRef => as Ref => where { ref($_) eq 'SCALAR' };
124 subtype ArrayRef => as Ref => where { ref($_) eq 'ARRAY' };
125 subtype HashRef => as Ref => where { ref($_) eq 'HASH' };
126 subtype CodeRef => as Ref => where { ref($_) eq 'CODE' };
127 subtype RegexpRef => as Ref => where { ref($_) eq 'Regexp' };
130 # blessed(qr/.../) returns true,.. how odd
131 subtype Object => as Ref => where { blessed($_) && blessed($_) ne 'Regexp' };
141 Moose::Util::TypeConstraints - Type constraint system for Moose
145 use Moose::Util::TypeConstraints;
147 type Num => where { Scalar::Util::looks_like_number($_) };
153 subtype NaturalLessThanTen
155 => where { $_ < 10 };
163 This module provides Moose with the ability to create type contraints
164 to be are used in both attribute definitions and for method argument
167 This is B<NOT> a type system for Perl 5.
169 This module also provides a simple hierarchy for Perl 5 types, this
170 could probably use some work, but it works for me at the moment.
184 Suggestions for improvement are welcome.
188 =head2 Type Constraint Registry
192 =item B<find_type_constraint ($type_name)>
194 =item B<create_type_constraint ($type_name, $type_constraint)>
196 =item B<find_type_coercion>
198 =item B<register_type_coercion>
200 =item B<export_type_contstraints_as_functions>
202 =item B<dump_type_constraints>
206 =head2 Type Constraint Constructors
226 =head2 Built-in Type Constraints
256 All complex software has bugs lurking in it, and this module is no
257 exception. If you find a bug please either email me, or add the bug
262 Stevan Little E<lt>stevan@iinteractive.comE<gt>
264 =head1 COPYRIGHT AND LICENSE
266 Copyright 2006 by Infinity Interactive, Inc.
268 L<http://www.iinteractive.com>
270 This library is free software; you can redistribute it and/or modify
271 it under the same terms as Perl itself.