2 package Moose::Util::TypeConstraints;
8 use Sub::Name 'subname';
9 use Scalar::Util 'blessed';
11 our $VERSION = '0.02';
15 my $pkg = shift || caller();
16 return if $pkg eq ':no_export';
18 foreach my $export (qw(type subtype as where coerce from via)) {
19 *{"${pkg}::${export}"} = \&{"${export}"};
25 sub find_type_constraint {
26 my $type_name = shift;
30 sub register_type_constraint {
31 my ($type_name, $type_constraint) = @_;
32 (not exists $TYPES{$type_name})
33 || confess "The type constraint '$type_name' has already been registered";
34 $TYPES{$type_name} = $type_constraint;
37 sub dump_type_constraints {
39 $Data::Dumper::Deparse = 1;
40 Data::Dumper::Dumper(\%TYPES);
43 sub export_type_contstraints_as_functions {
46 foreach my $constraint (keys %TYPES) {
47 *{"${pkg}::${constraint}"} = $TYPES{$constraint};
54 sub find_type_coercion {
55 my $type_name = shift;
56 $COERCIONS{$type_name};
59 sub register_type_coercion {
60 my ($type_name, $type_coercion) = @_;
61 (not exists $COERCIONS{$type_name})
62 || confess "The type coercion for '$type_name' has already been registered";
63 $COERCIONS{$type_name} = $type_coercion;
69 my ($name, $check) = @_;
70 my $full_name = caller() . "::${name}";
71 register_type_constraint($name => subname $full_name => sub {
73 return undef unless $check->($_[0]);
79 my ($name, $parent, $check) = @_;
81 my $full_name = caller() . "::${name}";
82 $parent = find_type_constraint($parent)
83 unless $parent && ref($parent) eq 'CODE';
84 register_type_constraint($name => subname $full_name => sub {
86 return undef unless defined $parent->($_[0]) && $check->($_[0]);
91 ($parent, $check) = ($name, $parent);
92 $parent = find_type_constraint($parent)
93 unless $parent && ref($parent) eq 'CODE';
94 return subname '__anon_subtype__' => sub {
96 return undef unless defined $parent->($_[0]) && $check->($_[0]);
103 my ($type_name, @coercion_map) = @_;
105 #warn Dumper \@coercion_map;
107 while (@coercion_map) {
108 my ($constraint_name, $action) = splice(@coercion_map, 0, 2);
109 my $constraint = find_type_constraint($constraint_name);
110 (defined $constraint)
111 || confess "Could not find the type constraint ($constraint_name)";
112 push @coercions => [ $constraint, $action ];
114 register_type_coercion($type_name, sub {
116 foreach my $coercion (@coercions) {
117 my ($constraint, $converter) = @$coercion;
118 if (defined $constraint->($thing)) {
120 return $converter->($thing);
128 sub from ($) { $_[0] }
129 sub where (&) { $_[0] }
130 sub via (&) { $_[0] }
132 # define some basic types
134 type Any => where { 1 };
136 type Value => where { !ref($_) };
137 type Ref => where { ref($_) };
139 subtype Int => as Value => where { Scalar::Util::looks_like_number($_) };
140 subtype Str => as Value => where { !Scalar::Util::looks_like_number($_) };
142 subtype ScalarRef => as Ref => where { ref($_) eq 'SCALAR' };
143 subtype ArrayRef => as Ref => where { ref($_) eq 'ARRAY' };
144 subtype HashRef => as Ref => where { ref($_) eq 'HASH' };
145 subtype CodeRef => as Ref => where { ref($_) eq 'CODE' };
146 subtype RegexpRef => as Ref => where { ref($_) eq 'Regexp' };
149 # blessed(qr/.../) returns true,.. how odd
150 subtype Object => as Ref => where { blessed($_) && blessed($_) ne 'Regexp' };
160 Moose::Util::TypeConstraints - Type constraint system for Moose
164 use Moose::Util::TypeConstraints;
166 type Num => where { Scalar::Util::looks_like_number($_) };
172 subtype NaturalLessThanTen
174 => where { $_ < 10 };
182 This module provides Moose with the ability to create type contraints
183 to be are used in both attribute definitions and for method argument
186 This is B<NOT> a type system for Perl 5.
188 This module also provides a simple hierarchy for Perl 5 types, this
189 could probably use some work, but it works for me at the moment.
203 Suggestions for improvement are welcome.
207 =head2 Type Constraint Registry
211 =item B<find_type_constraint ($type_name)>
213 =item B<register_type_constraint ($type_name, $type_constraint)>
215 =item B<find_type_coercion>
217 =item B<register_type_coercion>
219 =item B<export_type_contstraints_as_functions>
221 =item B<dump_type_constraints>
225 =head2 Type Constraint Constructors
245 =head2 Built-in Type Constraints
275 All complex software has bugs lurking in it, and this module is no
276 exception. If you find a bug please either email me, or add the bug
281 Stevan Little E<lt>stevan@iinteractive.comE<gt>
283 =head1 COPYRIGHT AND LICENSE
285 Copyright 2006 by Infinity Interactive, Inc.
287 L<http://www.iinteractive.com>
289 This library is free software; you can redistribute it and/or modify
290 it under the same terms as Perl itself.