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 ($type_name, $type_constraint) = @_;
34 (not exists $TYPES{$type_name})
35 || confess "The type constraint '$type_name' has already been registered";
36 $TYPES{$type_name} = Moose::Meta::TypeConstraint->new(
38 constraint_code => $type_constraint
42 sub export_type_contstraints_as_functions {
45 foreach my $constraint (keys %TYPES) {
46 *{"${pkg}::${constraint}"} = $TYPES{$constraint}->constraint_code;
50 sub find_type_coercion {
51 my $type_name = shift;
52 $TYPES{$type_name}->coercion_code;
55 sub register_type_coercion {
56 my ($type_name, $type_coercion) = @_;
57 my $type = $TYPES{$type_name};
58 (!$type->has_coercion)
59 || confess "The type coercion for '$type_name' has already been registered";
60 $type->set_coercion_code($type_coercion);
66 my ($name, $check) = @_;
67 my $full_name = caller() . "::${name}";
68 register_type_constraint($name => subname $full_name => sub {
70 return undef unless $check->($_[0]);
76 my ($name, $parent, $check) = @_;
78 my $full_name = caller() . "::${name}";
79 $parent = find_type_constraint($parent)->constraint_code
80 unless $parent && ref($parent) eq 'CODE';
81 register_type_constraint($name => subname $full_name => sub {
83 return undef unless defined $parent->($_[0]) && $check->($_[0]);
88 ($parent, $check) = ($name, $parent);
89 $parent = find_type_constraint($parent)->constraint_code
90 unless $parent && ref($parent) eq 'CODE';
91 return subname '__anon_subtype__' => sub {
93 return undef unless defined $parent->($_[0]) && $check->($_[0]);
100 my ($type_name, @coercion_map) = @_;
102 #warn Dumper \@coercion_map;
104 while (@coercion_map) {
105 my ($constraint_name, $action) = splice(@coercion_map, 0, 2);
106 my $constraint = find_type_constraint($constraint_name)->constraint_code;
107 (defined $constraint)
108 || confess "Could not find the type constraint ($constraint_name)";
109 push @coercions => [ $constraint, $action ];
111 register_type_coercion($type_name, sub {
113 foreach my $coercion (@coercions) {
114 my ($constraint, $converter) = @$coercion;
115 if (defined $constraint->($thing)) {
117 return $converter->($thing);
125 sub from ($) { $_[0] }
126 sub where (&) { $_[0] }
127 sub via (&) { $_[0] }
129 # define some basic types
131 type Any => where { 1 };
133 type Value => where { !ref($_) };
134 type Ref => where { ref($_) };
136 subtype Int => as Value => where { Scalar::Util::looks_like_number($_) };
137 subtype Str => as Value => where { !Scalar::Util::looks_like_number($_) };
139 subtype ScalarRef => as Ref => where { ref($_) eq 'SCALAR' };
140 subtype ArrayRef => as Ref => where { ref($_) eq 'ARRAY' };
141 subtype HashRef => as Ref => where { ref($_) eq 'HASH' };
142 subtype CodeRef => as Ref => where { ref($_) eq 'CODE' };
143 subtype RegexpRef => as Ref => where { ref($_) eq 'Regexp' };
146 # blessed(qr/.../) returns true,.. how odd
147 subtype Object => as Ref => where { blessed($_) && blessed($_) ne 'Regexp' };
157 Moose::Util::TypeConstraints - Type constraint system for Moose
161 use Moose::Util::TypeConstraints;
163 type Num => where { Scalar::Util::looks_like_number($_) };
169 subtype NaturalLessThanTen
171 => where { $_ < 10 };
179 This module provides Moose with the ability to create type contraints
180 to be are used in both attribute definitions and for method argument
183 This is B<NOT> a type system for Perl 5.
185 This module also provides a simple hierarchy for Perl 5 types, this
186 could probably use some work, but it works for me at the moment.
200 Suggestions for improvement are welcome.
204 =head2 Type Constraint Registry
208 =item B<find_type_constraint ($type_name)>
210 =item B<register_type_constraint ($type_name, $type_constraint)>
212 =item B<find_type_coercion>
214 =item B<register_type_coercion>
216 =item B<export_type_contstraints_as_functions>
218 =item B<dump_type_constraints>
222 =head2 Type Constraint Constructors
242 =head2 Built-in Type Constraints
272 All complex software has bugs lurking in it, and this module is no
273 exception. If you find a bug please either email me, or add the bug
278 Stevan Little E<lt>stevan@iinteractive.comE<gt>
280 =head1 COPYRIGHT AND LICENSE
282 Copyright 2006 by Infinity Interactive, Inc.
284 L<http://www.iinteractive.com>
286 This library is free software; you can redistribute it and/or modify
287 it under the same terms as Perl itself.