X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FClass%2FMOP%2FMethod%2FConstructor.pm;h=53df29f297dd36be047f5cd0e427a4caa6039606;hb=d7fe25089fe79be9d93445f25c55dfed82b11e7a;hp=aeaa607aeddae2d6a3486f15d25c37a44fd0062d;hpb=0c6f32800dad3cfe10510f2443d2cac4c9a1a76f;p=gitmo%2FClass-MOP.git diff --git a/lib/Class/MOP/Method/Constructor.pm b/lib/Class/MOP/Method/Constructor.pm index aeaa607..53df29f 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.79'; $VERSION = eval $VERSION; our $AUTHORITY = 'cpan:STEVAN'; @@ -101,32 +101,25 @@ 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($_) - } 0 .. (@{$self->attributes} - 1)); + $self->_generate_slot_initializer($_, $close_over) + } @{$self->attributes}); $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 $code = $self->_eval_closure( + $close_over, + $source + ); + confess "Could not eval the constructor :\n\n$source\n\nbecause :\n\n$@" if $@; - $code = $self->_eval_closure( - { '$attrs' => \$self->attributes }, - $source - ); - confess "Could not eval the constructor :\n\n$source\n\nbecause :\n\n$@" if $@; - } return $code; } sub _generate_slot_initializer { my $self = shift; - my $index = shift; - - my $attr = $self->attributes->[$index]; + my $attr = shift; + my $close = shift; my $default; if ($attr->has_default) { @@ -137,7 +130,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; @@ -155,12 +150,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" . '}') ); @@ -168,7 +163,7 @@ sub _generate_slot_initializer { return ( $self->meta_instance->inline_set_slot_value( '$instance', - ("'" . $attr->name . "'"), + $attr->name, $default ) . "\n" ); } else { return '' } @@ -200,58 +195,53 @@ Class::MOP::Method::Constructor - Method Meta Object for constructors =head1 DESCRIPTION -This is a subclass of C which deals with -class constructors. This is used when making a class immutable -to generate an optimized constructor. +This is a subclass of C which generates +constructor methods. =head1 METHODS =over 4 -=item B $meta, options => \%options)> - -=item B +=item B<< Class::MOP::Method::Constructor->new(%options) >> -This returns the options HASH which is passed into C. +This creates a new constructor object. It accepts a hash reference of +options. -=item B +=over 8 -This returns the metaclass which is passed into C. +=item * metaclass -=item B +This should be a L object. It is required. -This returns the list of attributes which are associated with the -metaclass which is passed into C. +=item * name -=item B +The method name (without a package name). This is required. -This returns the meta instance which is associated with the -metaclass which is passed into C. +=item * package_name -=item B +The package name for the method. This is required. -This returns a boolean, but since constructors are very rarely -not inlined, this always returns true for now. +=item * is_inline -=item B +This indicates whether or not the constructor should be inlined. This +defaults to false. -This method always returns true in this class. It exists so that -subclasses (like in Moose) can override and do some sort of checking -to determine whether or not inlining the constructor is safe. +=back -=item B +=item B<< $metamethod->is_inline >> -This creates the code reference for the constructor itself. +Returns a boolean indicating whether or not the constructor is +inlined. -=back +=item B<< $metamethod->associated_metaclass >> -=head2 Method Generators +This returns the L object for the method. -=over 4 +=item B<< $metamethod->can_be_inlined >> -=item B - -=item B +This method always returns true in this class. It exists so that +subclasses (as in Moose) can do some sort of checking to determine +whether or not inlining the constructor is safe. =back @@ -261,7 +251,7 @@ Stevan Little Estevan@iinteractive.comE =head1 COPYRIGHT AND LICENSE -Copyright 2006-2008 by Infinity Interactive, Inc. +Copyright 2006-2009 by Infinity Interactive, Inc. L