X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FMoose%2FManual%2FTypes.pod;h=1b7d873b72b819712f3c7bfea58129cd0d4a7438;hb=1e62ec3bf60b5f85bc0a567958dda1be8df93e84;hp=3cce8b72c0767a9a2f9225e21e4ecdfa48031814;hpb=8e5dd3fb4d0102f0e91b658540172825831ee49d;p=gitmo%2FMoose.git diff --git a/lib/Moose/Manual/Types.pod b/lib/Moose/Manual/Types.pod index 3cce8b7..1b7d873 100644 --- a/lib/Moose/Manual/Types.pod +++ b/lib/Moose/Manual/Types.pod @@ -1,8 +1,10 @@ -=pod +package Moose::Manual::Types; + +# ABSTRACT: Moose's type system -=head1 NAME +__END__ -Moose::Manual::Types - Moose's type system +=pod =head1 TYPES IN PERL? @@ -164,14 +166,16 @@ A subtype can also define its own constraint failure message. This lets you do things like have an error "The value you provided (20), was not a valid rating, which must be a number from 1-10." This is much friendlier than the default error, which just says that the value -failed a validation check for the type. +failed a validation check for the type. The default error can, however, +be made more friendly by installing L (version 0.14 or +higher), which Moose will use if possible to display the invalid value. Here's a simple (and useful) subtype example: - subtype 'PositiveInt' - => as 'Int' - => where { $_ > 0 } - => message { "The number you provided, $_, was not a positive number" } + subtype 'PositiveInt', + as 'Int', + where { $_ > 0 }, + message { "The number you provided, $_, was not a positive number" }; Note that the sugar functions for working with types are all exported by L. @@ -200,7 +204,7 @@ export your types as perl constants. has 'counter' => (is => 'rw', isa => PositiveInt); This lets you use a short name rather than needing to fully qualify the name -everywhere. It also allows you to write easily create parameterized types: +everywhere. It also allows you to easily create parameterized types: has 'counts' => (is => 'ro', isa => HashRef[PositiveInt]); @@ -211,12 +215,12 @@ robust than the string type parsing for complex cases. A coercion lets you tell Moose to automatically convert one type to another. - subtype 'ArrayRefOfInts' - => as 'ArrayRef[Int]'; + subtype 'ArrayRefOfInts', + as 'ArrayRef[Int]'; - coerce 'ArrayRefOfInts' - => from 'Int' - => via { [ $_ ] }; + coerce 'ArrayRefOfInts', + from 'Int', + via { [ $_ ] }; You'll note that we created a subtype rather than coercing C directly. It's a bad idea to add coercions to the raw built in @@ -247,13 +251,13 @@ object will have C<[ 42 ]> as its C attribute. Deep coercion is the coercion of type parameters for parameterized types. Let's take these types as an example: - subtype 'HexNum' - => as 'Str' - => where { /[a-f0-9]/i }; + subtype 'HexNum', + as 'Str', + where { /[a-f0-9]/i }; - coerce 'Int' - => from 'HexNum' - => via { hex $_ }; + coerce 'Int', + from 'HexNum', + via { hex $_ }; has 'sizes' => ( is => 'ro', @@ -267,15 +271,15 @@ attribute, Moose will not do any coercion. However, you can define a set of subtypes to enable coercion between two parameterized types. - subtype 'ArrayRefOfHexNums' - => as 'ArrayRef[HexNum]'; + subtype 'ArrayRefOfHexNums', + as 'ArrayRef[HexNum]'; - subtype 'ArrayRefOfInts' - => as 'ArrayRef[Int]'; + subtype 'ArrayRefOfInts', + as 'ArrayRef[Int]'; - coerce 'ArrayRefOfInts' - => from 'ArrayRefOfHexNums' - => via { [ map { hex } @{$_} ] }; + coerce 'ArrayRefOfInts', + from 'ArrayRefOfHexNums', + via { [ map { hex } @{$_} ] }; Foo->new( sizes => [ 'a1', 'ff', '22' ] ); @@ -285,9 +289,9 @@ Moose does not attempt to chain coercions, so it will not coerce a single hex number. To do that, we need to define a separate coercion: - coerce 'ArrayRefOfInts' - => from 'HexNum' - => via { [ hex $_ ] }; + coerce 'ArrayRefOfInts', + from 'HexNum', + via { [ hex $_ ] }; Yes, this can all get verbose, but coercion is tricky magic, and we think it's best to make it explicit. @@ -314,9 +318,7 @@ coercion might be a better answer. For our example above, we might want to be more specific, and insist that output be an object with a C method: - subtype 'CanPrint' - => as 'Object' - => where { $_->can('print') }; + duck_type 'CanPrint', [qw(print)]; We can coerce file handles to an object that satisfies this condition with a simple wrapper class: @@ -356,13 +358,13 @@ make your class internals simpler. The L module exports a number of helper functions for creating specific kinds of types. These include -C, C, and C. See the docs for -details. +C, C, C, and C. See the +docs for details. One helper worth noting is C, which allows you to create a subtype of C that only allows the specified values: - enum 'RGB' => qw( red green blue ); + enum 'RGB', [qw( red green blue )]; This creates a type named C. @@ -422,17 +424,4 @@ In order to ameliorate this problem, we recommend defining I of your custom types in one module, C, and then loading this module in all of your other modules. -=head1 AUTHOR - -Dave Rolsky Eautarch@urth.orgE - -=head1 COPYRIGHT AND LICENSE - -Copyright 2009-2010 by Infinity Interactive, Inc. - -L - -This library is free software; you can redistribute it and/or modify -it under the same terms as Perl itself. - =cut