2 package Moose::Util::TypeConstraints;
7 use Sub::Name 'subname';
8 use Scalar::Util 'blessed';
10 our $VERSION = '0.02';
14 my $pkg = shift || caller();
15 return if $pkg eq ':no_export';
17 foreach my $export (qw(type subtype coerce as where to)) {
18 *{"${pkg}::${export}"} = \&{"${export}"};
24 sub find_type_constraint {
25 my $type_name = shift;
29 sub register_type_constraint {
30 my ($type_name, $type_constraint) = @_;
31 $TYPES{$type_name} = $type_constraint;
34 sub export_type_contstraints_as_functions {
37 foreach my $constraint (keys %TYPES) {
38 *{"${pkg}::${constraint}"} = $TYPES{$constraint};
45 sub find_type_coercion {
46 my $type_name = shift;
47 $COERCIONS{$type_name};
50 sub register_type_coercion {
51 my ($type_name, $type_coercion) = @_;
52 $COERCIONS{$type_name} = $type_coercion;
58 my ($name, $check) = @_;
59 my $full_name = caller() . "::${name}";
60 register_type_constraint($name => subname $full_name => sub {
61 return find_type_constraint($name) unless defined $_[0];
63 return undef unless $check->($_[0]);
69 my ($name, $parent, $check) = @_;
71 my $full_name = caller() . "::${name}";
72 $parent = find_type_constraint($parent)
73 unless $parent && ref($parent) eq 'CODE';
74 register_type_constraint($name => subname $full_name => sub {
75 return find_type_constraint($name) unless defined $_[0];
77 return undef unless defined $parent->($_[0]) && $check->($_[0]);
82 ($parent, $check) = ($name, $parent);
83 $parent = find_type_constraint($parent)
84 unless $parent && ref($parent) eq 'CODE';
85 return subname '__anon_subtype__' => sub {
87 return undef unless defined $parent->($_[0]) && $check->($_[0]);
94 my ($type_name, %coercion_map) = @_;
95 register_type_coercion($type_name, sub {
101 sub where (&) { $_[0] }
104 # define some basic types
106 type Any => where { 1 };
108 type Value => where { !ref($_) };
109 type Ref => where { ref($_) };
111 subtype Int => as Value => where { Scalar::Util::looks_like_number($_) };
112 subtype Str => as Value => where { !Scalar::Util::looks_like_number($_) };
114 subtype ScalarRef => as Ref => where { ref($_) eq 'SCALAR' };
115 subtype ArrayRef => as Ref => where { ref($_) eq 'ARRAY' };
116 subtype HashRef => as Ref => where { ref($_) eq 'HASH' };
117 subtype CodeRef => as Ref => where { ref($_) eq 'CODE' };
118 subtype RegexpRef => as Ref => where { ref($_) eq 'Regexp' };
121 # blessed(qr/.../) returns true,.. how odd
122 subtype Object => as Ref => where { blessed($_) && blessed($_) ne 'Regexp' };
132 Moose::Util::TypeConstraints - Type constraint system for Moose
136 use Moose::Util::TypeConstraints;
138 type Num => where { Scalar::Util::looks_like_number($_) };
144 subtype NaturalLessThanTen
146 => where { $_ < 10 };
150 This module provides Moose with the ability to create type contraints
151 to be are used in both attribute definitions and for method argument
154 This is B<NOT> a type system for Perl 5.
156 The type and subtype constraints are basically functions which will
157 validate their first argument. If called with no arguments, they will
158 return themselves (this is syntactic sugar for Moose attributes).
160 This module also provides a simple hierarchy for Perl 5 types, this
161 could probably use some work, but it works for me at the moment.
175 Suggestions for improvement are welcome.
179 =head2 Type Constraint Registry
183 =item B<find_type_constraint ($type_name)>
185 =item B<register_type_constraint ($type_name, $type_constraint)>
187 =item B<find_type_coercion>
189 =item B<register_type_coercion>
191 =item B<export_type_contstraints_as_functions>
195 =head2 Type Constraint Constructors
213 =head2 Built-in Type Constraints
243 All complex software has bugs lurking in it, and this module is no
244 exception. If you find a bug please either email me, or add the bug
249 Stevan Little E<lt>stevan@iinteractive.comE<gt>
251 =head1 COPYRIGHT AND LICENSE
253 Copyright 2006 by Infinity Interactive, Inc.
255 L<http://www.iinteractive.com>
257 This library is free software; you can redistribute it and/or modify
258 it under the same terms as Perl itself.