X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FMouse%2FMeta%2FMethod%2FConstructor.pm;h=37e2505e1da5e4283f60ca4714535972397b61f8;hb=53d4053e1f62161dd56e6adf8158eb7df72d1af0;hp=8b3f3a17f905f8e2b085715f02f4b49fd4d61294;hpb=88b6c018ca804f12a431dacf12bb52d0df169e4c;p=gitmo%2FMouse.git diff --git a/lib/Mouse/Meta/Method/Constructor.pm b/lib/Mouse/Meta/Method/Constructor.pm index 8b3f3a1..37e2505 100644 --- a/lib/Mouse/Meta/Method/Constructor.pm +++ b/lib/Mouse/Meta/Method/Constructor.pm @@ -9,11 +9,12 @@ 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 { my \$class = shift; - my \$args = $buildargs; + $buildargs; my \$instance = bless {}, \$class; $processattrs; $buildall; @@ -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) } }"; @@ -137,25 +143,19 @@ sub _generate_BUILDARGS { my $meta = shift; if ($meta->name->can('BUILDARGS') && $meta->name->can('BUILDARGS') != Mouse::Object->can('BUILDARGS')) { - return '$class->BUILDARGS(@_)'; + return 'my $args = $class->BUILDARGS(@_)'; } return <<'...'; - do { + my $args; if ( scalar @_ == 1 ) { - if ( defined $_[0] ) { - ( ref( $_[0] ) eq 'HASH' ) + ( ref( $_[0] ) eq 'HASH' ) || Carp::confess "Single parameters to new() must be a HASH ref"; - +{ %{ $_[0] } }; - } - else { - +{}; - } + $args = +{ %{ $_[0] } }; } else { - +{@_}; + $args = +{@_}; } - }; ... }