File Coverage

blib/lib/MooseX/Types.pm
Criterion Covered Total %
statement 101 102 99.0
branch 22 26 84.6
condition n/a
subroutine 99 99 100.0
pod 6 6 100.0
total 228 233 97.8


line stmt bran cond sub pod time code
1             package MooseX::Types;
2             # git description: v0.44-8-g4f7d179
3             $MooseX::Types::VERSION = '0.45';
4 17     17   894367 use Moose;
  17         6704142  
  17         249  
5             # ABSTRACT: Organise your Moose types in libraries
6             # KEYWORDS: moose types classes objects constraints declare libraries
7              
8 17     17   144735 use Moose::Util::TypeConstraints;
  17         44  
  17         183  
9 17     17   55028 use MooseX::Types::TypeDecorator;
  17         56  
  17         529  
10 17     17   14925 use MooseX::Types::Base ();
  17         68  
  17         697  
11 17     17   186 use MooseX::Types::Util qw( filter_tags );
  17         152  
  17         1894  
12 17     17   12794 use MooseX::Types::UndefinedType;
  17         50  
  17         453  
13 17     17   11505 use MooseX::Types::CheckedUtilExports ();
  17         55  
  17         508  
14 17     17   152 use Carp::Clan qw( ^MooseX::Types );
  17         32  
  17         144  
15 17     17   2947 use Sub::Name;
  17         36  
  17         936  
16 17     17   111 use Scalar::Util 'reftype';
  17         33  
  17         957  
17              
18 17     17   126 use namespace::autoclean;
  17         31  
  17         136  
19              
20 17     17   1495 use 5.008;
  17         73  
  17         4102  
21             my $UndefMsg = q{Action for type '%s' not yet defined in library '%s'};
22              
23             #pod =pod
24             #pod
25             #pod =head1 SYNOPSIS
26             #pod
27             #pod =head2 Library Definition
28             #pod
29             #pod package MyLibrary;
30             #pod
31             #pod # predeclare our own types
32             #pod use MooseX::Types -declare => [
33             #pod qw(
34             #pod PositiveInt
35             #pod NegativeInt
36             #pod ArrayRefOfPositiveInt
37             #pod ArrayRefOfAtLeastThreeNegativeInts
38             #pod LotsOfInnerConstraints
39             #pod StrOrArrayRef
40             #pod MyDateTime
41             #pod )
42             #pod ];
43             #pod
44             #pod # import builtin types
45             #pod use MooseX::Types::Moose qw/Int HashRef/;
46             #pod
47             #pod # type definition.
48             #pod subtype PositiveInt,
49             #pod as Int,
50             #pod where { $_ > 0 },
51             #pod message { "Int is not larger than 0" };
52             #pod
53             #pod subtype NegativeInt,
54             #pod as Int,
55             #pod where { $_ < 0 },
56             #pod message { "Int is not smaller than 0" };
57             #pod
58             #pod # type coercion
59             #pod coerce PositiveInt,
60             #pod from Int,
61             #pod via { 1 };
62             #pod
63             #pod # with parameterized constraints.
64             #pod
65             #pod subtype ArrayRefOfPositiveInt,
66             #pod as ArrayRef[PositiveInt];
67             #pod
68             #pod subtype ArrayRefOfAtLeastThreeNegativeInts,
69             #pod as ArrayRef[NegativeInt],
70             #pod where { scalar(@$_) > 2 };
71             #pod
72             #pod subtype LotsOfInnerConstraints,
73             #pod as ArrayRef[ArrayRef[HashRef[Int]]];
74             #pod
75             #pod # with TypeConstraint Unions
76             #pod
77             #pod subtype StrOrArrayRef,
78             #pod as Str|ArrayRef;
79             #pod
80             #pod # class types
81             #pod
82             #pod class_type 'DateTime';
83             #pod
84             #pod # or better
85             #pod
86             #pod class_type MyDateTime, { class => 'DateTime' };
87             #pod
88             #pod coerce MyDateTime,
89             #pod from HashRef,
90             #pod via { DateTime->new(%$_) };
91             #pod
92             #pod 1;
93             #pod
94             #pod =head2 Usage
95             #pod
96             #pod package Foo;
97             #pod use Moose;
98             #pod use MyLibrary qw( PositiveInt NegativeInt );
99             #pod
100             #pod # use the exported constants as type names
101             #pod has 'bar',
102             #pod isa => PositiveInt,
103             #pod is => 'rw';
104             #pod has 'baz',
105             #pod isa => NegativeInt,
106             #pod is => 'rw';
107             #pod
108             #pod sub quux {
109             #pod my ($self, $value);
110             #pod
111             #pod # test the value
112             #pod print "positive\n" if is_PositiveInt($value);
113             #pod print "negative\n" if is_NegativeInt($value);
114             #pod
115             #pod # coerce the value, NegativeInt doesn't have a coercion
116             #pod # helper, since it didn't define any coercions.
117             #pod $value = to_PositiveInt($value) or die "Cannot coerce";
118             #pod }
119             #pod
120             #pod 1;
121             #pod
122             #pod =head1 DESCRIPTION
123             #pod
124             #pod The type system provided by Moose effectively makes all of its builtin type
125             #pod global, as are any types you declare with Moose. This means that every module
126             #pod that declares a type named C<PositiveInt> is sharing the same type object. This
127             #pod can be a problem when different parts of the code base want to use the same
128             #pod name for different things.
129             #pod
130             #pod This package lets you declare types using short names, but behind the scenes
131             #pod it namespaces all your type declarations, effectively prevent name clashes
132             #pod between packages.
133             #pod
134             #pod This is done by creating a type library module like C<MyApp::Types> and then
135             #pod importing types from that module into other modules.
136             #pod
137             #pod As a side effect, the declaration mechanism allows you to write type names as
138             #pod barewords (really function calls), which catches typos in names at compile
139             #pod time rather than run time.
140             #pod
141             #pod This module also provides some helper functions for using Moose types outside
142             #pod of attribute declarations.
143             #pod
144             #pod If you mix string-based names with types created by this module, it will warn,
145             #pod with a few exceptions. If you are declaring a C<class_type()> or
146             #pod C<role_type()> within your type library, or if you use a fully qualified name
147             #pod like C<"MyApp::Foo">.
148             #pod
149             #pod =head1 LIBRARY DEFINITION
150             #pod
151             #pod A MooseX::Types is just a normal Perl module. Unlike Moose
152             #pod itself, it does not install C<use strict> and C<use warnings> in your
153             #pod class by default, so this is up to you.
154             #pod
155             #pod The only thing a library is required to do is
156             #pod
157             #pod use MooseX::Types -declare => \@types;
158             #pod
159             #pod with C<@types> being a list of types you wish to define in this library.
160             #pod This line will install a proper base class in your package as well as the
161             #pod full set of L<handlers|/"TYPE HANDLER FUNCTIONS"> for your declared
162             #pod types. It will then hand control over to L<Moose::Util::TypeConstraints>'
163             #pod C<import> method to export the functions you will need to declare your
164             #pod types.
165             #pod
166             #pod If you want to use Moose' built-in types (e.g. for subtyping) you will
167             #pod want to
168             #pod
169             #pod use MooseX::Types::Moose @types;
170             #pod
171             #pod to import the helpers from the shipped L<MooseX::Types::Moose>
172             #pod library which can export all types that come with Moose.
173             #pod
174             #pod You will have to define coercions for your types or your library won't
175             #pod export a L</to_$type> coercion helper for it.
176             #pod
177             #pod Note that you currently cannot define types containing C<::>, since
178             #pod exporting would be a problem.
179             #pod
180             #pod You also don't need to use C<warnings> and C<strict>, since the
181             #pod definition of a library automatically exports those.
182             #pod
183             #pod =head1 LIBRARY USAGE
184             #pod
185             #pod You can import the L<"type helpers"|/"TYPE HANDLER FUNCTIONS"> of a
186             #pod library by C<use>ing it with a list of types to import as arguments. If
187             #pod you want all of them, use the C<:all> tag. For example:
188             #pod
189             #pod use MyLibrary ':all';
190             #pod use MyOtherLibrary qw( TypeA TypeB );
191             #pod
192             #pod MooseX::Types comes with a library of Moose' built-in types called
193             #pod L<MooseX::Types::Moose>.
194             #pod
195             #pod The exporting mechanism is, since version 0.5, implemented via a wrapper
196             #pod around L<Sub::Exporter>. This means you can do something like this:
197             #pod
198             #pod use MyLibrary TypeA => { -as => 'MyTypeA' },
199             #pod TypeB => { -as => 'MyTypeB' };
200             #pod
201             #pod =head1 TYPE HANDLER FUNCTIONS
202             #pod
203             #pod =head2 $type
204             #pod
205             #pod A constant with the name of your type. It contains the type's fully
206             #pod qualified name. Takes no value, as all constants.
207             #pod
208             #pod =head2 is_$type
209             #pod
210             #pod This handler takes a value and tests if it is a valid value for this
211             #pod C<$type>. It will return true or false.
212             #pod
213             #pod =head2 to_$type
214             #pod
215             #pod A handler that will take a value and coerce it into the C<$type>. It will
216             #pod return a false value if the type could not be coerced.
217             #pod
218             #pod B<Important Note>: This handler will only be exported for types that can
219             #pod do type coercion. This has the advantage that a coercion to a type that
220             #pod has not defined any coercions will lead to a compile-time error.
221             #pod
222             #pod =head1 WRAPPING A LIBRARY
223             #pod
224             #pod You can define your own wrapper subclasses to manipulate the behaviour
225             #pod of a set of library exports. Here is an example:
226             #pod
227             #pod package MyWrapper;
228             #pod use strict;
229             #pod use MRO::Compat;
230             #pod use base 'MooseX::Types::Wrapper';
231             #pod
232             #pod sub coercion_export_generator {
233             #pod my $class = shift;
234             #pod my $code = $class->next::method(@_);
235             #pod return sub {
236             #pod my $value = $code->(@_);
237             #pod warn "Coercion returned undef!"
238             #pod unless defined $value;
239             #pod return $value;
240             #pod };
241             #pod }
242             #pod
243             #pod 1;
244             #pod
245             #pod This class wraps the coercion generator (e.g., C<to_Int()>) and warns
246             #pod if a coercion returned an undefined value. You can wrap any library
247             #pod with this:
248             #pod
249             #pod package Foo;
250             #pod use strict;
251             #pod use MyWrapper MyLibrary => [qw( Foo Bar )],
252             #pod Moose => [qw( Str Int )];
253             #pod
254             #pod ...
255             #pod 1;
256             #pod
257             #pod The C<Moose> library name is a special shortcut for L<MooseX::Types::Moose>.
258             #pod
259             #pod =head2 Generator methods you can overload
260             #pod
261             #pod =over 4
262             #pod
263             #pod =item type_export_generator( $short, $full )
264             #pod
265             #pod Creates a closure returning the type's L<Moose::Meta::TypeConstraint> object.
266             #pod
267             #pod =item check_export_generator( $short, $full, $undef_message )
268             #pod
269             #pod This creates the closure used to test if a value is valid for this type.
270             #pod
271             #pod =item coercion_export_generator( $short, $full, $undef_message )
272             #pod
273             #pod This is the closure that's doing coercions.
274             #pod
275             #pod =back
276             #pod
277             #pod =head2 Provided Parameters
278             #pod
279             #pod =over 4
280             #pod
281             #pod =item $short
282             #pod
283             #pod The short, exported name of the type.
284             #pod
285             #pod =item $full
286             #pod
287             #pod The fully qualified name of this type as L<Moose> knows it.
288             #pod
289             #pod =item $undef_message
290             #pod
291             #pod A message that will be thrown when type functionality is used but the
292             #pod type does not yet exist.
293             #pod
294             #pod =back
295             #pod
296             #pod =head1 RECURSIVE SUBTYPES
297             #pod
298             #pod As of version 0.08, L<Moose::Types> has experimental support for Recursive
299             #pod subtypes. This will allow:
300             #pod
301             #pod subtype Tree() => as HashRef[Str|Tree];
302             #pod
303             #pod Which validates things like:
304             #pod
305             #pod {key=>'value'};
306             #pod {key=>{subkey1=>'value', subkey2=>'value'}}
307             #pod
308             #pod And so on. This feature is new and there may be lurking bugs so don't be afraid
309             #pod to hunt me down with patches and test cases if you have trouble.
310             #pod
311             #pod =head1 NOTES REGARDING TYPE UNIONS
312             #pod
313             #pod L<MooseX::Types> uses L<MooseX::Types::TypeDecorator> to do some overloading
314             #pod which generally allows you to easily create union types:
315             #pod
316             #pod subtype StrOrArrayRef,
317             #pod as Str|ArrayRef;
318             #pod
319             #pod As with parameterized constraints, this overloading extends to modules using the
320             #pod types you define in a type library.
321             #pod
322             #pod use Moose;
323             #pod use MooseX::Types::Moose qw(HashRef Int);
324             #pod
325             #pod has 'attr' => ( isa => HashRef | Int );
326             #pod
327             #pod And everything should just work as you'd think.
328             #pod
329             #pod =head1 METHODS
330             #pod
331             #pod =head2 import
332             #pod
333             #pod Installs the L<MooseX::Types::Base> class into the caller and exports types
334             #pod according to the specification described in L</"LIBRARY DEFINITION">. This
335             #pod will continue to L<Moose::Util::TypeConstraints>' C<import> method to export
336             #pod helper functions you will need to declare your types.
337             #pod
338             #pod =cut
339              
340             sub import {
341 37     37   11564 my ($class, %args) = @_;
342 37         116 my $caller = caller;
343              
344             # everyone should want this
345 37         733 strict->import;
346 37         626 warnings->import;
347              
348             # inject base class into new library
349 17     17   106 { no strict 'refs';
  17         32  
  17         20776  
  37         61  
350 37         65 unshift @{ $caller . '::ISA' }, 'MooseX::Types::Base';
  37         738  
351             }
352              
353             # generate predeclared type helpers
354 37 100       72 if (my @orig_declare = @{ $args{ -declare } || [] }) {
  37 100       402  
355 20         121 my ($tags, $declare) = filter_tags @orig_declare;
356 20         41 my @to_export;
357              
358 20         63 for my $type (@$declare) {
359              
360 57 100       180 croak "Cannot create a type containing '::' ($type) at the moment"
361             if $type =~ /::/;
362              
363             # add type to library and remember to export
364 56         323 $caller->add_type($type);
365 56         138 push @to_export, $type;
366             }
367              
368             $caller->import({
369             -full => 1,
370             -into => $caller,
371             # just like Sub::Exporter::ForMethods, but with the blessing added
372             installer => sub {
373 19     19   227 my ($arg, $to_export) = @_;
374              
375 19         45 my $into = $arg->{into};
376              
377 19         125 for (my $i = 0; $i < @$to_export; $i += 2) {
378 168         321 my ($as, $code) = @$to_export[ $i, $i+1 ];
379              
380 168 50       341 next if ref $as;
381              
382 168     121   593 my $sub = sub { $code->(@_) };
  99     111   74911  
        131      
        131      
        106      
        108      
        96      
        76      
        68      
        60      
        73      
        68      
        32      
        32      
        32      
        32      
        32      
        32      
        32      
        32      
        32      
        32      
        32      
        32      
        32      
        32      
        32      
        32      
        32      
        32      
        32      
        32      
        32      
        32      
        32      
        32      
        32      
        32      
        32      
        32      
        32      
        32      
        32      
        32      
        32      
        32      
        32      
        32      
        32      
        32      
        32      
383 168         380 my $subtype = blessed $code;
384 168 100       409 bless $sub, $subtype if $subtype;
385              
386 168         1741 $to_export->[ $i + 1 ] = Sub::Name::subname(
387             join(q{::}, $into, $as),
388             $sub,
389             );
390             }
391              
392 19         93 Sub::Exporter::default_installer($arg, $to_export);
393             },
394 19         1200 }, @to_export);
395             }
396              
397             # run type constraints import
398 36         10195 Moose::Util::TypeConstraints->import({ into => $caller });
399              
400             # override some with versions that check for syntax errors
401 36         77048 MooseX::Types::CheckedUtilExports->import({ into => $caller });
402              
403 36         70232 1;
404             }
405              
406             #pod =head2 type_export_generator
407             #pod
408             #pod Generate a type export, e.g. C<Int()>. This will return either a
409             #pod L<Moose::Meta::TypeConstraint> object, or alternatively a
410             #pod L<MooseX::Types::UndefinedType> object if the type was not yet defined.
411             #pod
412             #pod =cut
413              
414             sub type_export_generator {
415 156     156 1 564 my ($class, $type, $name) = @_;
416              
417             ## Return an anonymous subroutine that will generate the proxied type
418             ## constraint for you.
419              
420             return subname "__TYPE__::$name" => sub {
421 251     37   62511 my $type_constraint = $class->create_base_type_constraint($name);
        184      
        153      
        138      
        21      
        21      
        21      
        21      
        110      
        299      
        191      
        193      
        139      
        205      
        181      
        21      
        21      
        21      
        228      
        127      
        127      
        338      
        34      
        34      
        34      
422              
423 251 100       26929 if(defined(my $params = shift @_)) {
424             ## We currently only allow a TC to accept a single, ArrayRef
425             ## parameter, as in HashRef[Int], where [Int] is what's inside the
426             ## ArrayRef passed.
427 21 100       151 if(reftype $params eq 'ARRAY') {
    50          
428 20         88 $type_constraint = $class->create_arged_type_constraint($name, @$params);
429             } elsif(!defined $type_constraint) {
430 1         9 croak "Syntax error in type definition (did you forget a comma"
431             . " after $type?)";
432             } else {
433 0         0 croak "Argument must be an ArrayRef to create a parameterized "
434             . "type, Eg.: ${type}[Int]. Got: ".ref($params)."."
435             }
436             }
437              
438 250 100       893 $type_constraint = defined($type_constraint) ? $type_constraint
439             : MooseX::Types::UndefinedType->new($name);
440              
441 250         788 my $type_decorator = $class->create_type_decorator($type_constraint);
442              
443             ## If there are additional args, that means it's probably stuff that
444             ## needs to be returned to the subtype. Not an ideal solution here but
445             ## doesn't seem to cause trouble.
446              
447 250 100       917 if(@_) {
448 3         20 return ($type_decorator, @_);
449             } else {
450 247         1867 return $type_decorator;
451             }
452 156         2758 };
453             }
454              
455             #pod =head2 create_arged_type_constraint ($name, @args)
456             #pod
457             #pod Given a String $name with @args find the matching type constraint and parameterize
458             #pod it with @args.
459             #pod
460             #pod =cut
461              
462             sub create_arged_type_constraint {
463 20     107 1 55 my ($class, $name, @args) = @_;
464 20         140 my $type_constraint = Moose::Util::TypeConstraints::find_or_create_type_constraint("$name");
465 20         2742 my $parameterized = $type_constraint->parameterize(@args);
466             # It's obnoxious to have to parameterize before looking for the TC, but the
467             # alternative is to hard-code the assumption that the name is
468             # "$name[$args[0]]", which would be worse.
469             # This breaks MXMS, unfortunately, which relies on things like Tuple[...]
470             # creating new type objects each time.
471             # if (my $existing =
472             # Moose::Util::TypeConstraints::find_type_constraint($parameterized->name)) {
473             # return $existing;
474             # }
475             # Moose::Util::TypeConstraints::register_type_constraint($parameterized);
476 20         17202 return $parameterized;
477             }
478              
479             #pod =head2 create_base_type_constraint ($name)
480             #pod
481             #pod Given a String $name, find the matching type constraint.
482             #pod
483             #pod =cut
484              
485             sub create_base_type_constraint {
486 251     331 1 499 my ($class, $name) = @_;
487 251         1058 return find_type_constraint($name);
488             }
489              
490             #pod =head2 create_type_decorator ($type_constraint)
491             #pod
492             #pod Given a $type_constraint, return a lightweight L<MooseX::Types::TypeDecorator>
493             #pod instance.
494             #pod
495             #pod =cut
496              
497             sub create_type_decorator {
498 250     250 1 441 my ($class, $type_constraint) = @_;
499 250         1165 return MooseX::Types::TypeDecorator->new($type_constraint);
500             }
501              
502             #pod =head2 coercion_export_generator
503             #pod
504             #pod This generates a coercion handler function, e.g. C<to_Int($value)>.
505             #pod
506             #pod =cut
507              
508             sub coercion_export_generator {
509 69     69 1 186 my ($class, $type, $full, $undef_msg) = @_;
510             return sub {
511 10     10   14263 my ($value) = @_;
512              
513             # we need a type object
514 10 50       48 my $tobj = find_type_constraint($full) or croak $undef_msg;
515 10         1256 my $return = $tobj->coerce($value);
516              
517             # non-successful coercion returns false
518 10 100       2291 return unless $tobj->check($return);
519              
520 5         6749 return $return;
521             }
522 69         612 }
523              
524             #pod =head2 check_export_generator
525             #pod
526             #pod Generates a constraint check closure, e.g. C<is_Int($value)>.
527             #pod
528             #pod =cut
529              
530             sub check_export_generator {
531 156     156 1 374 my ($class, $type, $full, $undef_msg) = @_;
532             return sub {
533 11     11   9633 my ($value) = @_;
534              
535             # we need a type object
536 11 50       44 my $tobj = find_type_constraint($full) or croak $undef_msg;
537              
538 11         1247 return $tobj->check($value);
539             }
540 156         1048 }
541              
542             __END__
543              
544             =pod
545              
546             =encoding UTF-8
547              
548             =head1 NAME
549              
550             MooseX::Types - Organise your Moose types in libraries
551              
552             =head1 VERSION
553              
554             version 0.45
555              
556             =head1 SYNOPSIS
557              
558             =head2 Library Definition
559              
560             package MyLibrary;
561              
562             # predeclare our own types
563             use MooseX::Types -declare => [
564             qw(
565             PositiveInt
566             NegativeInt
567             ArrayRefOfPositiveInt
568             ArrayRefOfAtLeastThreeNegativeInts
569             LotsOfInnerConstraints
570             StrOrArrayRef
571             MyDateTime
572             )
573             ];
574              
575             # import builtin types
576             use MooseX::Types::Moose qw/Int HashRef/;
577              
578             # type definition.
579             subtype PositiveInt,
580             as Int,
581             where { $_ > 0 },
582             message { "Int is not larger than 0" };
583              
584             subtype NegativeInt,
585             as Int,
586             where { $_ < 0 },
587             message { "Int is not smaller than 0" };
588              
589             # type coercion
590             coerce PositiveInt,
591             from Int,
592             via { 1 };
593              
594             # with parameterized constraints.
595              
596             subtype ArrayRefOfPositiveInt,
597             as ArrayRef[PositiveInt];
598              
599             subtype ArrayRefOfAtLeastThreeNegativeInts,
600             as ArrayRef[NegativeInt],
601             where { scalar(@$_) > 2 };
602              
603             subtype LotsOfInnerConstraints,
604             as ArrayRef[ArrayRef[HashRef[Int]]];
605              
606             # with TypeConstraint Unions
607              
608             subtype StrOrArrayRef,
609             as Str|ArrayRef;
610              
611             # class types
612              
613             class_type 'DateTime';
614              
615             # or better
616              
617             class_type MyDateTime, { class => 'DateTime' };
618              
619             coerce MyDateTime,
620             from HashRef,
621             via { DateTime->new(%$_) };
622              
623             1;
624              
625             =head2 Usage
626              
627             package Foo;
628             use Moose;
629             use MyLibrary qw( PositiveInt NegativeInt );
630              
631             # use the exported constants as type names
632             has 'bar',
633             isa => PositiveInt,
634             is => 'rw';
635             has 'baz',
636             isa => NegativeInt,
637             is => 'rw';
638              
639             sub quux {
640             my ($self, $value);
641              
642             # test the value
643             print "positive\n" if is_PositiveInt($value);
644             print "negative\n" if is_NegativeInt($value);
645              
646             # coerce the value, NegativeInt doesn't have a coercion
647             # helper, since it didn't define any coercions.
648             $value = to_PositiveInt($value) or die "Cannot coerce";
649             }
650              
651             1;
652              
653             =head1 DESCRIPTION
654              
655             The type system provided by Moose effectively makes all of its builtin type
656             global, as are any types you declare with Moose. This means that every module
657             that declares a type named C<PositiveInt> is sharing the same type object. This
658             can be a problem when different parts of the code base want to use the same
659             name for different things.
660              
661             This package lets you declare types using short names, but behind the scenes
662             it namespaces all your type declarations, effectively prevent name clashes
663             between packages.
664              
665             This is done by creating a type library module like C<MyApp::Types> and then
666             importing types from that module into other modules.
667              
668             As a side effect, the declaration mechanism allows you to write type names as
669             barewords (really function calls), which catches typos in names at compile
670             time rather than run time.
671              
672             This module also provides some helper functions for using Moose types outside
673             of attribute declarations.
674              
675             If you mix string-based names with types created by this module, it will warn,
676             with a few exceptions. If you are declaring a C<class_type()> or
677             C<role_type()> within your type library, or if you use a fully qualified name
678             like C<"MyApp::Foo">.
679              
680             =head1 LIBRARY DEFINITION
681              
682             A MooseX::Types is just a normal Perl module. Unlike Moose
683             itself, it does not install C<use strict> and C<use warnings> in your
684             class by default, so this is up to you.
685              
686             The only thing a library is required to do is
687              
688             use MooseX::Types -declare => \@types;
689              
690             with C<@types> being a list of types you wish to define in this library.
691             This line will install a proper base class in your package as well as the
692             full set of L<handlers|/"TYPE HANDLER FUNCTIONS"> for your declared
693             types. It will then hand control over to L<Moose::Util::TypeConstraints>'
694             C<import> method to export the functions you will need to declare your
695             types.
696              
697             If you want to use Moose' built-in types (e.g. for subtyping) you will
698             want to
699              
700             use MooseX::Types::Moose @types;
701              
702             to import the helpers from the shipped L<MooseX::Types::Moose>
703             library which can export all types that come with Moose.
704              
705             You will have to define coercions for your types or your library won't
706             export a L</to_$type> coercion helper for it.
707              
708             Note that you currently cannot define types containing C<::>, since
709             exporting would be a problem.
710              
711             You also don't need to use C<warnings> and C<strict>, since the
712             definition of a library automatically exports those.
713              
714             =head1 LIBRARY USAGE
715              
716             You can import the L<"type helpers"|/"TYPE HANDLER FUNCTIONS"> of a
717             library by C<use>ing it with a list of types to import as arguments. If
718             you want all of them, use the C<:all> tag. For example:
719              
720             use MyLibrary ':all';
721             use MyOtherLibrary qw( TypeA TypeB );
722              
723             MooseX::Types comes with a library of Moose' built-in types called
724             L<MooseX::Types::Moose>.
725              
726             The exporting mechanism is, since version 0.5, implemented via a wrapper
727             around L<Sub::Exporter>. This means you can do something like this:
728              
729             use MyLibrary TypeA => { -as => 'MyTypeA' },
730             TypeB => { -as => 'MyTypeB' };
731              
732             =head1 TYPE HANDLER FUNCTIONS
733              
734             =head2 $type
735              
736             A constant with the name of your type. It contains the type's fully
737             qualified name. Takes no value, as all constants.
738              
739             =head2 is_$type
740              
741             This handler takes a value and tests if it is a valid value for this
742             C<$type>. It will return true or false.
743              
744             =head2 to_$type
745              
746             A handler that will take a value and coerce it into the C<$type>. It will
747             return a false value if the type could not be coerced.
748              
749             B<Important Note>: This handler will only be exported for types that can
750             do type coercion. This has the advantage that a coercion to a type that
751             has not defined any coercions will lead to a compile-time error.
752              
753             =head1 WRAPPING A LIBRARY
754              
755             You can define your own wrapper subclasses to manipulate the behaviour
756             of a set of library exports. Here is an example:
757              
758             package MyWrapper;
759             use strict;
760             use MRO::Compat;
761             use base 'MooseX::Types::Wrapper';
762              
763             sub coercion_export_generator {
764             my $class = shift;
765             my $code = $class->next::method(@_);
766             return sub {
767             my $value = $code->(@_);
768             warn "Coercion returned undef!"
769             unless defined $value;
770             return $value;
771             };
772             }
773              
774             1;
775              
776             This class wraps the coercion generator (e.g., C<to_Int()>) and warns
777             if a coercion returned an undefined value. You can wrap any library
778             with this:
779              
780             package Foo;
781             use strict;
782             use MyWrapper MyLibrary => [qw( Foo Bar )],
783             Moose => [qw( Str Int )];
784              
785             ...
786             1;
787              
788             The C<Moose> library name is a special shortcut for L<MooseX::Types::Moose>.
789              
790             =head2 Generator methods you can overload
791              
792             =over 4
793              
794             =item type_export_generator( $short, $full )
795              
796             Creates a closure returning the type's L<Moose::Meta::TypeConstraint> object.
797              
798             =item check_export_generator( $short, $full, $undef_message )
799              
800             This creates the closure used to test if a value is valid for this type.
801              
802             =item coercion_export_generator( $short, $full, $undef_message )
803              
804             This is the closure that's doing coercions.
805              
806             =back
807              
808             =head2 Provided Parameters
809              
810             =over 4
811              
812             =item $short
813              
814             The short, exported name of the type.
815              
816             =item $full
817              
818             The fully qualified name of this type as L<Moose> knows it.
819              
820             =item $undef_message
821              
822             A message that will be thrown when type functionality is used but the
823             type does not yet exist.
824              
825             =back
826              
827             =head1 RECURSIVE SUBTYPES
828              
829             As of version 0.08, L<Moose::Types> has experimental support for Recursive
830             subtypes. This will allow:
831              
832             subtype Tree() => as HashRef[Str|Tree];
833              
834             Which validates things like:
835              
836             {key=>'value'};
837             {key=>{subkey1=>'value', subkey2=>'value'}}
838              
839             And so on. This feature is new and there may be lurking bugs so don't be afraid
840             to hunt me down with patches and test cases if you have trouble.
841              
842             =head1 NOTES REGARDING TYPE UNIONS
843              
844             L<MooseX::Types> uses L<MooseX::Types::TypeDecorator> to do some overloading
845             which generally allows you to easily create union types:
846              
847             subtype StrOrArrayRef,
848             as Str|ArrayRef;
849              
850             As with parameterized constraints, this overloading extends to modules using the
851             types you define in a type library.
852              
853             use Moose;
854             use MooseX::Types::Moose qw(HashRef Int);
855              
856             has 'attr' => ( isa => HashRef | Int );
857              
858             And everything should just work as you'd think.
859              
860             =head1 METHODS
861              
862             =head2 import
863              
864             Installs the L<MooseX::Types::Base> class into the caller and exports types
865             according to the specification described in L</"LIBRARY DEFINITION">. This
866             will continue to L<Moose::Util::TypeConstraints>' C<import> method to export
867             helper functions you will need to declare your types.
868              
869             =head2 type_export_generator
870              
871             Generate a type export, e.g. C<Int()>. This will return either a
872             L<Moose::Meta::TypeConstraint> object, or alternatively a
873             L<MooseX::Types::UndefinedType> object if the type was not yet defined.
874              
875             =head2 create_arged_type_constraint ($name, @args)
876              
877             Given a String $name with @args find the matching type constraint and parameterize
878             it with @args.
879              
880             =head2 create_base_type_constraint ($name)
881              
882             Given a String $name, find the matching type constraint.
883              
884             =head2 create_type_decorator ($type_constraint)
885              
886             Given a $type_constraint, return a lightweight L<MooseX::Types::TypeDecorator>
887             instance.
888              
889             =head2 coercion_export_generator
890              
891             This generates a coercion handler function, e.g. C<to_Int($value)>.
892              
893             =head2 check_export_generator
894              
895             Generates a constraint check closure, e.g. C<is_Int($value)>.
896              
897             =head1 CAVEATS
898              
899             The following are lists of gotchas and their workarounds for developers coming
900             from the standard string based type constraint names
901              
902             =head2 Uniqueness
903              
904             A library makes the types quasi-unique by prefixing their names with (by
905             default) the library package name. If you're only using the type handler
906             functions provided by MooseX::Types, you shouldn't ever have to use
907             a type's actual full name.
908              
909             =head2 Argument separation ('=>' versus ',')
910              
911             The L<perlop> manpage has this to say about the '=>' operator: "The => operator is
912             a synonym for the comma, but forces any word (consisting entirely of word
913             characters) to its left to be interpreted as a string (as of 5.001). This
914             includes words that might otherwise be considered a constant or function call."
915              
916             Due to this stringification, the following will NOT work as you might think:
917              
918             subtype StrOrArrayRef => as Str | ArrayRef;
919              
920             The C<StrOrArrayRef> type will have its stringification activated -- this causes the
921             subtype to not be created. Since the bareword type constraints are not strings
922             you really should not try to treat them that way. You will have to use the ','
923             operator instead. The authors of this package realize that all the L<Moose>
924             documentation and examples nearly uniformly use the '=>' version of the comma
925             operator and this could be an issue if you are converting code.
926              
927             Patches welcome for discussion.
928              
929             =head2 Compatibility with Sub::Exporter
930              
931             If you want to use L<Sub::Exporter> with a Type Library, you need to make sure
932             you export all the type constraints declared AS WELL AS any additional export
933             targets. For example if you do:
934              
935             package TypeAndSubExporter;
936              
937             use MooseX::Types::Moose qw(Str);
938             use MooseX::Types -declare => [qw(MyStr)];
939             use Sub::Exporter -setup => { exports => [qw(something)] };
940              
941             subtype MyStr, as Str;
942              
943             sub something {
944             return 1;
945             }
946              
947             # then in another module ...
948              
949             package Foo;
950             use TypeAndSubExporter qw(MyStr);
951              
952             You'll get a C<< "MyStr" is not exported by the TypeAndSubExporter module >> error.
953             It can be worked around by:
954              
955             - use Sub::Exporter -setup => { exports => [ qw(something) ] };
956             + use Sub::Exporter -setup => { exports => [ qw(something MyStr) ] };
957              
958             This is a workaround and I am exploring how to make these modules work better
959             together. I realize this workaround will lead a lot of duplication in your
960             export declarations and will be onerous for large type libraries. Patches and
961             detailed test cases welcome. See the tests directory for a start on this.
962              
963             =head1 COMBINING TYPE LIBRARIES
964              
965             You may want to combine a set of types for your application with other type
966             libraries, like L<MooseX::Types::Moose> or L<MooseX::Types::Common::String>.
967              
968             The L<MooseX::Types::Combine> module provides a simple API for combining a set
969             of type libraries together.
970              
971             =head1 SEE ALSO
972              
973             L<Moose>, L<Moose::Util::TypeConstraints>, L<MooseX::Types::Moose>,
974             L<Sub::Exporter>
975              
976             =head1 ACKNOWLEDGEMENTS
977              
978             Many thanks to the C<#moose> cabal on C<irc.perl.org>.
979              
980             =head1 AUTHOR
981              
982             Robert "phaylon" Sedlacek <rs@474.at>
983              
984             =head1 COPYRIGHT AND LICENSE
985              
986             This software is copyright (c) 2007 by Robert "phaylon" Sedlacek.
987              
988             This is free software; you can redistribute it and/or modify it under
989             the same terms as the Perl 5 programming language system itself.
990              
991             =head1 CONTRIBUTORS
992              
993             =for stopwords Dave Rolsky Karen Etheridge John Napiorkowski Robert 'phaylon' Sedlacek Rafael Kitover Florian Ragwitz Matt S Trout Hans Dieter Pearcey Jesse Luehrs Tomas Doran (t0m) matthewt Justin Hunter Kent Fredric Paul Fenwick Graham Knop
994              
995             =over 4
996              
997             =item *
998              
999             Dave Rolsky <autarch@urth.org>
1000              
1001             =item *
1002              
1003             Karen Etheridge <ether@cpan.org>
1004              
1005             =item *
1006              
1007             John Napiorkowski <jjnapiork@cpan.org>
1008              
1009             =item *
1010              
1011             Robert 'phaylon' Sedlacek <phaylon@cpan.org>
1012              
1013             =item *
1014              
1015             Rafael Kitover <rkitover@cpan.org>
1016              
1017             =item *
1018              
1019             Florian Ragwitz <rafl@debian.org>
1020              
1021             =item *
1022              
1023             Matt S Trout <mst@shadowcat.co.uk>
1024              
1025             =item *
1026              
1027             Hans Dieter Pearcey <hdp@weftsoar.net>
1028              
1029             =item *
1030              
1031             Jesse Luehrs <doy@tozt.net>
1032              
1033             =item *
1034              
1035             Tomas Doran (t0m) <bobtfish@bobtfish.net>
1036              
1037             =item *
1038              
1039             matthewt <matthewt@3efe9002-19ed-0310-8735-a98156148065>
1040              
1041             =item *
1042              
1043             Justin Hunter <justin.d.hunter@gmail.com>
1044              
1045             =item *
1046              
1047             Kent Fredric <kentfredric@gmail.com>
1048              
1049             =item *
1050              
1051             Paul Fenwick <pjf@perltraining.com.au>
1052              
1053             =item *
1054              
1055             Graham Knop <haarg@haarg.org>
1056              
1057             =back
1058              
1059             =cut