X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FMouse%2FMeta%2FMethod%2FConstructor.pm;h=77cced9787de9070d81ef18ffab60193d3e68799;hb=63d74d7adc05bdf93288048a851adaefd6ffc8d3;hp=2de3802411b28c164cb08fa7522b405f0ace87f5;hpb=c12edd9ac9343d9853e4b9ca9f2254336e468ffc;p=gitmo%2FMouse.git diff --git a/lib/Mouse/Meta/Method/Constructor.pm b/lib/Mouse/Meta/Method/Constructor.pm index 2de3802..77cced9 100644 --- a/lib/Mouse/Meta/Method/Constructor.pm +++ b/lib/Mouse/Meta/Method/Constructor.pm @@ -36,52 +36,80 @@ sub _generate_processattrs { my $attr = $attrs->[$index]; my $from = $attr->init_arg; my $key = $attr->name; - my $part1 = do { + + my $set_value = do { my @code; + if ($attr->should_coerce) { - push @code, "\$args->{\$from} = \$attr->coerce_constraint( \$args->{\$from} );"; + push @code, "my \$value = \$attr->coerce_constraint( \$args->{'$from'});"; } + else { + push @code, "my \$value = \$args->{'$from'};"; + } + if ($attr->has_type_constraint) { - push @code, "\$attr->verify_type_constraint( \$args->{\$from} );"; + push @code, "\$attr->verify_type_constraint( \$value );"; } - push @code, "\$instance->{\$key} = \$args->{\$from};"; + + 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( \$value );"; } + if ( $attr->has_trigger ) { - push @code, "\$attr->trigger->( \$instance, \$args->{\$from}, \$attr );"; + push @code, "\$attr->trigger->( \$instance, \$value, \$attr );"; } + join "\n", @code; }; - my $part2 = do { + + my $make_default_value = do { my @code; + if ( $attr->has_default || $attr->has_builder ) { unless ( $attr->is_lazy ) { my $default = $attr->default; my $builder = $attr->builder; - if ($attr->has_builder) { - push @code, "my \$value = \$instance->$builder;"; - } elsif (ref($default) eq 'CODE') { - push @code, "my \$value = \$attr->default()->();"; - } else { - push @code, "my \$value = \$attr->default();"; + + push @code, "my \$value = "; + + if ($attr->should_coerce) { + push @code, "\$attr->coerce_constraint("; } + + if ($attr->has_builder) { + push @code, "\$instance->$builder"; + } + elsif (ref($default) eq 'CODE') { + push @code, "\$attr->default()->()"; + } + else { + push @code, "\$attr->default()"; + } + if ($attr->should_coerce) { - push @code, "\$value = \$attr->coerce_constraint(\$value);"; + push @code, ");"; + } + else { + push @code, ";"; } + 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( \$value );"; } } join "\n", @code; } else { if ( $attr->is_required ) { - q{Carp::confess("Attribute (} . $attr->name . q{) is required");}; + qq{Carp::confess("Attribute ($key) is required");}; } else { "" } @@ -90,12 +118,10 @@ sub _generate_processattrs { my $code = <<"..."; { my \$attr = \$attrs[$index]; - my \$from = '$from'; - my \$key = '$key'; - if (defined(\$from) && exists(\$args->{\$from})) { - $part1; + if (exists(\$args->{'$from'})) { + $set_value; } else { - $part2; + $make_default_value; } } ... @@ -132,9 +158,9 @@ sub _generate_BUILDALL { push @code, q{no strict 'refs';}; push @code, q{no warnings 'once';}; no strict 'refs'; - for my $class ($meta->linearized_isa) { - if (*{ $class . '::BUILD' }{CODE}) { - push @code, qq{${class}::BUILD->(\$instance, \$args);}; + for my $klass ($meta->linearized_isa) { + if (*{ $klass . '::BUILD' }{CODE}) { + push @code, qq{${klass}::BUILD(\$instance, \$args);}; } } return join "\n", @code;