factor codegen stuff out to Eval::Closure
[gitmo/Class-MOP.git] / lib / Class / MOP / Method / Accessor.pm
index 5ba0f3d..b4377e9 100644 (file)
@@ -6,6 +6,7 @@ use warnings;
 
 use Carp         'confess';
 use Scalar::Util 'blessed', 'weaken';
+use Try::Tiny;
 
 our $VERSION   = '1.11';
 $VERSION = eval $VERSION;
@@ -133,14 +134,20 @@ sub _generate_accessor_method_inline {
     my $self = shift;
     my $attr = $self->associated_attribute;
 
-    my ( $code, $e ) = $self->_eval_closure(
-        {},
-        'sub {'
-            . $attr->inline_set( '$_[0]', '$_[1]' )
-            . ' if scalar(@_) == 2; '
-            . $attr->inline_get('$_[0]') . '}'
-    );
-    confess "Could not generate inline accessor because : $e" if $e;
+    my $code = try {
+        $self->_compile_code(
+            source => [
+                'sub {',
+                    $attr->inline_set( '$_[0]', '$_[1]' )
+                        . ' if scalar(@_) == 2;',
+                    $attr->inline_get('$_[0]') . ';',
+                '}',
+            ]
+        );
+    }
+    catch {
+        confess "Could not generate inline accessor because : $_";
+    };
 
     return $code;
 }
@@ -149,13 +156,20 @@ sub _generate_reader_method_inline {
     my $self = shift;
     my $attr = $self->associated_attribute;
 
-    my ( $code, $e ) = $self->_eval_closure(
-        {},
-        'sub {'
-            . 'confess "Cannot assign a value to a read-only accessor" if @_ > 1;'
-            . $attr->inline_get('$_[0]') . '}'
-    );
-    confess "Could not generate inline reader because : $e" if $e;
+    my $code = try {
+        $self->_compile_code(
+            source => [
+                '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;
 }
@@ -164,11 +178,18 @@ sub _generate_writer_method_inline {
     my $self = shift;
     my $attr = $self->associated_attribute;
 
-    my ( $code, $e ) = $self->_eval_closure(
-        {},
-        'sub {' . $attr->inline_set( '$_[0]', '$_[1]' ) . '}'
-    );
-    confess "Could not generate inline writer because : $e" if $e;
+    my $code = try {
+        $self->_compile_code(
+            source => [
+                'sub {',
+                    $attr->inline_set( '$_[0]', '$_[1]' ) . ';',
+                '}',
+            ],
+        );
+    }
+    catch {
+        confess "Could not generate inline writer because : $_";
+    };
 
     return $code;
 }
@@ -177,11 +198,18 @@ sub _generate_predicate_method_inline {
     my $self = shift;
     my $attr = $self->associated_attribute;
 
-    my ( $code, $e ) = $self->_eval_closure(
-        {},
-        'sub {' . $attr->inline_has('$_[0]') . '}'
-    );
-    confess "Could not generate inline predicate because : $e" if $e;
+    my $code = try {
+        $self->_compile_code(
+            source => [
+                'sub {',
+                    $attr->inline_has('$_[0]') . ';',
+                '}',
+            ],
+        );
+    }
+    catch {
+        confess "Could not generate inline predicate because : $_";
+    };
 
     return $code;
 }
@@ -190,11 +218,18 @@ sub _generate_clearer_method_inline {
     my $self = shift;
     my $attr = $self->associated_attribute;
 
-    my ( $code, $e ) = $self->_eval_closure(
-        {},
-        'sub {' . $attr->inline_clear('$_[0]') . '}'
-    );
-    confess "Could not generate inline clearer because : $e" if $e;
+    my $code = try {
+        $self->_compile_code(
+            source => [
+                'sub {',
+                    $attr->inline_clear('$_[0]') . ';',
+                '}',
+            ],
+        );
+    }
+    catch {
+        confess "Could not generate inline clearer because : $_";
+    };
 
     return $code;
 }