2 MooseX::Types - Organise your Moose types in libraries
8 # predeclare our own types
11 PositiveInt NegativeInt
12 ArrayRefOfPositiveInt ArrayRefOfAtLeastThreeNegativeInts
13 LotsOfInnerConstraints StrOrArrayRef
16 # import builtin types
17 use MooseX::Types::Moose 'Int';
23 message { "Int is not larger than 0" };
28 message { "Int is not smaller than 0" };
35 # with parameterized constraints.
37 subtype ArrayRefOfPositiveInt,
38 as ArrayRef[PositiveInt];
40 subtype ArrayRefOfAtLeastThreeNegativeInts,
41 as ArrayRef[NegativeInt],
42 where { scalar(@$_) > 2 };
44 subtype LotsOfInnerConstraints,
45 as ArrayRef[ArrayRef[HashRef[Int]]];
47 # with TypeConstraint Unions
49 subtype StrOrArrayRef,
57 use MyLibrary qw( PositiveInt NegativeInt );
59 # use the exported constants as type names
71 print "positive\n" if is_PositiveInt($value);
72 print "negative\n" if is_NegativeInt($value);
74 # coerce the value, NegativeInt doesn't have a coercion
75 # helper, since it didn't define any coercions.
76 $value = to_PositiveInt($value) or die "Cannot coerce";
82 The types provided with Moose are by design global. This package helps
83 you to organise and selectively import your own and the built-in types
84 in libraries. As a nice side effect, it catches typos at compile-time
87 However, the main reason for this module is to provide an easy way to
88 not have conflicts with your type names, since the internal fully
89 qualified names of the types will be prefixed with the library's name.
91 This module will also provide you with some helper functions to make it
92 easier to use Moose types in your code.
94 TYPE HANDLER FUNCTIONS
96 A constant with the name of your type. It contains the type's fully
97 qualified name. Takes no value, as all constants.
100 This handler takes a value and tests if it is a valid value for this
101 $type. It will return true or false.
104 A handler that will take a value and coerce it into the $type. It will
105 return a false value if the type could not be coerced.
107 Important Note: This handler will only be exported for types that can do
108 type coercion. This has the advantage that a coercion to a type that
109 cannot hasn't defined any coercions will lead to a compile-time error.
112 A MooseX::Types is just a normal Perl module. Unlike Moose itself, it
113 does not install "use strict" and "use warnings" in your class by
114 default, so this is up to you.
116 The only thing a library is required to do is
118 use MooseX::Types -declare => \@types;
120 with @types being a list of types you wish to define in this library.
121 This line will install a proper base class in your package as well as
122 the full set of handlers for your declared types. It will then hand
123 control over to Moose::Util::TypeConstraints' "import" method to export
124 the functions you will need to declare your types.
126 If you want to use Moose' built-in types (e.g. for subtyping) you will
129 use MooseX::Types::Moose @types;
131 to import the helpers from the shipped MooseX::Types::Moose library
132 which can export all types that come with Moose.
134 You will have to define coercions for your types or your library won't
135 export a "to_$type" coercion helper for it.
137 Note that you currently cannot define types containing "::", since
138 exporting would be a problem.
140 You also don't need to use "warnings" and "strict", since the definition
141 of a library automatically exports those.
144 You can import the "type helpers" of a library by "use"ing it with a
145 list of types to import as arguments. If you want all of them, use the
146 ":all" tag. For example:
148 use MyLibrary ':all';
149 use MyOtherLibrary qw( TypeA TypeB );
151 MooseX::Types comes with a library of Moose' built-in types called
152 MooseX::Types::Moose.
154 The exporting mechanism is, since version 0.5, implemented via a wrapper
155 around Sub::Exporter. This means you can do something like this:
157 use MyLibrary TypeA => { -as => 'MyTypeA' },
158 TypeB => { -as => 'MyTypeB' };
161 You can define your own wrapper subclasses to manipulate the behaviour
162 of a set of library exports. Here is an example:
167 use base 'MooseX::Types::Wrapper';
169 sub coercion_export_generator {
171 my $code = $class->next::method(@_);
173 my $value = $code->(@_);
174 warn "Coercion returned undef!"
175 unless defined $value;
182 This class wraps the coercion generator (e.g., "to_Int()") and warns if
183 a coercion returned an undefined value. You can wrap any library with
188 use MyWrapper MyLibrary => [qw( Foo Bar )],
189 Moose => [qw( Str Int )];
194 The "Moose" library name is a special shortcut for MooseX::Types::Moose.
196 Generator methods you can overload
197 type_export_generator( $short, $full )
198 Creates a closure returning the type's Moose::Meta::TypeConstraint
201 check_export_generator( $short, $full, $undef_message )
202 This creates the closure used to test if a value is valid for this
205 coercion_export_generator( $short, $full, $undef_message )
206 This is the closure that's doing coercions.
210 The short, exported name of the type.
213 The fully qualified name of this type as Moose knows it.
216 A message that will be thrown when type functionality is used but
217 the type does not yet exist.
219 NOTES REGARDING TYPE UNIONS
220 MooseX::Types uses MooseX::Types::TypeDecorator to do some
221 overloading which generally allows you to easily create union types:
223 subtype StrOrArrayRef,
226 As with parameterized constrains, this overloading extends to
227 modules using the types you define in a type library.
230 use MooseX::Types::Moose qw(HashRef Int);
232 has 'attr' => (isa=>HashRef|Int);
234 And everything should just work as you'd think.
238 Installs the MooseX::Types::Base class into the caller and exports
239 types according to the specification described in "LIBRARY
240 DEFINITION". This will continue to Moose::Util::TypeConstraints'
241 "import" method to export helper functions you will need to declare
244 type_export_generator
245 Generate a type export, e.g. "Int()". This will return either a
246 Moose::Meta::TypeConstraint object, or alternatively a
247 MooseX::Types::UndefinedType object if the type was not yet defined.
249 create_arged_type_constraint ($name, @args)
250 Given a String $name with @args find the matching typeconstraint and
251 parameterize it with @args.
253 create_base_type_constraint ($name)
254 Given a String $name, find the matching typeconstraint.
256 create_type_decorator ($type_constraint)
257 Given a $type_constraint, return a lightweight
258 MooseX::Types::TypeDecorator instance.
260 coercion_export_generator
261 This generates a coercion handler function, e.g. "to_Int($value)".
263 check_export_generator
264 Generates a constraint check closure, e.g. "is_Int($value)".
267 The following are lists of gotcha's and their workarounds for
268 developers coming from the standard string based type constraint
272 A library makes the types quasi-unique by prefixing their names with
273 (by default) the library package name. If you're only using the type
274 handler functions provided by MooseX::Types, you shouldn't ever have
275 to use a type's actual full name.
277 Argument separation ('=>' versus ',')
278 The Perlop manpage has this to say about the '=>' operator: "The =>
279 operator is a synonym for the comma, but forces any word (consisting
280 entirely of word characters) to its left to be interpreted as a
281 string (as of 5.001). This includes words that might otherwise be
282 considered a constant or function call."
284 Due to this stringification, the following will NOT work as you
287 subtype StrOrArrayRef => as Str|ArrayRef;
289 The 'StrOrArrayRef' will have it's stringification activated this
290 causes the subtype to not be created. Since the bareword type
291 constraints are not strings you really should not try to treat them
292 that way. You will have to use the ',' operator instead. The
293 author's of this package realize that all the Moose documention and
294 examples nearly uniformly use the '=>' version of the comma operator
295 and this could be an issue if you are converting code.
297 Patches welcome for discussion.
300 Moose, Moose::Util::TypeConstraints, MooseX::Types::Moose,
304 Robert 'phaylon' Sedlacek "<rs@474.at>", with many thanks to the
305 "#moose" cabal on "irc.perl.org".
307 Additional features by John Napiorkowski (jnapiorkowski)
308 <jjnapiork@cpan.org>.
311 This program is free software; you can redistribute it and/or modify
312 it under the same terms as perl itself.