Version 1.05
[gitmo/Moose.git] / lib / Moose / Util / TypeConstraints.pm
index b6fa18e..2818d5a 100644 (file)
@@ -6,7 +6,7 @@ use List::MoreUtils qw( all any );
 use Scalar::Util qw( blessed reftype );
 use Moose::Exporter;
 
-our $VERSION = '0.93';
+our $VERSION = '1.05';
 $VERSION = eval $VERSION;
 our $AUTHORITY = 'cpan:STEVAN';
 
@@ -572,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;
 
@@ -671,9 +672,6 @@ subtype 'Num' => as 'Str' =>
 subtype 'Int' => as 'Num' => where { "$_" =~ /^-?[0-9]+$/ } =>
     optimize_as \&Moose::Util::TypeConstraints::OptimizedConstraints::Int;
 
-subtype 'ScalarRef' => as 'Ref' => where { ref($_) eq 'SCALAR' } =>
-    optimize_as
-    \&Moose::Util::TypeConstraints::OptimizedConstraints::ScalarRef;
 subtype 'CodeRef' => as 'Ref' => where { ref($_) eq 'CODE' } =>
     optimize_as \&Moose::Util::TypeConstraints::OptimizedConstraints::CodeRef;
 subtype 'RegexpRef' => as 'Ref' => where { ref($_) eq 'Regexp' } =>
@@ -716,6 +714,24 @@ subtype 'RoleName' => as 'ClassName' => where {
 
 $REGISTRY->add_type_constraint(
     Moose::Meta::TypeConstraint::Parameterizable->new(
+        name               => 'ScalarRef',
+        package_defined_in => __PACKAGE__,
+        parent             => find_type_constraint('Ref'),
+        constraint         => sub { ref($_) eq 'SCALAR' || ref($_) eq 'REF' },
+        optimized =>
+            \&Moose::Util::TypeConstraints::OptimizedConstraints::ScalarRef,
+        constraint_generator => sub {
+            my $type_parameter = shift;
+            my $check          = $type_parameter->_compiled_type_constraint;
+            return sub {
+                return $check->(${ $_ });
+            };
+        }
+    )
+);
+
+$REGISTRY->add_type_constraint(
+    Moose::Meta::TypeConstraint::Parameterizable->new(
         name               => 'ArrayRef',
         package_defined_in => __PACKAGE__,
         parent             => find_type_constraint('Ref'),
@@ -774,7 +790,7 @@ $REGISTRY->add_type_constraint(
 );
 
 my @PARAMETERIZABLE_TYPES
-    = map { $REGISTRY->get_type_constraint($_) } qw[ArrayRef HashRef Maybe];
+    = map { $REGISTRY->get_type_constraint($_) } qw[ScalarRef ArrayRef HashRef Maybe];
 
 sub get_all_parameterizable_types {@PARAMETERIZABLE_TYPES}
 
@@ -893,7 +909,7 @@ that hierarchy represented visually.
                   ClassName
                   RoleName
           Ref
-              ScalarRef
+              ScalarRef[`a]
               ArrayRef[`a]
               HashRef[`a]
               CodeRef
@@ -907,6 +923,7 @@ parameterized, this means you can say:
 
   ArrayRef[Int]    # an array of integers
   HashRef[CodeRef] # a hash of str to CODE ref mappings
+  ScalarRef[Int]   # a reference to an integer
   Maybe[Str]       # value may be a string, may be undefined
 
 If Moose finds a name in brackets that it does not recognize as an
@@ -984,7 +1001,7 @@ The following functions are used to create type constraints.  They
 will also register the type constraints your create in a global
 registry that is used to look types up by name.
 
-See the L<SYNOPSIS> for an example of how to use these.
+See the L</SYNOPSIS> for an example of how to use these.
 
 =over 4
 
@@ -1056,7 +1073,7 @@ This can be used in an attribute definition like so:
 This will create a basic subtype for a given set of strings.
 The resulting constraint will be a subtype of C<Str> and
 will match any of the items in C<\@values>. It is case sensitive.
-See the L<SYNOPSIS> for a simple example.
+See the L</SYNOPSIS> for a simple example.
 
 B<NOTE:> This is not a true proper enum type, it is simply
 a convenient constraint builder.
@@ -1127,7 +1144,7 @@ The valid hashref keys are C<where>, C<message>, and C<optimize_as>.
 =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.
@@ -1201,7 +1218,7 @@ the type-coercion code first, followed by the type constraint
 check. This feature should be used carefully as it is very powerful
 and could easily take off a limb if you are not careful.
 
-See the L<SYNOPSIS> for an example of how to use these.
+See the L</SYNOPSIS> for an example of how to use these.
 
 =over 4
 
@@ -1355,9 +1372,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<Moose/BUGS> for details on reporting bugs.
 
 =head1 AUTHOR
 
@@ -1365,7 +1380,7 @@ Stevan Little E<lt>stevan@iinteractive.comE<gt>
 
 =head1 COPYRIGHT AND LICENSE
 
-Copyright 2006-2009 by Infinity Interactive, Inc.
+Copyright 2006-2010 by Infinity Interactive, Inc.
 
 L<http://www.iinteractive.com>