1 package MooseX::Types::Dependent;
5 use Moose::Util::TypeConstraints;
6 use MooseX::Meta::TypeConstraint::Dependent;
7 use MooseX::Types -declare => [qw(Depending)];
10 our $AUTHORITY = 'cpan:JJNAPIORK';
14 MooseX::Types::Dependent - L<MooseX::Types> constraints that depend on values.
22 shift->exists(shift) ? 0:1;
29 shift->exists(shift) ? 0:1;
32 Please see the test cases for more examples.
36 The following is a list of terms used in this documentation.
38 =head2 Dependent Type Constraint
42 =head2 Constraining Type Constraint
44 =head2 Constraining Value
48 A dependent type is a type constraint whose validity is dependent on a second
49 value. You defined the dependent type constraint with a primary type constraint
50 (such as 'Int') a 'constraining' value type constraint (such as a Set object)
51 and a coderef which will compare the incoming value to be checked with a value
52 that conforms to the constraining type constraint. Typically there should be a
53 comparision operator between the check value and the constraining value
55 =head2 Subtyping a Dependent type constraints
57 TDB: Need discussion and examples.
61 TBD: Need discussion and example of coercions working for both the
62 constrainted and dependent type constraint.
66 Newer versions of L<MooseX::Types> support recursive type constraints. That is
67 you can include a type constraint as a contained type constraint of itself.
68 Recursion is support in both the dependent and constraining type constraint. For
71 =head1 TYPE CONSTRAINTS
73 This type library defines the following constraints.
75 =head2 Depending[$dependent_tc, $codref, $constraining_tc]
77 Create a subtype of $dependent_tc that is constrainted by a value that is a
78 valid $constraining_tc using $coderef. For example:
80 subtype GreaterThanInt,
84 my($constraining_value, $check_value) = @_;
85 return $constraining_value > $check_value ? 1:0;
90 Note that the coderef is passed the constraining value and the check value as an
91 Array NOT an ArrayRef.
93 This would create a type constraint that takes an integer and checks it against
94 a second integer, requiring that the check value is greater. For example:
96 GreaterThanInt->check([5,10]); ## Fails, 5 is less than 10
97 GreaterThanInt->check(['a',10]); ## Fails, 'a' is not an Int.
98 GreaterThanInt->check([5,'b']); ## Fails, 'b' is not an Int either.
99 GreaterThanInt->check([10,5]); ## Success, 10 is greater than 5.
103 Here are some additional example usage for structured types. All examples can
104 be found also in the 't/examples.t' test. Your contributions are also welcomed.
110 Moose::Util::TypeConstraints::get_type_constraint_registry->add_type_constraint(
111 MooseX::Meta::TypeConstraint::Dependent->new(
112 name => "MooseX::Types::Dependent::Depending" ,
113 parent => find_type_constraint('ArrayRef'),
114 constraint_generator=> sub {
115 my ($dependent_val, $callback, $constraining_val) = @_;
116 return $callback->($dependent_val, $constraining_val);
123 The following modules or resources may be of interest.
125 L<Moose>, L<MooseX::Types>, L<Moose::Meta::TypeConstraint>,
126 L<MooseX::Meta::TypeConstraint::Dependent>
130 Here's a list of stuff I would be happy to get volunteers helping with:
136 Examples of useful code with both POD and ideally a test case to show it
143 John Napiorkowski, C<< <jjnapiork@cpan.org> >>
145 =head1 COPYRIGHT & LICENSE
147 This program is free software; you can redistribute it and/or modify
148 it under the same terms as Perl itself.