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 {
28 my $type_name = shift;
32 sub register_type_constraint {
33 my ($name, $parent, $constraint) = @_;
34 (not exists $TYPES{$name})
35 || confess "The type constraint '$name' has already been registered";
36 $parent = find_type_constraint($parent) if defined $parent;
37 $TYPES{$name} = Moose::Meta::TypeConstraint->new(
40 constraint => $constraint,
44 sub find_type_coercion {
45 my $type_name = shift;
46 $TYPES{$type_name}->coercion_code;
49 sub register_type_coercion {
50 my ($type_name, $type_coercion) = @_;
51 my $type = $TYPES{$type_name};
52 (!$type->has_coercion)
53 || confess "The type coercion for '$type_name' has already been registered";
54 $type->set_coercion_code($type_coercion);
57 sub export_type_contstraints_as_functions {
60 foreach my $constraint (keys %TYPES) {
61 *{"${pkg}::${constraint}"} = $TYPES{$constraint}->constraint_code;
68 my ($name, $check) = @_;
69 register_type_constraint($name, undef, $check);
74 my ($name, $parent, $check) = @_;
75 register_type_constraint($name, $parent, $check);
78 my ($parent, $check) = @_;
79 $parent = find_type_constraint($parent);
80 return Moose::Meta::TypeConstraint->new(
89 my ($type_name, @coercion_map) = @_;
91 while (@coercion_map) {
92 my ($constraint_name, $action) = splice(@coercion_map, 0, 2);
93 my $constraint = find_type_constraint($constraint_name)->constraint_code;
95 || confess "Could not find the type constraint ($constraint_name)";
96 push @coercions => [ $constraint, $action ];
98 register_type_coercion($type_name, sub {
100 foreach my $coercion (@coercions) {
101 my ($constraint, $converter) = @$coercion;
102 if (defined $constraint->($thing)) {
104 return $converter->($thing);
112 sub from ($) { $_[0] }
113 sub where (&) { $_[0] }
114 sub via (&) { $_[0] }
116 # define some basic types
118 type Any => where { 1 };
120 type Value => where { !ref($_) };
121 type Ref => where { ref($_) };
123 subtype Int => as Value => where { Scalar::Util::looks_like_number($_) };
124 subtype Str => as Value => where { !Scalar::Util::looks_like_number($_) };
126 subtype ScalarRef => as Ref => where { ref($_) eq 'SCALAR' };
127 subtype ArrayRef => as Ref => where { ref($_) eq 'ARRAY' };
128 subtype HashRef => as Ref => where { ref($_) eq 'HASH' };
129 subtype CodeRef => as Ref => where { ref($_) eq 'CODE' };
130 subtype RegexpRef => as Ref => where { ref($_) eq 'Regexp' };
133 # blessed(qr/.../) returns true,.. how odd
134 subtype Object => as Ref => where { blessed($_) && blessed($_) ne 'Regexp' };
144 Moose::Util::TypeConstraints - Type constraint system for Moose
148 use Moose::Util::TypeConstraints;
150 type Num => where { Scalar::Util::looks_like_number($_) };
156 subtype NaturalLessThanTen
158 => where { $_ < 10 };
166 This module provides Moose with the ability to create type contraints
167 to be are used in both attribute definitions and for method argument
170 This is B<NOT> a type system for Perl 5.
172 This module also provides a simple hierarchy for Perl 5 types, this
173 could probably use some work, but it works for me at the moment.
187 Suggestions for improvement are welcome.
191 =head2 Type Constraint Registry
195 =item B<find_type_constraint ($type_name)>
197 =item B<register_type_constraint ($type_name, $type_constraint)>
199 =item B<find_type_coercion>
201 =item B<register_type_coercion>
203 =item B<export_type_contstraints_as_functions>
205 =item B<dump_type_constraints>
209 =head2 Type Constraint Constructors
229 =head2 Built-in Type Constraints
259 All complex software has bugs lurking in it, and this module is no
260 exception. If you find a bug please either email me, or add the bug
265 Stevan Little E<lt>stevan@iinteractive.comE<gt>
267 =head1 COPYRIGHT AND LICENSE
269 Copyright 2006 by Infinity Interactive, Inc.
271 L<http://www.iinteractive.com>
273 This library is free software; you can redistribute it and/or modify
274 it under the same terms as Perl itself.