X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FClass%2FMOP%2FMethod%2FAccessor.pm;h=08bcbd179197fc7796ad3d1b669b80bb8a98ebd4;hb=5efa6a46982d17e1ff642e8b97673c6618fa7e6d;hp=d1a5403d45b15c130382320f27c60613cd735eb7;hpb=436d2a84bd01a0a9798178c57d89e5edbc9b5f02;p=gitmo%2FClass-MOP.git diff --git a/lib/Class/MOP/Method/Accessor.pm b/lib/Class/MOP/Method/Accessor.pm index d1a5403..08bcbd1 100644 --- a/lib/Class/MOP/Method/Accessor.pm +++ b/lib/Class/MOP/Method/Accessor.pm @@ -6,8 +6,9 @@ use warnings; use Carp 'confess'; use Scalar::Util 'blessed', 'weaken'; +use Try::Tiny; -our $VERSION = '0.93'; +our $VERSION = '1.11'; $VERSION = eval $VERSION; our $AUTHORITY = 'cpan:STEVAN'; @@ -130,89 +131,95 @@ sub _generate_clearer_method { ## Inline methods sub _generate_accessor_method_inline { - my $self = shift; - my $attr = $self->associated_attribute; - my $attr_name = $attr->name; - my $meta_instance = $attr->associated_class->instance_metaclass; - - my ( $code, $e ) = $self->_eval_closure( - {}, - 'sub {' - . $meta_instance->inline_set_slot_value('$_[0]', $attr_name, '$_[1]') - . ' if scalar(@_) == 2; ' - . $meta_instance->inline_get_slot_value('$_[0]', $attr_name) - . '}' - ); - confess "Could not generate inline accessor because : $e" if $e; + my $self = shift; + my $attr = $self->associated_attribute; + + my $code = try { + $self->_compile_code([ + 'sub {', + $attr->inline_set('$_[0]', '$_[1]'), + 'if scalar(@_) == 2;', + $attr->inline_get('$_[0]') . ';', + '}', + ]); + } + catch { + confess "Could not generate inline accessor because : $_"; + }; return $code; } sub _generate_reader_method_inline { - my $self = shift; - my $attr = $self->associated_attribute; - my $attr_name = $attr->name; - my $meta_instance = $attr->associated_class->instance_metaclass; - - my ( $code, $e ) = $self->_eval_closure( - {}, - 'sub {' - . 'confess "Cannot assign a value to a read-only accessor" if @_ > 1;' - . $meta_instance->inline_get_slot_value('$_[0]', $attr_name) - . '}' - ); - confess "Could not generate inline reader because : $e" if $e; + my $self = shift; + my $attr = $self->associated_attribute; + + my $code = try { + $self->_compile_code([ + 'sub {', + 'confess "Cannot assign a value to a read-only accessor"', + 'if @_ > 1;', + $attr->inline_get('$_[0]') . ';', + '}', + ]); + } + catch { + confess "Could not generate inline reader because : $_"; + }; return $code; } sub _generate_writer_method_inline { - my $self = shift; - my $attr = $self->associated_attribute; - my $attr_name = $attr->name; - my $meta_instance = $attr->associated_class->instance_metaclass; - - my ( $code, $e ) = $self->_eval_closure( - {}, - 'sub {' - . $meta_instance->inline_set_slot_value('$_[0]', $attr_name, '$_[1]') - . '}' - ); - confess "Could not generate inline writer because : $e" if $e; + my $self = shift; + my $attr = $self->associated_attribute; + + my $code = try { + $self->_compile_code([ + 'sub {', + $attr->inline_set('$_[0]', '$_[1]') . ';', + '}', + ]); + } + catch { + confess "Could not generate inline writer because : $_"; + }; return $code; } sub _generate_predicate_method_inline { - my $self = shift; - my $attr = $self->associated_attribute; - my $attr_name = $attr->name; - my $meta_instance = $attr->associated_class->instance_metaclass; - - my ( $code, $e ) = $self->_eval_closure( - {}, - 'sub {' - . $meta_instance->inline_is_slot_initialized('$_[0]', $attr_name) - . '}' - ); - confess "Could not generate inline predicate because : $e" if $e; + my $self = shift; + my $attr = $self->associated_attribute; + + my $code = try { + $self->_compile_code([ + 'sub {', + $attr->inline_has('$_[0]') . ';', + '}', + ]); + } + catch { + confess "Could not generate inline predicate because : $_"; + }; return $code; } sub _generate_clearer_method_inline { - my $self = shift; - my $attr = $self->associated_attribute; - my $attr_name = $attr->name; - my $meta_instance = $attr->associated_class->instance_metaclass; - - my ( $code, $e ) = $self->_eval_closure( - {}, - 'sub {' - . $meta_instance->inline_deinitialize_slot('$_[0]', $attr_name) - . '}' - ); - confess "Could not generate inline clearer because : $e" if $e; + my $self = shift; + my $attr = $self->associated_attribute; + + my $code = try { + $self->_compile_code([ + 'sub {', + $attr->inline_clear('$_[0]') . ';', + '}', + ]); + } + catch { + confess "Could not generate inline clearer because : $_"; + }; return $code; } @@ -241,7 +248,7 @@ Class::MOP::Method::Accessor - Method Meta Object for accessors =head1 DESCRIPTION -This is a subclass of which is used by +This is a subclass of C which is used by C to generate accessor code. It handles generation of readers, writers, predicates and clearers. For each type of method, it can either create a subroutine reference, or actually @@ -310,7 +317,7 @@ Stevan Little Estevan@iinteractive.comE =head1 COPYRIGHT AND LICENSE -Copyright 2006-2009 by Infinity Interactive, Inc. +Copyright 2006-2010 by Infinity Interactive, Inc. L