X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FMouse%2FMeta%2FMethod%2FConstructor.pm;h=32baa0011f9f17683e628cb3c4b520a47a79f317;hb=8dcbf65da69b42871e448d45e23f25ef9e91d364;hp=1786d4e6fae746a3300900497cb20b55127809df;hpb=fc1d8369f17d2d6a06ecdcb13199e1d4ecb2e53f;p=gitmo%2FMouse.git diff --git a/lib/Mouse/Meta/Method/Constructor.pm b/lib/Mouse/Meta/Method/Constructor.pm index 1786d4e..32baa00 100644 --- a/lib/Mouse/Meta/Method/Constructor.pm +++ b/lib/Mouse/Meta/Method/Constructor.pm @@ -3,34 +3,37 @@ use strict; use warnings; sub generate_constructor_method_inline { - my ($class, $meta) = @_; + my ($class, $meta) = @_; + + my @attrs = $meta->compute_all_applicable_attributes; # this one is using by evaled code my $buildall = $class->_generate_BUILDALL($meta); my $buildargs = $class->_generate_BUILDARGS(); - my $classname = $meta->name; - my $processattrs = $class->_generate_processattrs($meta); + my $processattrs = $class->_generate_processattrs($meta, \@attrs); my $code = <<"..."; sub { my \$class = shift; my \$args = $buildargs; - my \$instance = bless {}, '$classname'; + my \$instance = bless {}, \$class; $processattrs; $buildall; return \$instance; } ... + warn $code if $ENV{DEBUG}; + local $@; my $res = eval $code; die $@ if $@; $res; } sub _generate_processattrs { - my ($class, $meta, ) = @_; - my @attrs = $meta->compute_all_applicable_attributes; + my ($class, $meta, $attrs) = @_; my @res; - for my $attr (@attrs) { + for my $index (0..scalar(@$attrs)-1) { + my $attr = $attrs->[$index]; my $from = $attr->init_arg; my $key = $attr->name; my $part1 = do { @@ -41,8 +44,10 @@ sub _generate_processattrs { if ($attr->has_type_constraint) { push @code, "\$attr->verify_type_constraint( \$args->{\$from} );"; } - push @code, "\$instance->{\$key} = \$args->{\$from};"; - push @code, "weaken( \$instance->{\$key} ) if ref( \$instance->{\$key} ) && \$attr->is_weak_ref;"; + push @code, "\$instance->{'$key'} = \$args->{\$from};"; + if ($attr->is_weak_ref) { + push @code, "weaken( \$instance->{'$key'} ) if ref( \$instance->{'$key'} );"; + } if ( $attr->has_trigger ) { push @code, "\$attr->trigger->( \$instance, \$args->{\$from}, \$attr );"; } @@ -67,9 +72,9 @@ sub _generate_processattrs { if ($attr->has_type_constraint) { push @code, "\$attr->verify_type_constraint(\$value);"; } - push @code, "\$instance->{\$key} = \$value;"; + push @code, "\$instance->{'$key'} = \$value;"; if ($attr->is_weak_ref) { - push @code, "weaken( \$instance->{\$key} ) if ref( \$instance->{\$key} );"; + push @code, "weaken( \$instance->{'$key'} ) if ref( \$instance->{'$key'} );"; } } join "\n", @code; @@ -84,9 +89,8 @@ sub _generate_processattrs { }; my $code = <<"..."; { - my \$attr = \$instance->meta->get_attribute_map->{'$key'}; + my \$attr = \$attrs[$index]; my \$from = '$from'; - my \$key = '$key'; if (defined(\$from) && exists(\$args->{\$from})) { $part1; } else {