X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FClass%2FMOP%2FMethod%2FConstructor.pm;h=3e18a0ed89a22c4203ccbe3b0cdc2df085206562;hb=94278c1ba6283af20c09a6aef615954825d08162;hp=a05ebacf7a730d60fe4a718dea233880c2ad13b3;hpb=69e3ab0a5a391925610bbb917d81da8d53fd1b91;p=gitmo%2FClass-MOP.git diff --git a/lib/Class/MOP/Method/Constructor.pm b/lib/Class/MOP/Method/Constructor.pm index a05ebac..3e18a0e 100644 --- a/lib/Class/MOP/Method/Constructor.pm +++ b/lib/Class/MOP/Method/Constructor.pm @@ -7,7 +7,8 @@ use warnings; use Carp 'confess'; use Scalar::Util 'blessed', 'weaken', 'looks_like_number'; -our $VERSION = '0.02'; +our $VERSION = '0.65'; +$VERSION = eval $VERSION; our $AUTHORITY = 'cpan:STEVAN'; use base 'Class::MOP::Method::Generated'; @@ -20,40 +21,52 @@ sub new { || confess "You must pass a metaclass instance if you want to inline" if $options{is_inline}; - my $self = bless { - # from our superclass - '&!body' => undef, - # specific to this subclass - '%!options' => $options{options} || {}, - '$!associated_metaclass' => $options{metaclass}, - '$!is_inline' => ($options{is_inline} || 0), - } => $class; + ($options{package_name} && $options{name}) + || confess "You must supply the package_name and name parameters $Class::MOP::Method::UPGRADE_ERROR_TEXT"; + + my $self = $class->_new(\%options); # we don't want this creating # a cycle in the code, if not # needed - weaken($self->{'$!associated_metaclass'}); + weaken($self->{'associated_metaclass'}); $self->initialize_body; return $self; } +sub _new { + my $class = shift; + my $options = @_ == 1 ? $_[0] : {@_}; + + bless { + # from our superclass + 'body' => undef, + 'package_name' => $options->{package_name}, + 'name' => $options->{name}, + # specific to this subclass + 'options' => $options->{options} || {}, + 'associated_metaclass' => $options->{metaclass}, + 'is_inline' => ($options->{is_inline} || 0), + }, $class; +} + ## accessors -sub options { (shift)->{'%!options'} } -sub associated_metaclass { (shift)->{'$!associated_metaclass'} } +sub options { (shift)->{'options'} } +sub associated_metaclass { (shift)->{'associated_metaclass'} } ## cached values ... sub meta_instance { my $self = shift; - $self->{'$!meta_instance'} ||= $self->associated_metaclass->get_meta_instance; + $self->{'meta_instance'} ||= $self->associated_metaclass->get_meta_instance; } sub attributes { my $self = shift; - $self->{'@!attributes'} ||= [ $self->associated_metaclass->compute_all_applicable_attributes ] + $self->{'attributes'} ||= [ $self->associated_metaclass->compute_all_applicable_attributes ] } ## method @@ -64,22 +77,24 @@ sub initialize_body { $method_name .= '_inline' if $self->is_inline; - $self->{'&!body'} = $self->$method_name; + $self->{'body'} = $self->$method_name; } sub generate_constructor_method { - return sub { (shift)->meta->new_object(@_) } + return sub { Class::MOP::Class->initialize(shift)->new_object(@_) } } sub generate_constructor_method_inline { my $self = shift; my $source = 'sub {'; - $source .= "\n" . 'my ($class, %params) = @_;'; + $source .= "\n" . 'my $class = shift;'; - $source .= "\n" . 'return $class->meta->new_object(%params)'; + $source .= "\n" . 'return Class::MOP::Class->initialize($class)->new_object(@_)'; $source .= "\n" . ' if $class ne \'' . $self->associated_metaclass->name . '\';'; + $source .= "\n" . 'my $params = @_ == 1 ? $_[0] : {@_};'; + $source .= "\n" . 'my $instance = ' . $self->meta_instance->inline_create_instance('$class'); $source .= ";\n" . (join ";\n" => map { $self->_generate_slot_initializer($_) @@ -129,23 +144,32 @@ sub _generate_slot_initializer { $default = '$instance->'.$attr->builder; } - 'if(exists $params{\'' . $attr->init_arg . '\'}){' . "\n" . - $self->meta_instance->inline_set_slot_value( - '$instance', - ("'" . $attr->name . "'"), - '$params{\'' . $attr->init_arg . '\'}' ) . "\n" . - '} ' . (!defined $default ? '' : 'else {' . "\n" . - $self->meta_instance->inline_set_slot_value( - '$instance', - ("'" . $attr->name . "'"), - $default ) . "\n" . - '}'); + if ( defined $attr->init_arg ) { + return ( + 'if(exists $params->{\'' . $attr->init_arg . '\'}){' . "\n" . + $self->meta_instance->inline_set_slot_value( + '$instance', + ("'" . $attr->name . "'"), + '$params->{\'' . $attr->init_arg . '\'}' ) . "\n" . + '} ' . (!defined $default ? '' : 'else {' . "\n" . + $self->meta_instance->inline_set_slot_value( + '$instance', + ("'" . $attr->name . "'"), + $default ) . "\n" . + '}') + ); + } elsif ( defined $default ) { + return ( + $self->meta_instance->inline_set_slot_value( + '$instance', + ("'" . $attr->name . "'"), + $default ) . "\n" + ); + } else { return '' } } 1; -1; - __END__ =pod @@ -171,7 +195,8 @@ Class::MOP::Method::Constructor - Method Meta Object for constructors =head1 DESCRIPTION This is a subclass of C which deals with -class constructors. +class constructors. This is used when making a class immutable +to generate an optimized constructor. =head1 METHODS @@ -208,7 +233,7 @@ This creates the code reference for the constructor itself. =back -=head2 Method Generators +=head2 Method Generators =over 4