2 package Moose::Util::TypeConstraints;
8 use Scalar::Util 'blessed';
10 our $VERSION = '0.02';
12 use Moose::Meta::TypeConstraint;
13 use Moose::Meta::TypeCoercion;
17 my $pkg = shift || caller();
19 foreach my $export (qw(type subtype as where coerce from via find_type_constraint)) {
20 *{"${pkg}::${export}"} = \&{"${export}"};
26 sub find_type_constraint { $TYPES{$_[0]} }
28 sub _create_type_constraint {
29 my ($name, $parent, $check) = @_;
30 (!exists $TYPES{$name})
31 || confess "The type constraint '$name' has already been created"
33 $parent = $TYPES{$parent} if defined $parent;
34 my $constraint = Moose::Meta::TypeConstraint->new(
35 name => $name || '__ANON__',
39 $TYPES{$name} = $constraint if defined $name;
43 sub _install_type_coercions {
44 my ($type_name, $coercion_map) = @_;
45 my $type = $TYPES{$type_name};
46 (!$type->has_coercion)
47 || confess "The type coercion for '$type_name' has already been registered";
48 my $type_coercion = Moose::Meta::TypeCoercion->new(
49 type_coercion_map => $coercion_map,
50 type_constraint => $type
52 $type->coercion($type_coercion);
55 sub export_type_contstraints_as_functions {
58 foreach my $constraint (keys %TYPES) {
59 *{"${pkg}::${constraint}"} = $TYPES{$constraint}->_compiled_type_constraint;
67 my ($name, $check) = @_;
68 _create_type_constraint($name, undef, $check);
72 unshift @_ => undef if scalar @_ == 2;
73 _create_type_constraint(@_);
77 my ($type_name, @coercion_map) = @_;
78 _install_type_coercions($type_name, \@coercion_map);
82 sub from ($) { $_[0] }
83 sub where (&) { $_[0] }
86 # define some basic types
88 type Any => where { 1 };
90 type Value => where { !ref($_) };
91 type Ref => where { ref($_) };
93 subtype Int => as Value => where { Scalar::Util::looks_like_number($_) };
94 subtype Str => as Value => where { !Scalar::Util::looks_like_number($_) };
96 subtype ScalarRef => as Ref => where { ref($_) eq 'SCALAR' };
97 subtype ArrayRef => as Ref => where { ref($_) eq 'ARRAY' };
98 subtype HashRef => as Ref => where { ref($_) eq 'HASH' };
99 subtype CodeRef => as Ref => where { ref($_) eq 'CODE' };
100 subtype RegexpRef => as Ref => where { ref($_) eq 'Regexp' };
103 # blessed(qr/.../) returns true,.. how odd
104 subtype Object => as Ref => where { blessed($_) && blessed($_) ne 'Regexp' };
114 Moose::Util::TypeConstraints - Type constraint system for Moose
118 use Moose::Util::TypeConstraints;
120 type Num => where { Scalar::Util::looks_like_number($_) };
126 subtype NaturalLessThanTen
128 => where { $_ < 10 };
136 This module provides Moose with the ability to create type contraints
137 to be are used in both attribute definitions and for method argument
140 This is B<NOT> a type system for Perl 5.
142 This module also provides a simple hierarchy for Perl 5 types, this
143 could probably use some work, but it works for me at the moment.
157 Suggestions for improvement are welcome.
161 =head2 Type Constraint Registry
165 =item B<find_type_constraint ($type_name)>
167 =item B<_create_type_constraint ($type_name, $type_constraint)>
169 =item B<_install_type_coercions>
171 =item B<export_type_contstraints_as_functions>
175 =head2 Type Constraint Constructors
195 =head2 Built-in Type Constraints
225 All complex software has bugs lurking in it, and this module is no
226 exception. If you find a bug please either email me, or add the bug
231 Stevan Little E<lt>stevan@iinteractive.comE<gt>
233 =head1 COPYRIGHT AND LICENSE
235 Copyright 2006 by Infinity Interactive, Inc.
237 L<http://www.iinteractive.com>
239 This library is free software; you can redistribute it and/or modify
240 it under the same terms as Perl itself.