2 package Moose::Meta::TypeConstraint;
8 use Sub::Name 'subname';
11 our $VERSION = '0.01';
13 __PACKAGE__->meta->add_attribute('name' => (reader => 'name' ));
14 __PACKAGE__->meta->add_attribute('parent' => (reader => 'parent' ));
15 __PACKAGE__->meta->add_attribute('constraint' => (reader => 'constraint'));
16 __PACKAGE__->meta->add_attribute('coercion' => (
17 accessor => 'coercion',
18 predicate => 'has_coercion'
22 __PACKAGE__->meta->add_attribute('compiled_type_constraint' => (
23 accessor => '_compiled_type_constraint'
28 my $self = $class->meta->new_object(@_);
29 $self->compile_type_constraint();
33 sub compile_type_constraint () {
35 my $check = $self->constraint;
37 || confess "Could not compile type constraint '" . $self->name . "' because no constraint check";
38 my $parent = $self->parent;
39 if (defined $parent) {
40 # we have a subtype ...
41 $parent = $parent->_compiled_type_constraint;
42 $self->_compiled_type_constraint(subname $self->name => sub {
44 return undef unless defined $parent->($_[0]) && $check->($_[0]);
50 $self->_compiled_type_constraint(subname $self->name => sub {
52 return undef unless $check->($_[0]);
58 sub check { $_[0]->_compiled_type_constraint->($_[1]) }
68 Moose::Meta::TypeConstraint - The Moose Type Constraint metaclass
72 For the most part, the only time you will ever encounter an
73 instance of this class is if you are doing some serious deep
74 introspection. This API should not be considered final, but
75 it is B<highly unlikely> that this will matter to a regular
78 If you wish to use features at this depth, please come to the
79 #moose IRC channel on irc.perl.org and we can talk :)
89 =item B<compile_type_constraint>
107 All complex software has bugs lurking in it, and this module is no
108 exception. If you find a bug please either email me, or add the bug
113 Stevan Little E<lt>stevan@iinteractive.comE<gt>
115 =head1 COPYRIGHT AND LICENSE
117 Copyright 2006 by Infinity Interactive, Inc.
119 L<http://www.iinteractive.com>
121 This library is free software; you can redistribute it and/or modify
122 it under the same terms as Perl itself.