X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=gitmo%2FMouse.git;a=blobdiff_plain;f=lib%2FMouse%2FMeta%2FMethod%2FConstructor.pm;h=d68e95858e7dd1753fb127d9744c7c4fa0f1c609;hp=1dd8b3a16a3c96a3adb26398da8190b56360a3fd;hb=420c0e9300dd016b83e1afb25576a877afdbd59d;hpb=3b89ea918d7ff7162f2bbeecf24df818148d5315 diff --git a/lib/Mouse/Meta/Method/Constructor.pm b/lib/Mouse/Meta/Method/Constructor.pm index 1dd8b3a..d68e958 100644 --- a/lib/Mouse/Meta/Method/Constructor.pm +++ b/lib/Mouse/Meta/Method/Constructor.pm @@ -1,9 +1,8 @@ package Mouse::Meta::Method::Constructor; -use strict; -use warnings; +use Mouse::Util; # enables strict and warnings -sub generate_constructor_method_inline { - my ($class, $metaclass) = @_; +sub _generate_constructor_method { + my ($class, $metaclass, $args) = @_; my $associated_metaclass_name = $metaclass->name; my @attrs = $metaclass->get_all_attributes; @@ -12,26 +11,34 @@ sub generate_constructor_method_inline { my $buildargs = $class->_generate_BUILDARGS($metaclass); my $processattrs = $class->_generate_processattrs($metaclass, \@attrs); - my @compiled_constraints = map { $_->_compiled_type_constraint } - map { $_->{type_constraint} ? $_->{type_constraint} : () } @attrs; - - my $code = <<"..."; - sub { - my \$class = shift; - return \$class->Mouse::Object::new(\@_) - if \$class ne q{$associated_metaclass_name}; - $buildargs; - my \$instance = bless {}, \$class; - $processattrs; - $buildall; - return \$instance; - } + my @compiled_constraints = map { $_ ? $_->_compiled_type_constraint : undef } + map { $_->type_constraint } @attrs; + + + + my $source = sprintf("#line %d %s\n", __LINE__, __FILE__).<<"..."; + sub \{ + my \$class = shift; + return \$class->Mouse::Object::new(\@_) + if \$class ne q{$associated_metaclass_name}; + $buildargs; + my \$instance = bless {}, \$class; + $processattrs; + $buildall; + return \$instance; + } ... - local $@; - my $res = eval $code; - die $@ if $@; - $res; + my $code; + my $e = do{ + local $@; + $code = eval $source; + $@; + }; + die $e if $e; + + $metaclass->add_method($args->{constructor_name} => $code); + return; } sub _generate_processattrs { @@ -48,34 +55,28 @@ sub _generate_processattrs { if (defined $attr->init_arg) { my $from = $attr->init_arg; - $code .= "if (exists \$args->{'$from'}) {\n"; + $code .= "if (exists \$args->{q{$from}}) {\n"; - if ($attr->should_coerce && $attr->type_constraint) { - $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"; - } + my $value = "\$args->{q{$from}}"; + if(my $type_constraint = $attr->type_constraint){ + if($attr->should_coerce && $type_constraint->has_coercion){ + $code .= "my \$value = \$attrs[$index]->{type_constraint}->coerce(\$args->{q{$from}});\n"; + $value = '$value'; + } - if ($attr->has_type_constraint) { - $code .= "unless (\$compiled_constraints[$index](\$value)) {"; - $code .= " - \$attrs[$index]->verify_type_constraint_error( - q{$key}, \$value, \$attrs[$index]->type_constraint - ) - } - "; + $code .= "\$compiled_constraints[$index]->($value)\n"; + $code .= " or \$attrs[$index]->verify_type_constraint_error(q{$key}, $value, \$attrs[$index]->{type_constraint});\n"; } - $code .= "\$instance->{q{$key}} = \$value;\n"; + $code .= "\$instance->{q{$key}} = $value;\n"; if ($attr->is_weak_ref) { - $code .= "Scalar::Util::weaken( \$instance->{q{$key}} ) if ref( \$value );\n"; + $code .= "Scalar::Util::weaken( \$instance->{q{$key}} ) if ref($value);\n"; } if ($attr->has_trigger) { $has_triggers++; - $code .= "push \@triggers, [\$attrs[$index]->{trigger}, \$value];\n"; + $code .= "push \@triggers, [\$attrs[$index]->{trigger}, $value];\n"; } $code .= "\n} else {\n"; @@ -89,24 +90,24 @@ 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]->{type_constraint}, "; + $code .= "\$attrs[$index]->_coerce_and_verify("; } - if ($attr->has_builder) { - $code .= "\$instance->$builder"; - } - elsif (ref($default) eq 'CODE') { - $code .= "\$attrs[$index]->{default}->(\$instance)"; - } - elsif (!defined($default)) { - $code .= 'undef'; - } - elsif ($default =~ /^\-?[0-9]+(?:\.[0-9]+)$/) { - $code .= $default; - } - else { - $code .= "'$default'"; - } + if ($attr->has_builder) { + $code .= "\$instance->$builder()"; + } + elsif (ref($default) eq 'CODE') { + $code .= "\$attrs[$index]->{default}->(\$instance)"; + } + elsif (!defined($default)) { + $code .= 'undef'; + } + elsif ($default =~ /^\-?[0-9]+(?:\.[0-9]+)$/) { + $code .= $default; + } + else { + $code .= "'$default'"; + } if ($attr->should_coerce) { $code .= ");\n";