2 package Moose::Util::TypeConstraints;
7 use Sub::Name 'subname';
8 use Scalar::Util 'blessed';
10 our $VERSION = '0.01';
14 my $pkg = shift || caller();
15 return if $pkg eq ':no_export';
17 foreach my $export (qw(
20 *{"${pkg}::${export}"} = \&{"${export}"};
23 foreach my $constraint (qw(
27 ScalarRef ArrayRef HashRef CodeRef RegexpRef
30 *{"${pkg}::${constraint}"} = \&{"${constraint}"};
37 #sub find_type_constraint { $TYPES{$_[0]} }
40 my ($name, $check) = @_;
42 my $full_name = "${pkg}::${name}";
44 *{$full_name} = $TYPES{$name} = subname $full_name => sub {
45 return $TYPES{$name} unless defined $_[0];
47 return undef unless $check->($_[0]);
53 my ($name, $parent, $check) = @_;
56 my $full_name = "${pkg}::${name}";
58 $parent = $TYPES{$parent} unless $parent && ref($parent) eq 'CODE';
59 *{$full_name} = $TYPES{$name} = subname $full_name => sub {
60 return $TYPES{$name} unless defined $_[0];
62 return undef unless defined $parent->($_[0]) && $check->($_[0]);
67 ($parent, $check) = ($name, $parent);
68 $parent = $TYPES{$parent} unless $parent && ref($parent) eq 'CODE';
69 return subname((caller() . '::__anon_subtype__') => sub {
70 return $TYPES{$name} unless defined $_[0];
72 return undef unless defined $parent->($_[0]) && $check->($_[0]);
79 sub where (&) { $_[0] }
81 # define some basic types
83 type Any => where { 1 };
85 type Value => where { !ref($_) };
86 type Ref => where { ref($_) };
88 subtype Int => as Value => where { Scalar::Util::looks_like_number($_) };
89 subtype Str => as Value => where { !Scalar::Util::looks_like_number($_) };
91 subtype ScalarRef => as Ref => where { ref($_) eq 'SCALAR' };
92 subtype ArrayRef => as Ref => where { ref($_) eq 'ARRAY' };
93 subtype HashRef => as Ref => where { ref($_) eq 'HASH' };
94 subtype CodeRef => as Ref => where { ref($_) eq 'CODE' };
95 subtype RegexpRef => as Ref => where { ref($_) eq 'Regexp' };
98 # blessed(qr/.../) returns true,.. how odd
99 subtype Object => as Ref => where { blessed($_) && blessed($_) ne 'Regexp' };
109 Moose::Util::TypeConstraints - Type constraint system for Moose
113 use Moose::Util::TypeConstraints;
115 type Num => where { Scalar::Util::looks_like_number($_) };
121 subtype NaturalLessThanTen
123 => where { $_ < 10 };
127 This module provides Moose with the ability to create type contraints
128 to be are used in both attribute definitions and for method argument
131 This is B<NOT> a type system for Perl 5.
133 The type and subtype constraints are basically functions which will
134 validate their first argument. If called with no arguments, they will
135 return themselves (this is syntactic sugar for Moose attributes).
137 This module also provides a simple hierarchy for Perl 5 types, this
138 could probably use some work, but it works for me at the moment.
152 Suggestions for improvement are welcome.
156 =head2 Type Constraint Constructors
170 =head2 Built-in Type Constraints
200 All complex software has bugs lurking in it, and this module is no
201 exception. If you find a bug please either email me, or add the bug
206 Stevan Little E<lt>stevan@iinteractive.comE<gt>
208 =head1 COPYRIGHT AND LICENSE
210 Copyright 2006 by Infinity Interactive, Inc.
212 L<http://www.iinteractive.com>
214 This library is free software; you can redistribute it and/or modify
215 it under the same terms as Perl itself.