X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FMouse%2FMeta%2FMethod%2FConstructor.pm;h=b899026d842f534871dc095941e673cc2c843423;hb=3fee6f680e64b907395238b4d7485e600fee4e46;hp=dfbeb2934348975b3c7b990d826ca48852821f85;hpb=c9aefe2628c559c55abe2ddc8de6c92a5d9dbbaf;p=gitmo%2FMouse.git diff --git a/lib/Mouse/Meta/Method/Constructor.pm b/lib/Mouse/Meta/Method/Constructor.pm index dfbeb29..b899026 100644 --- a/lib/Mouse/Meta/Method/Constructor.pm +++ b/lib/Mouse/Meta/Method/Constructor.pm @@ -9,6 +9,7 @@ sub generate_constructor_method_inline { my $buildall = $class->_generate_BUILDALL($meta); my $buildargs = $class->_generate_BUILDARGS($meta); my $processattrs = $class->_generate_processattrs($meta, \@attrs); + my @compiled_constraints = map { $_ ? $_->{_compiled_type_constraint} : undef } map { $_->{type_constraint} } @attrs; my $code = <<"..."; sub { @@ -22,6 +23,7 @@ sub generate_constructor_method_inline { ... local $@; + # warn $code; my $res = eval $code; die $@ if $@; $res; @@ -42,19 +44,24 @@ sub _generate_processattrs { $code .= "if (exists \$args->{'$from'}) {\n"; if ($attr->should_coerce && $attr->type_constraint) { - $code .= "my \$value = Mouse::Util::TypeConstraints->typecast_constraints('".$attr->associated_class->name."', \$attrs[$index]->{find_type_constraint}, \$attrs[$index]->{type_constraint}, \$args->{'$from'});\n"; + $code .= "my \$value = Mouse::Util::TypeConstraints->typecast_constraints('".$attr->associated_class->name."', \$attrs[$index]->{type_constraint}, \$args->{'$from'});\n"; } else { $code .= "my \$value = \$args->{'$from'};\n"; } if ($attr->has_type_constraint) { - $code .= "{ - local \$_ = \$value; - unless (\$attrs[$index]->{find_type_constraint}->(\$_)) { - \$attrs[$index]->verify_type_constraint_error('$key', \$_, \$attrs[$index]->type_constraint) + if ($attr->type_constraint->{_compiled_type_constraint}) { + $code .= "unless (\$compiled_constraints[$index](\$value)) {"; + } else { + $code .= "unless (\$attrs[$index]->{type_constraint}->check(\$value)) {"; + } + $code .= " + \$attrs[$index]->verify_type_constraint_error( + '$key', \$_, \$attrs[$index]->type_constraint + ) } - }"; + "; } $code .= "\$instance->{'$key'} = \$value;\n"; @@ -78,7 +85,7 @@ sub _generate_processattrs { $code .= "my \$value = "; if ($attr->should_coerce && $attr->type_constraint) { - $code .= "Mouse::Util::TypeConstraints->typecast_constraints('".$attr->associated_class->name."', \$attrs[$index]->{find_type_constraint}, \$attrs[$index]->{type_constraint}, "; + $code .= "Mouse::Util::TypeConstraints->typecast_constraints('".$attr->associated_class->name."', \$attrs[$index]->{type_constraint}, "; } if ($attr->has_builder) { @@ -106,8 +113,7 @@ sub _generate_processattrs { if ($attr->has_type_constraint) { $code .= "{ - local \$_ = \$value; - unless (\$attrs[$index]->{find_type_constraint}->(\$_)) { + unless (\$attrs[$index]->{type_constraint}->check(\$value)) { \$attrs[$index]->verify_type_constraint_error('$key', \$_, \$attrs[$index]->type_constraint) } }";