X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FClass%2FMOP%2FMethod%2FConstructor.pm;h=9d4a88c806b3a92f1d70248ff5ddd60c09cacf65;hb=e9a19694e92766a8d4de61fe2a576929748424c1;hp=f14c0354996f0bb0bde8bba487f6388f7ddc9b11;hpb=f0de47d9b033b6b5175454e7fb3fb43c4ef5ed2e;p=gitmo%2FClass-MOP.git diff --git a/lib/Class/MOP/Method/Constructor.pm b/lib/Class/MOP/Method/Constructor.pm index f14c035..9d4a88c 100644 --- a/lib/Class/MOP/Method/Constructor.pm +++ b/lib/Class/MOP/Method/Constructor.pm @@ -7,7 +7,7 @@ use warnings; use Carp 'confess'; use Scalar::Util 'blessed', 'weaken', 'looks_like_number'; -our $VERSION = '0.71_01'; +our $VERSION = '0.77'; $VERSION = eval $VERSION; our $AUTHORITY = 'cpan:STEVAN'; @@ -89,6 +89,8 @@ sub generate_constructor_method { sub generate_constructor_method_inline { my $self = shift; + my $close_over = {}; + my $source = 'sub {'; $source .= "\n" . 'my $class = shift;'; @@ -99,28 +101,22 @@ sub generate_constructor_method_inline { $source .= "\n" . 'my $instance = ' . $self->meta_instance->inline_create_instance('$class'); $source .= ";\n" . (join ";\n" => map { - $self->_generate_slot_initializer($_) + $self->_generate_slot_initializer($_, $close_over) } 0 .. (@{$self->attributes} - 1)); $source .= ";\n" . 'return $instance'; $source .= ";\n" . '}'; warn $source if $self->options->{debug}; - my $code; - { - # NOTE: - # create the nessecary lexicals - # to be picked up in the eval - my $attrs = $self->attributes; - - $code = eval $source; - confess "Could not eval the constructor :\n\n$source\n\nbecause :\n\n$@" if $@; - } - return $code; + return $self->_eval_closure( + $close_over, + $source + ); } sub _generate_slot_initializer { my $self = shift; my $index = shift; + my $close = shift; my $attr = $self->attributes->[$index]; @@ -133,7 +129,9 @@ sub _generate_slot_initializer { # in which case we can just deal with them # in the code we eval. if ($attr->is_default_a_coderef) { - $default = '$attrs->[' . $index . ']->default($instance)'; + my $idx = @{$close->{'@defaults'}||=[]}; + push(@{$close->{'@defaults'}}, $attr->default); + $default = '$defaults[' . $idx . ']->($instance)'; } else { $default = $attr->default; @@ -151,12 +149,12 @@ sub _generate_slot_initializer { 'if(exists $params->{\'' . $attr->init_arg . '\'}){' . "\n" . $self->meta_instance->inline_set_slot_value( '$instance', - ("'" . $attr->name . "'"), + $attr->name, '$params->{\'' . $attr->init_arg . '\'}' ) . "\n" . '} ' . (!defined $default ? '' : 'else {' . "\n" . $self->meta_instance->inline_set_slot_value( '$instance', - ("'" . $attr->name . "'"), + $attr->name, $default ) . "\n" . '}') ); @@ -164,7 +162,7 @@ sub _generate_slot_initializer { return ( $self->meta_instance->inline_set_slot_value( '$instance', - ("'" . $attr->name . "'"), + $attr->name, $default ) . "\n" ); } else { return '' }