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=ea8af0ffbaa8b11a4d9544ec6231aa0dcfcee6da;hpb=86b99892e77319cf2f046f4563e0717f4005851d;p=gitmo%2FMouse.git diff --git a/lib/Mouse/Meta/Method/Constructor.pm b/lib/Mouse/Meta/Method/Constructor.pm index ea8af0f..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; @@ -49,11 +51,17 @@ sub _generate_processattrs { } if ($attr->has_type_constraint) { - $code .= "{ - unless (\$attrs[$index]->{type_constraint}->check(\$value)) { - \$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"; @@ -135,20 +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 ) { ( ref( $_[0] ) eq 'HASH' ) || Carp::confess "Single parameters to new() must be a HASH ref"; - +{ %{ $_[0] } }; + $args = +{ %{ $_[0] } }; } else { - +{@_}; + $args = +{@_}; } - }; ... }