X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FMoose%2FUtil%2FTypeConstraints.pm;h=06733dfbc70b529e6a1239fe4caf3897ef4d5e81;hb=2ae1457eead0263b687bd59cd3630b893618e551;hp=d073ff7669b2a1ad4fb5328fd54ddb888dccac1c;hpb=1a15f4a82cb59277a8f6e681079e2e0c8c729b5f;p=gitmo%2FMoose.git diff --git a/lib/Moose/Util/TypeConstraints.pm b/lib/Moose/Util/TypeConstraints.pm index d073ff7..06733df 100644 --- a/lib/Moose/Util/TypeConstraints.pm +++ b/lib/Moose/Util/TypeConstraints.pm @@ -6,7 +6,7 @@ use List::MoreUtils qw( all any ); use Scalar::Util qw( blessed reftype ); use Moose::Exporter; -our $VERSION = '0.92'; +our $VERSION = '0.95'; $VERSION = eval $VERSION; our $AUTHORITY = 'cpan:STEVAN'; @@ -49,7 +49,6 @@ Moose::Exporter->setup_import_methods( register_type_constraint match_on_type ) ], - _export_to_main => 1, ); ## -------------------------------------------------------- @@ -366,6 +365,9 @@ sub duck_type { @methods = @$type_name; $type_name = undef; } + if ( @methods == 1 && ref $methods[0] eq 'ARRAY' ) { + @methods = @{ $methods[0] }; + } register_type_constraint( create_duck_type_constraint( @@ -412,6 +414,9 @@ sub enum { @values = @$type_name; $type_name = undef; } + if ( @values == 1 && ref $values[0] eq 'ARRAY' ) { + @values = @{ $values[0] }; + } ( scalar @values >= 2 ) || __PACKAGE__->_throw_error( "You must have at least two values to enumerate through"); @@ -567,18 +572,19 @@ sub _install_type_coercions ($$) { use re "eval"; my $valid_chars = qr{[\w:\.]}; - my $type_atom = qr{ $valid_chars+ }; + my $type_atom = qr{ (?>$valid_chars+) }x; + my $ws = qr{ (?>\s*) }x; my $any; - my $type = qr{ $valid_chars+ (?: \[ \s* (??{$any}) \s* \] )? }x; + my $type = qr{ $type_atom (?: \[ $ws (??{$any}) $ws \] )? }x; my $type_capture_parts - = qr{ ($valid_chars+) (?: \[ \s* ((??{$any})) \s* \] )? }x; + = qr{ ($type_atom) (?: \[ $ws ((??{$any})) $ws \] )? }x; my $type_with_parameter - = qr{ $valid_chars+ \[ \s* (??{$any}) \s* \] }x; + = qr{ $type_atom \[ $ws (??{$any}) $ws \] }x; - my $op_union = qr{ \s* \| \s* }x; - my $union = qr{ $type (?: $op_union $type )+ }x; + my $op_union = qr{ $ws \| $ws }x; + my $union = qr{ $type (?> (?: $op_union $type )+ ) }x; $any = qr{ $type | $union }x; @@ -656,7 +662,7 @@ subtype 'Value' => as 'Defined' => where { !ref($_) } => subtype 'Ref' => as 'Defined' => where { ref($_) } => optimize_as \&Moose::Util::TypeConstraints::OptimizedConstraints::Ref; -subtype 'Str' => as 'Value' => where {1} => +subtype 'Str' => as 'Value' => where { ref(\$_) eq 'SCALAR' } => optimize_as \&Moose::Util::TypeConstraints::OptimizedConstraints::Str; subtype 'Num' => as 'Str' => @@ -882,9 +888,9 @@ that hierarchy represented visually. Undef Defined Value - Num - Int Str + Num + Int ClassName RoleName Ref @@ -1026,10 +1032,10 @@ metaclass L. Creates a type constraint for either C or something of the given type. -=item B +=item B This will create a subtype of Object and test to make sure the value -C do the methods in C<@methods>. +C do the methods in C<\@methods>. This is intended as an easy way to accept non-Moose objects that provide a certain interface. If you're using Moose classes, we @@ -1037,20 +1043,20 @@ recommend that you use a C-only Role instead. =item B -If passed an ARRAY reference instead of the C<$name>, C<@methods> -pair, this will create an unnamed duck type. This can be used in an -attribute definition like so: +If passed an ARRAY reference as the only parameter instead of the +C<$name>, C<\@methods> pair, this will create an unnamed duck type. +This can be used in an attribute definition like so: has 'cache' => ( is => 'ro', isa => duck_type( [qw( get_set )] ), ); -=item B +=item B This will create a basic subtype for a given set of strings. The resulting constraint will be a subtype of C and -will match any of the items in C<@values>. It is case sensitive. +will match any of the items in C<\@values>. It is case sensitive. See the L for a simple example. B This is not a true proper enum type, it is simply @@ -1058,9 +1064,9 @@ a convenient constraint builder. =item B -If passed an ARRAY reference instead of the C<$name>, C<@values> pair, -this will create an unnamed enum. This can then be used in an attribute -definition like so: +If passed an ARRAY reference as the only parameter instead of the +C<$name>, C<\@values> pair, this will create an unnamed enum. This +can then be used in an attribute definition like so: has 'sort_order' => ( is => 'ro', @@ -1122,7 +1128,7 @@ The valid hashref keys are C, C, and C. =item B<< match_on_type $value => ( $type => \&action, ... ?\&default ) >> This is a utility function for doing simple type based dispatching similar to -match/case in O'Caml and case/of in Haskell. It is not as featureful as those +match/case in OCaml and case/of in Haskell. It is not as featureful as those languages, nor does not it support any kind of automatic destructuring bind. Here is a simple Perl pretty printer dispatching over the core Moose types. @@ -1350,9 +1356,7 @@ Adds C<$type> to the list of parameterizable types =head1 BUGS -All complex software has bugs lurking in it, and this module is no -exception. If you find a bug please either email me, or add the bug -to cpan-RT. +See L for details on reporting bugs. =head1 AUTHOR @@ -1360,7 +1364,7 @@ Stevan Little Estevan@iinteractive.comE =head1 COPYRIGHT AND LICENSE -Copyright 2006-2009 by Infinity Interactive, Inc. +Copyright 2006-2010 by Infinity Interactive, Inc. L