1 package MooseX::TypeLibrary;
5 MooseX::TypeLibrary - Organise your Moose types in libraries
13 use Moose::Util::TypeConstraints;
14 use MooseX::TypeLibrary::Base;
15 use Sub::Install qw( install_sub );
20 my $UndefMsg = q{Action for type '%s' not yet defined in library '%s'};
30 # predeclare our own types
31 use MooseX::TypeLibrary
32 -declare => [qw( PositiveInt NegativeInt )];
34 # import builtin types
35 use MooseX::TypeLibrary::Moose 'Int';
41 message { "Int is not larger than 0" };
46 message { "Int is not smaller than 0" };
60 use MyLibrary qw( PositiveInt NegativeInt );
62 # use the exported constants as type names
74 print "positive\n" if is_PositiveInt($value);
75 print "negative\n" if is_NegativeInt($value);
77 # coerce the value, NegativeInt doesn't have a coercion
78 # helper, since it didn't define any coercions.
79 $value = to_PositiveInt($value) or die "Cannot coerce";
86 The types provided with L<Moose> are by design global. This package helps
87 you to organise and selectively import your own and the built-in types in
88 libraries. As a nice side effect, it catches typos at compile-time too.
90 However, the main reason for this module is to provide an easy way to not
91 have conflicts with your type names, since the internal fully qualified
92 names of the types will be prefixed with the library's name.
94 This module will also provide you with some helper functions to make it
95 easier to use Moose types in your code.
97 =head1 TYPE HANDLER FUNCTIONS
101 A constant with the name of your type. It contains the type's fully
102 qualified name. Takes no value, as all constants.
106 This handler takes a value and tests if it is a valid value for this
107 C<$type>. It will return true or false.
111 A handler that will take a value and coerce it into the C<$type>. It will
112 return a false value if the type could not be coerced.
114 B<Important Note>: This handler will only be exported for types that can
115 do type coercion. This has the advantage that a coercion to a type that
116 cannot hasn't defined any coercions will lead to a compile-time error.
118 =head1 LIBRARY DEFINITION
120 A MooseX::TypeLibrary is just a normal Perl module. Unlike Moose
121 itself, it does not install C<use strict> and C<use warnings> in your
122 class by default, so this is up to you.
124 The only thing a library is required to do is
126 use MooseX::TypeLibrary -declare => \@types;
128 with C<@types> being a list of types you wish to define in this library.
129 This line will install a proper base class in your package as well as the
130 full set of L<handlers|/"TYPE HANDLER FUNCTIONS"> for your declared
131 types. It will then hand control over to L<Moose::Util::TypeConstraints>'
132 C<import> method to export the functions you will need to declare your
135 If you want to use Moose' built-in types (e.g. for subtyping) you will
138 use MooseX::TypeLibrary::Moose @types;
140 to import the helpers from the shipped L<MooseX::TypeLibrary::Moose>
141 library which can export all types that come with Moose.
143 You will have to define coercions for your types or your library won't
144 export a L</to_$type> coercion helper for it.
148 You can import the L<"type helpers"|/"TYPE HANDLER FUNCTIONS"> of a
149 library by C<use>ing it with a list of types to import as arguments. If
150 you want all of them, use the C<:all> tag. For example:
152 use MyLibrary ':all';
153 use MyOtherLibrary qw( TypeA TypeB );
155 MooseX::TypeLibrary comes with a library of Moose' built-in types called
156 L<MooseX::TypeLibrary::Moose>.
165 my ($class, %args) = @_;
168 # inject base class into new library
170 unshift @{ $callee . '::ISA' }, 'MooseX::TypeLibrary::Base';
173 # generate predeclared type helpers
174 if (my @declare = @{ $args{ -declare } || [] }) {
175 for my $type (@declare) {
176 $callee->add_type($type);
177 $callee->export_type_into(
179 sprintf($UndefMsg, $type, $callee),
185 # run type constraints import
187 Moose::Util::TypeConstraints->can('import'),
188 'Moose::Util::TypeConstraints';
191 =head2 type_export_generator
195 sub type_export_generator {
196 my ($class, $type, $full) = @_;
197 return sub { $full };
200 =head2 coercion_export_generator
204 sub coercion_export_generator {
205 my ($class, $type, $full, $undef_msg) = @_;
209 # we need a type object
210 my $tobj = find_type_constraint($full) or croak $undef_msg;
211 my $return = $tobj->coerce($value);
213 # non-successful coercion returns false
214 return unless $tobj->check($return);
220 =head2 check_export_generator
224 sub check_export_generator {
225 my ($class, $type, $full, $undef_msg) = @_;
229 # we need a type object
230 my $tobj = find_type_constraint($full) or croak $undef_msg;
232 return $tobj->check($value);
238 L<Moose>, L<Moose::Util::TypeConstraints>, L<MooseX::TypeLibrary::Moose>
240 =head1 AUTHOR AND COPYRIGHT
242 Robert 'phaylon' Sedlacek C<E<lt>rs@474.atE<gt>>, with many thanks to
243 the C<#moose> cabal on C<irc.perl.org>.
247 This program is free software; you can redistribute it and/or modify
248 it under the same terms as perl itself.