simplify more stuff
[gitmo/Class-MOP.git] / lib / Class / MOP / Method / Accessor.pm
index 685202e..08bcbd1 100644 (file)
@@ -6,8 +6,9 @@ use warnings;
 
 use Carp         'confess';
 use Scalar::Util 'blessed', 'weaken';
+use Try::Tiny;
 
-our $VERSION   = '1.04';
+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 <Class::MOP::Method> which is used by
+This is a subclass of C<Class::MOP::Method> which is used by
 C<Class::MOP::Attribute> 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