more optimized of TypeRegistry
大沢 和宏 [Fri, 5 Dec 2008 00:22:40 +0000 (00:22 +0000)]
lib/Mouse/TypeRegistry.pm
t/800_shikabased/009-overwrite-builtin-subtype.t [new file with mode: 0644]

index b5d069c..c722ec1 100644 (file)
@@ -47,7 +47,7 @@ my $optimized_constraints;
 my $optimized_constraints_base;
 {
     no warnings 'uninitialized';
-    $optimized_constraints = $optimized_constraints_base = {
+    $SUBTYPE = {
         Any        => sub { 1 },
         Item       => sub { 1 },
         Bool       => sub {
@@ -79,7 +79,12 @@ my $optimized_constraints_base;
 
         Object     => sub { blessed($_) && blessed($_) ne 'Regexp' },
     };
+
+    sub optimized_constraints { $SUBTYPE }
+    my @SUBTYPE_KEYS = keys %{ $SUBTYPE };
+    sub list_all_builtin_type_constraints { @SUBTYPE_KEYS }
 }
+
 sub _subtype {
     my $pkg = caller(0);
     my($name, %conf) = @_;
@@ -90,7 +95,6 @@ sub _subtype {
     my $stuff = $conf{where} || optimized_constraints()->{$as};
 
     $SUBTYPE->{$name} = $stuff;
-    $optimized_constraints = +{ %{ $SUBTYPE }, %{ $optimized_constraints_base } };
 }
 
 sub _coerce {
@@ -150,12 +154,6 @@ sub typecast_constraints {
     return $value;
 }
 
-sub optimized_constraints { $optimized_constraints }
-{
-    my @optimized_constraints_keys = keys %{ $optimized_constraints };
-    sub list_all_builtin_type_constraints { @optimized_constraints_keys }
-}
-
 1;
 
 __END__
diff --git a/t/800_shikabased/009-overwrite-builtin-subtype.t b/t/800_shikabased/009-overwrite-builtin-subtype.t
new file mode 100644 (file)
index 0000000..9bdd60c
--- /dev/null
@@ -0,0 +1,11 @@
+use strict;
+use warnings;
+use Test::More tests => 1;
+
+eval {
+    package Request;
+    use Mouse::TypeRegistry;
+
+    subtype 'Int' => where { 1};
+};
+like $@, qr/The type constraint 'Int' has already been created, cannot be created again in Request/;