6 MooseX::Types - Organise your Moose types in libraries
13 use Moose::Util::TypeConstraints;
14 use MooseX::Types::TypeDecorator;
15 use MooseX::Types::Base ();
16 use MooseX::Types::Util qw( filter_tags );
17 use MooseX::Types::UndefinedType;
18 use Carp::Clan qw( ^MooseX::Types );
20 use namespace::clean -except => [qw( meta )];
23 my $UndefMsg = q{Action for type '%s' not yet defined in library '%s'};
27 =head2 Library Definition
31 # predeclare our own types
34 PositiveInt NegativeInt
35 ArrayRefOfPositiveInt ArrayRefOfAtLeastThreeNegativeInts
36 LotsOfInnerConstraints StrOrArrayRef
39 # import builtin types
40 use MooseX::Types::Moose 'Int';
46 message { "Int is not larger than 0" };
51 message { "Int is not smaller than 0" };
58 # with parameterized constraints.
60 subtype ArrayRefOfPositiveInt,
61 as ArrayRef[PositiveInt];
63 subtype ArrayRefOfAtLeastThreeNegativeInts,
64 as ArrayRef[NegativeInt],
65 where { scalar(@$_) > 2 };
67 subtype LotsOfInnerConstraints,
68 as ArrayRef[ArrayRef[HashRef[Int]]];
70 # with TypeConstraint Unions
72 subtype StrOrArrayRef,
81 use MyLibrary qw( PositiveInt NegativeInt );
83 # use the exported constants as type names
95 print "positive\n" if is_PositiveInt($value);
96 print "negative\n" if is_NegativeInt($value);
98 # coerce the value, NegativeInt doesn't have a coercion
99 # helper, since it didn't define any coercions.
100 $value = to_PositiveInt($value) or die "Cannot coerce";
107 The types provided with L<Moose> are by design global. This package helps
108 you to organise and selectively import your own and the built-in types in
109 libraries. As a nice side effect, it catches typos at compile-time too.
111 However, the main reason for this module is to provide an easy way to not
112 have conflicts with your type names, since the internal fully qualified
113 names of the types will be prefixed with the library's name.
115 This module will also provide you with some helper functions to make it
116 easier to use Moose types in your code.
118 =head1 TYPE HANDLER FUNCTIONS
122 A constant with the name of your type. It contains the type's fully
123 qualified name. Takes no value, as all constants.
127 This handler takes a value and tests if it is a valid value for this
128 C<$type>. It will return true or false.
132 A handler that will take a value and coerce it into the C<$type>. It will
133 return a false value if the type could not be coerced.
135 B<Important Note>: This handler will only be exported for types that can
136 do type coercion. This has the advantage that a coercion to a type that
137 cannot hasn't defined any coercions will lead to a compile-time error.
139 =head1 LIBRARY DEFINITION
141 A MooseX::Types is just a normal Perl module. Unlike Moose
142 itself, it does not install C<use strict> and C<use warnings> in your
143 class by default, so this is up to you.
145 The only thing a library is required to do is
147 use MooseX::Types -declare => \@types;
149 with C<@types> being a list of types you wish to define in this library.
150 This line will install a proper base class in your package as well as the
151 full set of L<handlers|/"TYPE HANDLER FUNCTIONS"> for your declared
152 types. It will then hand control over to L<Moose::Util::TypeConstraints>'
153 C<import> method to export the functions you will need to declare your
156 If you want to use Moose' built-in types (e.g. for subtyping) you will
159 use MooseX::Types::Moose @types;
161 to import the helpers from the shipped L<MooseX::Types::Moose>
162 library which can export all types that come with Moose.
164 You will have to define coercions for your types or your library won't
165 export a L</to_$type> coercion helper for it.
167 Note that you currently cannot define types containing C<::>, since
168 exporting would be a problem.
170 You also don't need to use C<warnings> and C<strict>, since the
171 definition of a library automatically exports those.
175 You can import the L<"type helpers"|/"TYPE HANDLER FUNCTIONS"> of a
176 library by C<use>ing it with a list of types to import as arguments. If
177 you want all of them, use the C<:all> tag. For example:
179 use MyLibrary ':all';
180 use MyOtherLibrary qw( TypeA TypeB );
182 MooseX::Types comes with a library of Moose' built-in types called
183 L<MooseX::Types::Moose>.
185 The exporting mechanism is, since version 0.5, implemented via a wrapper
186 around L<Sub::Exporter>. This means you can do something like this:
188 use MyLibrary TypeA => { -as => 'MyTypeA' },
189 TypeB => { -as => 'MyTypeB' };
191 =head1 WRAPPING A LIBRARY
193 You can define your own wrapper subclasses to manipulate the behaviour
194 of a set of library exports. Here is an example:
199 use base 'MooseX::Types::Wrapper';
201 sub coercion_export_generator {
203 my $code = $class->next::method(@_);
205 my $value = $code->(@_);
206 warn "Coercion returned undef!"
207 unless defined $value;
214 This class wraps the coercion generator (e.g., C<to_Int()>) and warns
215 if a coercion returned an undefined value. You can wrap any library
220 use MyWrapper MyLibrary => [qw( Foo Bar )],
221 Moose => [qw( Str Int )];
226 The C<Moose> library name is a special shortcut for
227 L<MooseX::Types::Moose>.
229 =head2 Generator methods you can overload
233 =item type_export_generator( $short, $full )
235 Creates a closure returning the type's L<Moose::Meta::TypeConstraint>
238 =item check_export_generator( $short, $full, $undef_message )
240 This creates the closure used to test if a value is valid for this type.
242 =item coercion_export_generator( $short, $full, $undef_message )
244 This is the closure that's doing coercions.
248 =head2 Provided Parameters
254 The short, exported name of the type.
258 The fully qualified name of this type as L<Moose> knows it.
262 A message that will be thrown when type functionality is used but the
263 type does not yet exist.
265 =head1 NOTES REGARDING TYPE UNIONS
267 L<MooseX::Types> uses L<MooseX::Types::TypeDecorator> to do some overloading
268 which generally allows you to easily create union types:
270 subtype StrOrArrayRef,
273 As with parameterized constrains, this overloading extends to modules using the
274 types you define in a type library.
277 use MooseX::Types::Moose qw(HashRef Int);
279 has 'attr' => (isa=>HashRef|Int);
281 And everything should just work as you'd think.
287 Installs the L<MooseX::Types::Base> class into the caller and
288 exports types according to the specification described in
289 L</"LIBRARY DEFINITION">. This will continue to
290 L<Moose::Util::TypeConstraints>' C<import> method to export helper
291 functions you will need to declare your types.
296 my ($class, %args) = @_;
299 # everyone should want this
303 # inject base class into new library
305 unshift @{ $callee . '::ISA' }, 'MooseX::Types::Base';
308 # generate predeclared type helpers
309 if (my @orig_declare = @{ $args{ -declare } || [] }) {
310 my ($tags, $declare) = filter_tags @orig_declare;
313 for my $type (@$declare) {
315 croak "Cannot create a type containing '::' ($type) at the moment"
318 # add type to library and remember to export
319 $callee->add_type($type);
320 push @to_export, $type;
323 $callee->import({ -full => 1, -into => $callee }, @to_export);
326 # run type constraints import
327 return Moose::Util::TypeConstraints->import({ into => $callee });
330 =head2 type_export_generator
332 Generate a type export, e.g. C<Int()>. This will return either a
333 L<Moose::Meta::TypeConstraint> object, or alternatively a
334 L<MooseX::Types::UndefinedType> object if the type was not
339 sub type_export_generator {
340 my ($class, $type, $name) = @_;
342 ## Return an anonymous subroutine that will generate the proxied type
343 ## constraint for you.
347 if(defined(my $params = shift @_)) {
348 ## We currently only allow a TC to accept a single, ArrayRef
349 ## parameter, as in HashRef[Int], where [Int] is what's inside the
351 if(ref $params eq 'ARRAY') {
352 $type_constraint = $class->create_arged_type_constraint($name, @$params);
354 croak 'Arguments must be an ArrayRef, not '. ref $params;
357 $type_constraint = $class->create_base_type_constraint($name);
359 $type_constraint = defined($type_constraint) ? $type_constraint
360 : MooseX::Types::UndefinedType->new($name);
362 my $type_decorator = $class->create_type_decorator($type_constraint);
364 ## If there are additional args, that means it's probably stuff that
365 ## needs to be returned to the subtype. Not an ideal solution here but
366 ## doesn't seem to cause trouble.
369 return ($type_decorator, @_);
371 return $type_decorator;
376 =head2 create_arged_type_constraint ($name, @args)
378 Given a String $name with @args find the matching typeconstraint and parameterize
383 sub create_arged_type_constraint {
384 my ($class, $name, @args) = @_;
385 my $type_constraint = Moose::Util::TypeConstraints::find_or_create_type_constraint("$name");
386 return $type_constraint->parameterize(@args);
389 =head2 create_base_type_constraint ($name)
391 Given a String $name, find the matching typeconstraint.
395 sub create_base_type_constraint {
396 my ($class, $name) = @_;
397 return find_type_constraint($name);
400 =head2 create_type_decorator ($type_constraint)
402 Given a $type_constraint, return a lightweight L<MooseX::Types::TypeDecorator>
407 sub create_type_decorator {
408 my ($class, $type_constraint) = @_;
409 return MooseX::Types::TypeDecorator->new($type_constraint);
412 =head2 coercion_export_generator
414 This generates a coercion handler function, e.g. C<to_Int($value)>.
418 sub coercion_export_generator {
419 my ($class, $type, $full, $undef_msg) = @_;
423 # we need a type object
424 my $tobj = find_type_constraint($full) or croak $undef_msg;
425 my $return = $tobj->coerce($value);
427 # non-successful coercion returns false
428 return unless $tobj->check($return);
434 =head2 check_export_generator
436 Generates a constraint check closure, e.g. C<is_Int($value)>.
440 sub check_export_generator {
441 my ($class, $type, $full, $undef_msg) = @_;
445 # we need a type object
446 my $tobj = find_type_constraint($full) or croak $undef_msg;
448 return $tobj->check($value);
454 The following are lists of gotcha's and their workarounds for developers coming
455 from the standard string based type constraint names
459 A library makes the types quasi-unique by prefixing their names with (by
460 default) the library package name. If you're only using the type handler
461 functions provided by MooseX::Types, you shouldn't ever have to use
462 a type's actual full name.
464 =head2 Argument separation ('=>' versus ',')
466 The Perlop manpage has this to say about the '=>' operator: "The => operator is
467 a synonym for the comma, but forces any word (consisting entirely of word
468 characters) to its left to be interpreted as a string (as of 5.001). This
469 includes words that might otherwise be considered a constant or function call."
471 Due to this stringification, the following will NOT work as you might think:
473 subtype StrOrArrayRef => as Str|ArrayRef;
475 The 'StrOrArrayRef' will have it's stringification activated this causes the
476 subtype to not be created. Since the bareword type constraints are not strings
477 you really should not try to treat them that way. You will have to use the ','
478 operator instead. The author's of this package realize that all the L<Moose>
479 documention and examples nearly uniformly use the '=>' version of the comma
480 operator and this could be an issue if you are converting code.
482 Patches welcome for discussion.
487 L<Moose::Util::TypeConstraints>,
488 L<MooseX::Types::Moose>,
491 =head1 AUTHOR AND COPYRIGHT
493 Robert 'phaylon' Sedlacek C<E<lt>rs@474.atE<gt>>, with many thanks to
494 the C<#moose> cabal on C<irc.perl.org>.
496 Additional features by John Napiorkowski (jnapiorkowski) <jjnapiork@cpan.org>.
500 This program is free software; you can redistribute it and/or modify
501 it under the same terms as perl itself.