| 1 | package MooseX::Types; # git description: v0.50-19-ga14f8fd | ||||||||||||||||||||||
| 2 | |||||||||||||||||||||||
| 3 | # ABSTRACT: Organise your Moose types in libraries | ||||||||||||||||||||||
| 4 | # KEYWORDS: moose types classes objects constraints declare libraries | ||||||||||||||||||||||
| 5 | |||||||||||||||||||||||
| 6 | 17 | our $VERSION = '0.51'; | |||||||||||||||||||||
| 7 | |||||||||||||||||||||||
| 8 | 17 | ▶ | use Moose::Util::TypeConstraints qw( find_type_constraint ); | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 9 | 17 | ▶ | use MooseX::Types::TypeDecorator; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 10 | 17 | ▶ | use MooseX::Types::Base (); | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 11 | 17 | ▶ | use MooseX::Types::Util qw( filter_tags ); | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 12 | 17 | ▶ | use MooseX::Types::UndefinedType; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 13 | 17 | ▶ | use MooseX::Types::CheckedUtilExports (); | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 14 | 17 | ▶ | use Carp::Clan qw( ^MooseX::Types ); | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 15 | 17 | ▶ | use Sub::Util qw( set_subname ); | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 16 | 17 | ▶ | use Scalar::Util qw( reftype ); | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 17 | 17 | ▶ | use Sub::Exporter::ForMethods 0.100052 'method_installer'; # for 'rebless' | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 18 | |||||||||||||||||||||||
| 19 | 17 | ▶ | use namespace::autoclean; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 20 | |||||||||||||||||||||||
| 21 | 17 | ▶ | use 5.008; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 22 | 17 | my $UndefMsg = q{Action for type '%s' not yet defined in library '%s'}; | |||||||||||||||||||||
| 23 | |||||||||||||||||||||||
| 24 | #pod =pod | ||||||||||||||||||||||
| 25 | #pod | ||||||||||||||||||||||
| 26 | #pod =head1 SYNOPSIS | ||||||||||||||||||||||
| 27 | #pod | ||||||||||||||||||||||
| 28 | #pod =head2 Library Definition | ||||||||||||||||||||||
| 29 | #pod | ||||||||||||||||||||||
| 30 | #pod package MyLibrary; | ||||||||||||||||||||||
| 31 | #pod | ||||||||||||||||||||||
| 32 | #pod # predeclare our own types | ||||||||||||||||||||||
| 33 | #pod use MooseX::Types -declare => [ | ||||||||||||||||||||||
| 34 | #pod qw( | ||||||||||||||||||||||
| 35 | #pod PositiveInt | ||||||||||||||||||||||
| 36 | #pod NegativeInt | ||||||||||||||||||||||
| 37 | #pod ArrayRefOfPositiveInt | ||||||||||||||||||||||
| 38 | #pod ArrayRefOfAtLeastThreeNegativeInts | ||||||||||||||||||||||
| 39 | #pod LotsOfInnerConstraints | ||||||||||||||||||||||
| 40 | #pod StrOrArrayRef | ||||||||||||||||||||||
| 41 | #pod MyDateTime | ||||||||||||||||||||||
| 42 | #pod ) | ||||||||||||||||||||||
| 43 | #pod ]; | ||||||||||||||||||||||
| 44 | #pod | ||||||||||||||||||||||
| 45 | #pod # import builtin types | ||||||||||||||||||||||
| 46 | #pod use MooseX::Types::Moose qw/Int HashRef/; | ||||||||||||||||||||||
| 47 | #pod | ||||||||||||||||||||||
| 48 | #pod # type definition. | ||||||||||||||||||||||
| 49 | #pod subtype PositiveInt, | ||||||||||||||||||||||
| 50 | #pod as Int, | ||||||||||||||||||||||
| 51 | #pod where { $_ > 0 }, | ||||||||||||||||||||||
| 52 | #pod message { "Int is not larger than 0" }; | ||||||||||||||||||||||
| 53 | #pod | ||||||||||||||||||||||
| 54 | #pod subtype NegativeInt, | ||||||||||||||||||||||
| 55 | #pod as Int, | ||||||||||||||||||||||
| 56 | #pod where { $_ < 0 }, | ||||||||||||||||||||||
| 57 | #pod message { "Int is not smaller than 0" }; | ||||||||||||||||||||||
| 58 | #pod | ||||||||||||||||||||||
| 59 | #pod # type coercion | ||||||||||||||||||||||
| 60 | #pod coerce PositiveInt, | ||||||||||||||||||||||
| 61 | #pod from Int, | ||||||||||||||||||||||
| 62 | #pod via { 1 }; | ||||||||||||||||||||||
| 63 | #pod | ||||||||||||||||||||||
| 64 | #pod # with parameterized constraints. | ||||||||||||||||||||||
| 65 | #pod | ||||||||||||||||||||||
| 66 | #pod subtype ArrayRefOfPositiveInt, | ||||||||||||||||||||||
| 67 | #pod as ArrayRef[PositiveInt]; | ||||||||||||||||||||||
| 68 | #pod | ||||||||||||||||||||||
| 69 | #pod subtype ArrayRefOfAtLeastThreeNegativeInts, | ||||||||||||||||||||||
| 70 | #pod as ArrayRef[NegativeInt], | ||||||||||||||||||||||
| 71 | #pod where { scalar(@$_) > 2 }; | ||||||||||||||||||||||
| 72 | #pod | ||||||||||||||||||||||
| 73 | #pod subtype LotsOfInnerConstraints, | ||||||||||||||||||||||
| 74 | #pod as ArrayRef[ArrayRef[HashRef[Int]]]; | ||||||||||||||||||||||
| 75 | #pod | ||||||||||||||||||||||
| 76 | #pod # with TypeConstraint Unions | ||||||||||||||||||||||
| 77 | #pod | ||||||||||||||||||||||
| 78 | #pod subtype StrOrArrayRef, | ||||||||||||||||||||||
| 79 | #pod as Str|ArrayRef; | ||||||||||||||||||||||
| 80 | #pod | ||||||||||||||||||||||
| 81 | #pod # class types | ||||||||||||||||||||||
| 82 | #pod | ||||||||||||||||||||||
| 83 | #pod class_type 'DateTime'; | ||||||||||||||||||||||
| 84 | #pod | ||||||||||||||||||||||
| 85 | #pod # or better | ||||||||||||||||||||||
| 86 | #pod | ||||||||||||||||||||||
| 87 | #pod class_type MyDateTime, { class => 'DateTime' }; | ||||||||||||||||||||||
| 88 | #pod | ||||||||||||||||||||||
| 89 | #pod coerce MyDateTime, | ||||||||||||||||||||||
| 90 | #pod from HashRef, | ||||||||||||||||||||||
| 91 | #pod via { DateTime->new(%$_) }; | ||||||||||||||||||||||
| 92 | #pod | ||||||||||||||||||||||
| 93 | #pod 1; | ||||||||||||||||||||||
| 94 | #pod | ||||||||||||||||||||||
| 95 | #pod =head2 Usage | ||||||||||||||||||||||
| 96 | #pod | ||||||||||||||||||||||
| 97 | #pod package Foo; | ||||||||||||||||||||||
| 98 | #pod use Moose; | ||||||||||||||||||||||
| 99 | #pod use MyLibrary qw( PositiveInt NegativeInt ); | ||||||||||||||||||||||
| 100 | #pod | ||||||||||||||||||||||
| 101 | #pod # use the exported constants as type names | ||||||||||||||||||||||
| 102 | #pod has 'bar', | ||||||||||||||||||||||
| 103 | #pod isa => PositiveInt, | ||||||||||||||||||||||
| 104 | #pod is => 'rw'; | ||||||||||||||||||||||
| 105 | #pod has 'baz', | ||||||||||||||||||||||
| 106 | #pod isa => NegativeInt, | ||||||||||||||||||||||
| 107 | #pod is => 'rw'; | ||||||||||||||||||||||
| 108 | #pod | ||||||||||||||||||||||
| 109 | #pod sub quux { | ||||||||||||||||||||||
| 110 | #pod my ($self, $value); | ||||||||||||||||||||||
| 111 | #pod | ||||||||||||||||||||||
| 112 | #pod # test the value | ||||||||||||||||||||||
| 113 | #pod print "positive\n" if is_PositiveInt($value); | ||||||||||||||||||||||
| 114 | #pod print "negative\n" if is_NegativeInt($value); | ||||||||||||||||||||||
| 115 | #pod | ||||||||||||||||||||||
| 116 | #pod # coerce the value, NegativeInt doesn't have a coercion | ||||||||||||||||||||||
| 117 | #pod # helper, since it didn't define any coercions. | ||||||||||||||||||||||
| 118 | #pod $value = to_PositiveInt($value) or die "Cannot coerce"; | ||||||||||||||||||||||
| 119 | #pod } | ||||||||||||||||||||||
| 120 | #pod | ||||||||||||||||||||||
| 121 | #pod 1; | ||||||||||||||||||||||
| 122 | #pod | ||||||||||||||||||||||
| 123 | #pod =head1 DESCRIPTION | ||||||||||||||||||||||
| 124 | #pod | ||||||||||||||||||||||
| 125 | #pod The type system provided by Moose effectively makes all of its builtin type | ||||||||||||||||||||||
| 126 | #pod global, as are any types you declare with Moose. This means that every module | ||||||||||||||||||||||
| 127 | #pod that declares a type named C<PositiveInt> is sharing the same type object. This | ||||||||||||||||||||||
| 128 | #pod can be a problem when different parts of the code base want to use the same | ||||||||||||||||||||||
| 129 | #pod name for different things. | ||||||||||||||||||||||
| 130 | #pod | ||||||||||||||||||||||
| 131 | #pod This package lets you declare types using short names, but behind the scenes | ||||||||||||||||||||||
| 132 | #pod it namespaces all your type declarations, effectively prevent name clashes | ||||||||||||||||||||||
| 133 | #pod between packages. | ||||||||||||||||||||||
| 134 | #pod | ||||||||||||||||||||||
| 135 | #pod This is done by creating a type library module like C<MyApp::Types> and then | ||||||||||||||||||||||
| 136 | #pod importing types from that module into other modules. | ||||||||||||||||||||||
| 137 | #pod | ||||||||||||||||||||||
| 138 | #pod As a side effect, the declaration mechanism allows you to write type names as | ||||||||||||||||||||||
| 139 | #pod barewords (really function calls), which catches typos in names at compile | ||||||||||||||||||||||
| 140 | #pod time rather than run time. | ||||||||||||||||||||||
| 141 | #pod | ||||||||||||||||||||||
| 142 | #pod This module also provides some helper functions for using Moose types outside | ||||||||||||||||||||||
| 143 | #pod of attribute declarations. | ||||||||||||||||||||||
| 144 | #pod | ||||||||||||||||||||||
| 145 | #pod If you mix string-based names with types created by this module, it will warn, | ||||||||||||||||||||||
| 146 | #pod with a few exceptions. If you are declaring a C<class_type()> or | ||||||||||||||||||||||
| 147 | #pod C<role_type()> within your type library, or if you use a fully qualified name | ||||||||||||||||||||||
| 148 | #pod like C<"MyApp::Foo">. | ||||||||||||||||||||||
| 149 | #pod | ||||||||||||||||||||||
| 150 | #pod =head1 LIBRARY DEFINITION | ||||||||||||||||||||||
| 151 | #pod | ||||||||||||||||||||||
| 152 | #pod A MooseX::Types is just a normal Perl module. Unlike Moose | ||||||||||||||||||||||
| 153 | #pod itself, it does not install C<use strict> and C<use warnings> in your | ||||||||||||||||||||||
| 154 | #pod class by default, so this is up to you. | ||||||||||||||||||||||
| 155 | #pod | ||||||||||||||||||||||
| 156 | #pod The only thing a library is required to do is | ||||||||||||||||||||||
| 157 | #pod | ||||||||||||||||||||||
| 158 | #pod use MooseX::Types -declare => \@types; | ||||||||||||||||||||||
| 159 | #pod | ||||||||||||||||||||||
| 160 | #pod with C<@types> being a list of types you wish to define in this library. | ||||||||||||||||||||||
| 161 | #pod This line will install a proper base class in your package as well as the | ||||||||||||||||||||||
| 162 | #pod full set of L<handlers|/"TYPE HANDLER FUNCTIONS"> for your declared | ||||||||||||||||||||||
| 163 | #pod types. It will then hand control over to L<Moose::Util::TypeConstraints>' | ||||||||||||||||||||||
| 164 | #pod C<import> method to export the functions you will need to declare your | ||||||||||||||||||||||
| 165 | #pod types. | ||||||||||||||||||||||
| 166 | #pod | ||||||||||||||||||||||
| 167 | #pod If you want to use Moose' built-in types (e.g. for subtyping) you will | ||||||||||||||||||||||
| 168 | #pod want to | ||||||||||||||||||||||
| 169 | #pod | ||||||||||||||||||||||
| 170 | #pod use MooseX::Types::Moose @types; | ||||||||||||||||||||||
| 171 | #pod | ||||||||||||||||||||||
| 172 | #pod to import the helpers from the shipped L<MooseX::Types::Moose> | ||||||||||||||||||||||
| 173 | #pod library which can export all types that come with Moose. | ||||||||||||||||||||||
| 174 | #pod | ||||||||||||||||||||||
| 175 | #pod You will have to define coercions for your types or your library won't | ||||||||||||||||||||||
| 176 | #pod export a L</to_$type> coercion helper for it. | ||||||||||||||||||||||
| 177 | #pod | ||||||||||||||||||||||
| 178 | #pod Note that you currently cannot define types containing C<::>, since | ||||||||||||||||||||||
| 179 | #pod exporting would be a problem. | ||||||||||||||||||||||
| 180 | #pod | ||||||||||||||||||||||
| 181 | #pod You also don't need to use C<warnings> and C<strict>, since the | ||||||||||||||||||||||
| 182 | #pod definition of a library automatically exports those. | ||||||||||||||||||||||
| 183 | #pod | ||||||||||||||||||||||
| 184 | #pod =head1 LIBRARY USAGE | ||||||||||||||||||||||
| 185 | #pod | ||||||||||||||||||||||
| 186 | #pod You can import the L<"type helpers"|/"TYPE HANDLER FUNCTIONS"> of a | ||||||||||||||||||||||
| 187 | #pod library by C<use>ing it with a list of types to import as arguments. If | ||||||||||||||||||||||
| 188 | #pod you want all of them, use the C<:all> tag. For example: | ||||||||||||||||||||||
| 189 | #pod | ||||||||||||||||||||||
| 190 | #pod use MyLibrary ':all'; | ||||||||||||||||||||||
| 191 | #pod use MyOtherLibrary qw( TypeA TypeB ); | ||||||||||||||||||||||
| 192 | #pod | ||||||||||||||||||||||
| 193 | #pod MooseX::Types comes with a library of Moose' built-in types called | ||||||||||||||||||||||
| 194 | #pod L<MooseX::Types::Moose>. | ||||||||||||||||||||||
| 195 | #pod | ||||||||||||||||||||||
| 196 | #pod The exporting mechanism is, since version 0.5, implemented via a wrapper | ||||||||||||||||||||||
| 197 | #pod around L<Sub::Exporter>. This means you can do something like this: | ||||||||||||||||||||||
| 198 | #pod | ||||||||||||||||||||||
| 199 | #pod use MyLibrary TypeA => { -as => 'MyTypeA' }, | ||||||||||||||||||||||
| 200 | #pod TypeB => { -as => 'MyTypeB' }; | ||||||||||||||||||||||
| 201 | #pod | ||||||||||||||||||||||
| 202 | #pod =head1 TYPE HANDLER FUNCTIONS | ||||||||||||||||||||||
| 203 | #pod | ||||||||||||||||||||||
| 204 | #pod =head2 $type | ||||||||||||||||||||||
| 205 | #pod | ||||||||||||||||||||||
| 206 | #pod A constant with the name of your type. It contains the type's fully | ||||||||||||||||||||||
| 207 | #pod qualified name. Takes no value, as all constants. | ||||||||||||||||||||||
| 208 | #pod | ||||||||||||||||||||||
| 209 | #pod =head2 is_$type | ||||||||||||||||||||||
| 210 | #pod | ||||||||||||||||||||||
| 211 | #pod This handler takes a value and tests if it is a valid value for this | ||||||||||||||||||||||
| 212 | #pod C<$type>. It will return true or false. | ||||||||||||||||||||||
| 213 | #pod | ||||||||||||||||||||||
| 214 | #pod =head2 to_$type | ||||||||||||||||||||||
| 215 | #pod | ||||||||||||||||||||||
| 216 | #pod A handler that will take a value and coerce it into the C<$type>. It will | ||||||||||||||||||||||
| 217 | #pod return a false value if the type could not be coerced. | ||||||||||||||||||||||
| 218 | #pod | ||||||||||||||||||||||
| 219 | #pod B<Important Note>: This handler will only be exported for types that can | ||||||||||||||||||||||
| 220 | #pod do type coercion. This has the advantage that a coercion to a type that | ||||||||||||||||||||||
| 221 | #pod has not defined any coercions will lead to a compile-time error. | ||||||||||||||||||||||
| 222 | #pod | ||||||||||||||||||||||
| 223 | #pod =head1 WRAPPING A LIBRARY | ||||||||||||||||||||||
| 224 | #pod | ||||||||||||||||||||||
| 225 | #pod You can define your own wrapper subclasses to manipulate the behaviour | ||||||||||||||||||||||
| 226 | #pod of a set of library exports. Here is an example: | ||||||||||||||||||||||
| 227 | #pod | ||||||||||||||||||||||
| 228 | #pod package MyWrapper; | ||||||||||||||||||||||
| 229 | #pod use strict; | ||||||||||||||||||||||
| 230 | #pod use MRO::Compat; | ||||||||||||||||||||||
| 231 | #pod use base 'MooseX::Types::Wrapper'; | ||||||||||||||||||||||
| 232 | #pod | ||||||||||||||||||||||
| 233 | #pod sub coercion_export_generator { | ||||||||||||||||||||||
| 234 | #pod my $class = shift; | ||||||||||||||||||||||
| 235 | #pod my $code = $class->next::method(@_); | ||||||||||||||||||||||
| 236 | #pod return sub { | ||||||||||||||||||||||
| 237 | #pod my $value = $code->(@_); | ||||||||||||||||||||||
| 238 | #pod warn "Coercion returned undef!" | ||||||||||||||||||||||
| 239 | #pod unless defined $value; | ||||||||||||||||||||||
| 240 | #pod return $value; | ||||||||||||||||||||||
| 241 | #pod }; | ||||||||||||||||||||||
| 242 | #pod } | ||||||||||||||||||||||
| 243 | #pod | ||||||||||||||||||||||
| 244 | #pod 1; | ||||||||||||||||||||||
| 245 | #pod | ||||||||||||||||||||||
| 246 | #pod This class wraps the coercion generator (e.g., C<to_Int()>) and warns | ||||||||||||||||||||||
| 247 | #pod if a coercion returned an undefined value. You can wrap any library | ||||||||||||||||||||||
| 248 | #pod with this: | ||||||||||||||||||||||
| 249 | #pod | ||||||||||||||||||||||
| 250 | #pod package Foo; | ||||||||||||||||||||||
| 251 | #pod use strict; | ||||||||||||||||||||||
| 252 | #pod use MyWrapper MyLibrary => [qw( Foo Bar )], | ||||||||||||||||||||||
| 253 | #pod Moose => [qw( Str Int )]; | ||||||||||||||||||||||
| 254 | #pod | ||||||||||||||||||||||
| 255 | #pod ... | ||||||||||||||||||||||
| 256 | #pod 1; | ||||||||||||||||||||||
| 257 | #pod | ||||||||||||||||||||||
| 258 | #pod The C<Moose> library name is a special shortcut for L<MooseX::Types::Moose>. | ||||||||||||||||||||||
| 259 | #pod | ||||||||||||||||||||||
| 260 | #pod =head2 Generator methods you can overload | ||||||||||||||||||||||
| 261 | #pod | ||||||||||||||||||||||
| 262 | #pod =over 4 | ||||||||||||||||||||||
| 263 | #pod | ||||||||||||||||||||||
| 264 | #pod =item type_export_generator( $short, $full ) | ||||||||||||||||||||||
| 265 | #pod | ||||||||||||||||||||||
| 266 | #pod Creates a closure returning the type's L<Moose::Meta::TypeConstraint> object. | ||||||||||||||||||||||
| 267 | #pod | ||||||||||||||||||||||
| 268 | #pod =item check_export_generator( $short, $full, $undef_message ) | ||||||||||||||||||||||
| 269 | #pod | ||||||||||||||||||||||
| 270 | #pod This creates the closure used to test if a value is valid for this type. | ||||||||||||||||||||||
| 271 | #pod | ||||||||||||||||||||||
| 272 | #pod =item coercion_export_generator( $short, $full, $undef_message ) | ||||||||||||||||||||||
| 273 | #pod | ||||||||||||||||||||||
| 274 | #pod This is the closure that's doing coercions. | ||||||||||||||||||||||
| 275 | #pod | ||||||||||||||||||||||
| 276 | #pod =back | ||||||||||||||||||||||
| 277 | #pod | ||||||||||||||||||||||
| 278 | #pod =head2 Provided Parameters | ||||||||||||||||||||||
| 279 | #pod | ||||||||||||||||||||||
| 280 | #pod =over 4 | ||||||||||||||||||||||
| 281 | #pod | ||||||||||||||||||||||
| 282 | #pod =item $short | ||||||||||||||||||||||
| 283 | #pod | ||||||||||||||||||||||
| 284 | #pod The short, exported name of the type. | ||||||||||||||||||||||
| 285 | #pod | ||||||||||||||||||||||
| 286 | #pod =item $full | ||||||||||||||||||||||
| 287 | #pod | ||||||||||||||||||||||
| 288 | #pod The fully qualified name of this type as L<Moose> knows it. | ||||||||||||||||||||||
| 289 | #pod | ||||||||||||||||||||||
| 290 | #pod =item $undef_message | ||||||||||||||||||||||
| 291 | #pod | ||||||||||||||||||||||
| 292 | #pod A message that will be thrown when type functionality is used but the | ||||||||||||||||||||||
| 293 | #pod type does not yet exist. | ||||||||||||||||||||||
| 294 | #pod | ||||||||||||||||||||||
| 295 | #pod =back | ||||||||||||||||||||||
| 296 | #pod | ||||||||||||||||||||||
| 297 | #pod =head1 RECURSIVE SUBTYPES | ||||||||||||||||||||||
| 298 | #pod | ||||||||||||||||||||||
| 299 | #pod As of version 0.08, L<MooseX::Types> has experimental support for Recursive | ||||||||||||||||||||||
| 300 | #pod subtypes. This will allow: | ||||||||||||||||||||||
| 301 | #pod | ||||||||||||||||||||||
| 302 | #pod subtype Tree() => as HashRef[Str|Tree]; | ||||||||||||||||||||||
| 303 | #pod | ||||||||||||||||||||||
| 304 | #pod Which validates things like: | ||||||||||||||||||||||
| 305 | #pod | ||||||||||||||||||||||
| 306 | #pod {key=>'value'}; | ||||||||||||||||||||||
| 307 | #pod {key=>{subkey1=>'value', subkey2=>'value'}} | ||||||||||||||||||||||
| 308 | #pod | ||||||||||||||||||||||
| 309 | #pod And so on. This feature is new and there may be lurking bugs so don't be afraid | ||||||||||||||||||||||
| 310 | #pod to hunt me down with patches and test cases if you have trouble. | ||||||||||||||||||||||
| 311 | #pod | ||||||||||||||||||||||
| 312 | #pod =head1 NOTES REGARDING TYPE UNIONS | ||||||||||||||||||||||
| 313 | #pod | ||||||||||||||||||||||
| 314 | #pod L<MooseX::Types> uses L<MooseX::Types::TypeDecorator> to do some overloading | ||||||||||||||||||||||
| 315 | #pod which generally allows you to easily create union types: | ||||||||||||||||||||||
| 316 | #pod | ||||||||||||||||||||||
| 317 | #pod subtype StrOrArrayRef, | ||||||||||||||||||||||
| 318 | #pod as Str|ArrayRef; | ||||||||||||||||||||||
| 319 | #pod | ||||||||||||||||||||||
| 320 | #pod As with parameterized constraints, this overloading extends to modules using the | ||||||||||||||||||||||
| 321 | #pod types you define in a type library. | ||||||||||||||||||||||
| 322 | #pod | ||||||||||||||||||||||
| 323 | #pod use Moose; | ||||||||||||||||||||||
| 324 | #pod use MooseX::Types::Moose qw(HashRef Int); | ||||||||||||||||||||||
| 325 | #pod | ||||||||||||||||||||||
| 326 | #pod has 'attr' => ( isa => HashRef | Int ); | ||||||||||||||||||||||
| 327 | #pod | ||||||||||||||||||||||
| 328 | #pod And everything should just work as you'd think. | ||||||||||||||||||||||
| 329 | #pod | ||||||||||||||||||||||
| 330 | #pod =head1 METHODS | ||||||||||||||||||||||
| 331 | #pod | ||||||||||||||||||||||
| 332 | #pod =head2 import | ||||||||||||||||||||||
| 333 | #pod | ||||||||||||||||||||||
| 334 | #pod Installs the L<MooseX::Types::Base> class into the caller and exports types | ||||||||||||||||||||||
| 335 | #pod according to the specification described in L</"LIBRARY DEFINITION">. This | ||||||||||||||||||||||
| 336 | #pod will continue to L<Moose::Util::TypeConstraints>' C<import> method to export | ||||||||||||||||||||||
| 337 | #pod helper functions you will need to declare your types. | ||||||||||||||||||||||
| 338 | #pod | ||||||||||||||||||||||
| 339 | #pod =cut | ||||||||||||||||||||||
| 340 | |||||||||||||||||||||||
| 341 | 37 | ▶ | sub import { | ||||||||||||||||||||
|
Subroutine
import: called CC=5 SCAR=16.1
| |||||||||||||||||||||||
| 342 | 37 | my ($class, %args) = @_; | |||||||||||||||||||||
| 343 | 37 | my $caller = caller; | |||||||||||||||||||||
| 344 | |||||||||||||||||||||||
| 345 | # everyone should want this | ||||||||||||||||||||||
| 346 | 37 | strict->import; | |||||||||||||||||||||
| 347 | 37 | warnings->import; | |||||||||||||||||||||
| 348 | |||||||||||||||||||||||
| 349 | # inject base class into new library | ||||||||||||||||||||||
| 350 | 17 | ▶ | { no strict 'refs'; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 351 | 37 | unshift @{ $caller . '::ISA' }, 'MooseX::Types::Base'; | |||||||||||||||||||||
| 352 | } | ||||||||||||||||||||||
| 353 | |||||||||||||||||||||||
| 354 | # generate predeclared type helpers | ||||||||||||||||||||||
| 355 | 37 | ▶ | if (my @orig_declare = @{ $args{ -declare } || [] }) { | ||||||||||||||||||||
|
Branch: 1 branch
Condition100%2/2
$args{'-declare'} || []
MC/DC100%1/1
$args{'-declare'} || []
$args{'-declare'}
Truth table100%2/2
A
A= $args{'-declare'}
| |||||||||||||||||||||||
| 356 | 20 | my ($tags, $declare) = filter_tags @orig_declare; | |||||||||||||||||||||
| 357 | 20 | my @to_export; | |||||||||||||||||||||
| 358 | |||||||||||||||||||||||
| 359 | 20 | for my $type (@$declare) { | |||||||||||||||||||||
| 360 | |||||||||||||||||||||||
| 361 | 57 | ▶ | croak "Cannot create a type containing '::' ($type) at the moment" | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 362 | if $type =~ /::/; | ||||||||||||||||||||||
| 363 | |||||||||||||||||||||||
| 364 | # add type to library and remember to export | ||||||||||||||||||||||
| 365 | 56 | $caller->add_type($type); | |||||||||||||||||||||
| 366 | 56 | push @to_export, $type; | |||||||||||||||||||||
| 367 | } | ||||||||||||||||||||||
| 368 | |||||||||||||||||||||||
| 369 | $caller->import({ | ||||||||||||||||||||||
| 370 | 19 | -full => 1, | |||||||||||||||||||||
| 371 | -into => $caller, | ||||||||||||||||||||||
| 372 | installer => method_installer({ rebless => 1 }), | ||||||||||||||||||||||
| 373 | }, @to_export); | ||||||||||||||||||||||
| 374 | } | ||||||||||||||||||||||
| 375 | |||||||||||||||||||||||
| 376 | # run type constraints import | ||||||||||||||||||||||
| 377 | 36 | Moose::Util::TypeConstraints->import({ into => $caller }); | |||||||||||||||||||||
| 378 | |||||||||||||||||||||||
| 379 | # override some with versions that check for syntax errors | ||||||||||||||||||||||
| 380 | 36 | MooseX::Types::CheckedUtilExports->import({ into => $caller }); | |||||||||||||||||||||
| 381 | |||||||||||||||||||||||
| 382 | 36 | 1; | |||||||||||||||||||||
| 383 | } | ||||||||||||||||||||||
| 384 | |||||||||||||||||||||||
| 385 | #pod =head2 type_export_generator | ||||||||||||||||||||||
| 386 | #pod | ||||||||||||||||||||||
| 387 | #pod Generate a type export, e.g. C<Int()>. This will return either a | ||||||||||||||||||||||
| 388 | #pod L<Moose::Meta::TypeConstraint> object, or alternatively a | ||||||||||||||||||||||
| 389 | #pod L<MooseX::Types::UndefinedType> object if the type was not yet defined. | ||||||||||||||||||||||
| 390 | #pod | ||||||||||||||||||||||
| 391 | #pod =cut | ||||||||||||||||||||||
| 392 | |||||||||||||||||||||||
| 393 | 149 | ▶ | sub type_export_generator { | ||||||||||||||||||||
|
Subroutine
type_export_generator: called CC=1 SCAR=0.3
Pod
documented
| |||||||||||||||||||||||
| 394 | 149 | my ($class, $type, $name) = @_; | |||||||||||||||||||||
| 395 | |||||||||||||||||||||||
| 396 | ## Return an anonymous subroutine that will generate the proxied type | ||||||||||||||||||||||
| 397 | ## constraint for you. | ||||||||||||||||||||||
| 398 | |||||||||||||||||||||||
| 399 | 21 | ▶ | return set_subname "__TYPE__::$name" => sub { | ||||||||||||||||||||
|
Subroutine
Value: called CC=6 SCAR=19.9
Subroutine
Foo2Alias: called CC=6 SCAR=19.9
| |||||||||||||||||||||||
| 400 | 21 | my $type_constraint = $class->create_base_type_constraint($name); | |||||||||||||||||||||
| 401 | |||||||||||||||||||||||
| 402 | 21 | ▶ | if(defined(my $params = shift @_)) { | ||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||
| 403 | ## We currently only allow a TC to accept a single, ArrayRef | ||||||||||||||||||||||
| 404 | ## parameter, as in HashRef[Int], where [Int] is what's inside the | ||||||||||||||||||||||
| 405 | ## ArrayRef passed. | ||||||||||||||||||||||
| 406 | 0 | ▶ | if(reftype $params eq 'ARRAY') { | ||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||
| 407 | 0 | $type_constraint = $class->create_arged_type_constraint($name, @$params); | |||||||||||||||||||||
| 408 | } elsif(!defined $type_constraint) { | ||||||||||||||||||||||
| 409 | 0 | croak "Syntax error in type definition (did you forget a comma" | |||||||||||||||||||||
| 410 | . " after $type?)"; | ||||||||||||||||||||||
| 411 | } else { | ||||||||||||||||||||||
| 412 | 0 | croak "Argument must be an ArrayRef to create a parameterized " | |||||||||||||||||||||
| 413 | . "type, Eg.: ${type}[Int]. Got: ".ref($params)."." | ||||||||||||||||||||||
| 414 | } | ||||||||||||||||||||||
| 415 | } | ||||||||||||||||||||||
| 416 | |||||||||||||||||||||||
| 417 | 21 | ▶ | $type_constraint = defined($type_constraint) ? $type_constraint | ||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||
| 418 | : MooseX::Types::UndefinedType->new($name); | ||||||||||||||||||||||
| 419 | |||||||||||||||||||||||
| 420 | 21 | my $type_decorator = $class->create_type_decorator($type_constraint); | |||||||||||||||||||||
| 421 | |||||||||||||||||||||||
| 422 | ## If there are additional args, that means it's probably stuff that | ||||||||||||||||||||||
| 423 | ## needs to be returned to the subtype. Not an ideal solution here but | ||||||||||||||||||||||
| 424 | ## doesn't seem to cause trouble. | ||||||||||||||||||||||
| 425 | |||||||||||||||||||||||
| 426 | 21 | ▶ | if(@_) { | ||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||
| 427 | 0 | return ($type_decorator, @_); | |||||||||||||||||||||
| 428 | } else { | ||||||||||||||||||||||
| 429 | 21 | return $type_decorator; | |||||||||||||||||||||
| 430 | } | ||||||||||||||||||||||
| 431 | 149 | }; | |||||||||||||||||||||
| 432 | } | ||||||||||||||||||||||
| 433 | |||||||||||||||||||||||
| 434 | #pod =head2 create_arged_type_constraint ($name, @args) | ||||||||||||||||||||||
| 435 | #pod | ||||||||||||||||||||||
| 436 | #pod Given a String $name with @args find the matching type constraint and parameterize | ||||||||||||||||||||||
| 437 | #pod it with @args. | ||||||||||||||||||||||
| 438 | #pod | ||||||||||||||||||||||
| 439 | #pod =cut | ||||||||||||||||||||||
| 440 | |||||||||||||||||||||||
| 441 | 20 | ▶ | sub create_arged_type_constraint { | ||||||||||||||||||||
|
Subroutine
create_arged_type_constraint: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||
| 442 | 20 | my ($class, $name, @args) = @_; | |||||||||||||||||||||
| 443 | 20 | my $type_constraint = Moose::Util::TypeConstraints::find_or_create_type_constraint("$name"); | |||||||||||||||||||||
| 444 | 20 | my $parameterized = $type_constraint->parameterize(@args); | |||||||||||||||||||||
| 445 | # It's obnoxious to have to parameterize before looking for the TC, but the | ||||||||||||||||||||||
| 446 | # alternative is to hard-code the assumption that the name is | ||||||||||||||||||||||
| 447 | # "$name[$args[0]]", which would be worse. | ||||||||||||||||||||||
| 448 | # This breaks MXMS, unfortunately, which relies on things like Tuple[...] | ||||||||||||||||||||||
| 449 | # creating new type objects each time. | ||||||||||||||||||||||
| 450 | # if (my $existing = | ||||||||||||||||||||||
| 451 | # Moose::Util::TypeConstraints::find_type_constraint($parameterized->name)) { | ||||||||||||||||||||||
| 452 | # return $existing; | ||||||||||||||||||||||
| 453 | # } | ||||||||||||||||||||||
| 454 | # Moose::Util::TypeConstraints::register_type_constraint($parameterized); | ||||||||||||||||||||||
| 455 | 20 | return $parameterized; | |||||||||||||||||||||
| 456 | } | ||||||||||||||||||||||
| 457 | |||||||||||||||||||||||
| 458 | #pod =head2 create_base_type_constraint ($name) | ||||||||||||||||||||||
| 459 | #pod | ||||||||||||||||||||||
| 460 | #pod Given a String $name, find the matching type constraint. | ||||||||||||||||||||||
| 461 | #pod | ||||||||||||||||||||||
| 462 | #pod =cut | ||||||||||||||||||||||
| 463 | |||||||||||||||||||||||
| 464 | 250 | ▶ | sub create_base_type_constraint { | ||||||||||||||||||||
|
Subroutine
create_base_type_constraint: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||
| 465 | 250 | my ($class, $name) = @_; | |||||||||||||||||||||
| 466 | 250 | return find_type_constraint($name); | |||||||||||||||||||||
| 467 | } | ||||||||||||||||||||||
| 468 | |||||||||||||||||||||||
| 469 | #pod =head2 create_type_decorator ($type_constraint) | ||||||||||||||||||||||
| 470 | #pod | ||||||||||||||||||||||
| 471 | #pod Given a $type_constraint, return a lightweight L<MooseX::Types::TypeDecorator> | ||||||||||||||||||||||
| 472 | #pod instance. | ||||||||||||||||||||||
| 473 | #pod | ||||||||||||||||||||||
| 474 | #pod =cut | ||||||||||||||||||||||
| 475 | |||||||||||||||||||||||
| 476 | 249 | ▶ | sub create_type_decorator { | ||||||||||||||||||||
|
Subroutine
create_type_decorator: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||
| 477 | 249 | my ($class, $type_constraint) = @_; | |||||||||||||||||||||
| 478 | 249 | return MooseX::Types::TypeDecorator->new($type_constraint); | |||||||||||||||||||||
| 479 | } | ||||||||||||||||||||||
| 480 | |||||||||||||||||||||||
| 481 | #pod =head2 coercion_export_generator | ||||||||||||||||||||||
| 482 | #pod | ||||||||||||||||||||||
| 483 | #pod This generates a coercion handler function, e.g. C<to_Int($value)>. | ||||||||||||||||||||||
| 484 | #pod | ||||||||||||||||||||||
| 485 | #pod =cut | ||||||||||||||||||||||
| 486 | |||||||||||||||||||||||
| 487 | 67 | ▶ | sub coercion_export_generator { | ||||||||||||||||||||
|
Subroutine
coercion_export_generator: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||
| 488 | 67 | my ($class, $type, $full, $undef_msg) = @_; | |||||||||||||||||||||
| 489 | 12 | ▶ | return sub { | ||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||||||||||
| 490 | 12 | my ($value) = @_; | |||||||||||||||||||||
| 491 | |||||||||||||||||||||||
| 492 | # we need a type object | ||||||||||||||||||||||
| 493 | 12 | ▶ | my $tobj = find_type_constraint($full) or croak $undef_msg; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 494 | 12 | my $return = $tobj->coerce($value); | |||||||||||||||||||||
| 495 | |||||||||||||||||||||||
| 496 | # non-successful coercion returns false | ||||||||||||||||||||||
| 497 | 12 | ▶ | return unless $tobj->check($return); | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 498 | |||||||||||||||||||||||
| 499 | 7 | return $return; | |||||||||||||||||||||
| 500 | } | ||||||||||||||||||||||
| 501 | 67 | } | |||||||||||||||||||||
| 502 | |||||||||||||||||||||||
| 503 | #pod =head2 check_export_generator | ||||||||||||||||||||||
| 504 | #pod | ||||||||||||||||||||||
| 505 | #pod Generates a constraint check closure, e.g. C<is_Int($value)>. | ||||||||||||||||||||||
| 506 | #pod | ||||||||||||||||||||||
| 507 | #pod =cut | ||||||||||||||||||||||
| 508 | |||||||||||||||||||||||
| 509 | 149 | ▶ | sub check_export_generator { | ||||||||||||||||||||
|
Subroutine
check_export_generator: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||
| 510 | 149 | my ($class, $type, $full, $undef_msg) = @_; | |||||||||||||||||||||
| 511 | 13 | ▶ | return sub { | ||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.1
| |||||||||||||||||||||||
| 512 | 13 | my ($value) = @_; | |||||||||||||||||||||
| 513 | |||||||||||||||||||||||
| 514 | # we need a type object | ||||||||||||||||||||||
| 515 | 13 | ▶ | my $tobj = find_type_constraint($full) or croak $undef_msg; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 516 | |||||||||||||||||||||||
| 517 | 13 | return $tobj->check($value); | |||||||||||||||||||||
| 518 | } | ||||||||||||||||||||||
| 519 | 149 | } | |||||||||||||||||||||
| 520 | |||||||||||||||||||||||
| 521 | __END__ | ||||||||||||||||||||||