5 require Mouse::TypeRegistry;
10 my $caller = caller(0);
13 *{"$caller\::import"} = sub { my $pkg = caller(0); _import($caller, $pkg, @_) };
14 push @{"$caller\::ISA"}, 'MouseX::Types::Base';
16 if (defined $args{'-declare'} && ref($args{'-declare'}) eq 'ARRAY') {
17 my $storage = $caller->type_storage($caller);
18 for my $name (@{ $args{'-declare'} }) {
19 $storage->{$name} = "$caller\::$name";
20 *{"$caller\::$name"} = sub () { $caller->type_storage->{$name} };
24 return Mouse::TypeRegistry->import( callee => $caller );
28 my($type_class, $pkg, @types) = @_;
30 for my $name (@types) {
31 *{"$pkg\::$name"} = sub () { $type_class->type_storage->{$name} }
36 package MouseX::Types::Base;
39 $storage{$_[0]} ||= +{}
47 Mouse - Organise your Mouse types in libraries
51 =head2 Library Definition
55 # predeclare our own types
58 PositiveInt NegativeInt
59 ArrayRefOfPositiveInt ArrayRefOfAtLeastThreeNegativeInts
60 LotsOfInnerConstraints StrOrArrayRef
63 # import builtin types
64 use MouseX::Types::Mouse 'Int';
70 message { "Int is not larger than 0" };
75 message { "Int is not smaller than 0" };
82 # with parameterized constraints.
84 subtype ArrayRefOfPositiveInt,
85 as ArrayRef[PositiveInt];
87 subtype ArrayRefOfAtLeastThreeNegativeInts,
88 as ArrayRef[NegativeInt],
89 where { scalar(@$_) > 2 };
91 subtype LotsOfInnerConstraints,
92 as ArrayRef[ArrayRef[HashRef[Int]]];
94 # with TypeConstraint Unions
96 subtype StrOrArrayRef,
105 use MyLibrary qw( PositiveInt NegativeInt );
107 # use the exported constants as type names
119 print "positive\n" if is_PositiveInt($value);
120 print "negative\n" if is_NegativeInt($value);
122 # coerce the value, NegativeInt doesn't have a coercion
123 # helper, since it didn't define any coercions.
124 $value = to_PositiveInt($value) or die "Cannot coerce";