5 require Mouse::TypeRegistry;
6 use MouseX::Types::TypeDecorator;
11 my $caller = caller(0);
14 *{"$caller\::import"} = sub { my $pkg = caller(0); _import($caller, $pkg, @_) };
15 push @{"$caller\::ISA"}, 'MouseX::Types::Base';
17 if (defined $args{'-declare'} && ref($args{'-declare'}) eq 'ARRAY') {
18 my $storage = $caller->type_storage($caller);
19 for my $name (@{ $args{'-declare'} }) {
20 my $obj = $storage->{$name} = "$caller\::$name";
21 *{"$caller\::$name"} = sub () { $obj };
25 return Mouse::TypeRegistry->import( callee => $caller );
29 my($type_class, $pkg, @types) = @_;
31 for my $name (@types) {
32 my $obj = $type_class->type_storage->{$name};
33 $obj = $type_class->type_storage->{$name} = MouseX::Types::TypeDecorator->new($obj)
35 *{"$pkg\::$name"} = sub () { $obj };
40 package MouseX::Types::Base;
43 $storage{$_[0]} ||= +{}
51 Mouse - Organise your Mouse types in libraries
55 =head2 Library Definition
59 # predeclare our own types
62 PositiveInt NegativeInt
65 # import builtin types
66 use MouseX::Types::Mouse 'Int';
72 message { "Int is not larger than 0" };
77 message { "Int is not smaller than 0" };
90 use MyLibrary qw( PositiveInt NegativeInt );
92 # use the exported constants as type names
104 print "positive\n" if is_PositiveInt($value);
105 print "negative\n" if is_NegativeInt($value);
107 # coerce the value, NegativeInt doesn't have a coercion
108 # helper, since it didn't define any coercions.
109 $value = to_PositiveInt($value) or die "Cannot coerce";