$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" . '}';
# to be picked up in the eval
$code = $self->_eval_closure(
- { '$attrs' => \$self->attributes },
+ $close_over,
$source
);
confess "Could not eval the constructor :\n\n$source\n\nbecause :\n\n$@" if $@;
sub _generate_slot_initializer {
my $self = shift;
my $index = shift;
+ my $close = shift;
my $attr = $self->attributes->[$index];
# 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;