From: Jesse Luehrs Date: Wed, 20 Oct 2010 23:21:38 +0000 (-0500) Subject: use the new _compile_code interface in cmop X-Git-Tag: 1.9900~45 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=55c361dc01efaeb8e6f957e91c0b3b291e4648f8;p=gitmo%2FMoose.git use the new _compile_code interface in cmop --- diff --git a/lib/Moose/Meta/Method/Accessor.pm b/lib/Moose/Meta/Method/Accessor.pm index a293f8e..59c9341 100644 --- a/lib/Moose/Meta/Method/Accessor.pm +++ b/lib/Moose/Meta/Method/Accessor.pm @@ -4,6 +4,8 @@ package Moose::Meta::Method::Accessor; use strict; use warnings; +use Try::Tiny; + our $VERSION = '1.19'; $VERSION = eval $VERSION; our $AUTHORITY = 'cpan:STEVAN'; @@ -16,19 +18,20 @@ sub _error_thrower { ( ref $self && $self->associated_attribute ) || $self->SUPER::_error_thrower(); } -sub _eval_code { - my ( $self, $source ) = @_; - - my $environment = $self->_eval_environment; - - my ( $code, $e ) = $self->_compile_code( environment => $environment, code => $source ); - - $self->throw_error( - "Could not create writer for '${\$self->associated_attribute->name}' because $e \n code: $source", - error => $e, data => $source ) - if $e; - - return $code; +sub _compile_code { + my $self = shift; + my @args = @_; + try { + $self->SUPER::_compile_code(@args); + } + catch { + $self->throw_error( + 'Could not create writer for ' + . "'" . $self->associated_attribute->name . "' " + . 'because ' . $_, + error => $_, + ); + }; } sub _eval_environment { @@ -54,7 +57,7 @@ sub _generate_accessor_method_inline { my $inv = '$_[0]'; my $value_name = $self->_value_needs_copy ? '$val' : '$_[1]'; - $self->_eval_code('sub { ' . "\n" + $self->_compile_code('sub { ' . "\n" . $self->_inline_pre_body(@_) . "\n" . 'if (scalar(@_) >= 2) {' . "\n" . $self->_inline_copy_value . "\n" @@ -76,7 +79,7 @@ sub _generate_writer_method_inline { my $inv = '$_[0]'; my $value_name = $self->_value_needs_copy ? '$val' : '$_[1]'; - $self->_eval_code('sub { ' + $self->_compile_code('sub { ' . $self->_inline_pre_body(@_) . $self->_inline_copy_value . $self->_inline_check_required @@ -94,7 +97,7 @@ sub _generate_reader_method_inline { my $inv = '$_[0]'; my $slot_access = $self->_inline_get($inv); - $self->_eval_code('sub {' + $self->_compile_code('sub {' . $self->_inline_pre_body(@_) . $self->_inline_throw_error('"Cannot assign a value to a read-only accessor"', 'data => \@_') . ' if @_ > 1;' . $self->_inline_check_lazy($inv) diff --git a/lib/Moose/Meta/Method/Accessor/Native.pm b/lib/Moose/Meta/Method/Accessor/Native.pm index dd26002..5c8e22b 100644 --- a/lib/Moose/Meta/Method/Accessor/Native.pm +++ b/lib/Moose/Meta/Method/Accessor/Native.pm @@ -45,7 +45,7 @@ sub root_types { (shift)->{'root_types'} } sub _initialize_body { my $self = shift; - $self->{'body'} = $self->_eval_code( $self->_generate_method ); + $self->{'body'} = $self->_compile_code( $self->_generate_method ); return; } diff --git a/lib/Moose/Meta/Method/Constructor.pm b/lib/Moose/Meta/Method/Constructor.pm index c1af28d..d85c2a4 100644 --- a/lib/Moose/Meta/Method/Constructor.pm +++ b/lib/Moose/Meta/Method/Constructor.pm @@ -6,6 +6,7 @@ use warnings; use Carp (); use Scalar::Util 'blessed', 'weaken', 'looks_like_number', 'refaddr'; +use Try::Tiny; our $VERSION = '1.19'; our $AUTHORITY = 'cpan:STEVAN'; @@ -98,21 +99,25 @@ sub _initialize_body { my $defaults = [map { $_->default } @$attrs]; - my ( $code, $e ) = $self->_compile_code( - code => $source, - environment => { - '$meta' => \$self, - '$attrs' => \$attrs, - '$defaults' => \$defaults, - '@type_constraints' => \@type_constraints, - '@type_constraint_bodies' => \@type_constraint_bodies, - }, - ); - - $self->throw_error( - "Could not eval the constructor :\n\n$source\n\nbecause :\n\n$e", - error => $e, data => $source ) - if $e; + my $code = try { + $self->_compile_code( + source => $source, + environment => { + '$meta' => \$self, + '$attrs' => \$attrs, + '$defaults' => \$defaults, + '@type_constraints' => \@type_constraints, + '@type_constraint_bodies' => \@type_constraint_bodies, + }, + ); + } + catch { + $self->throw_error( + "Could not eval the constructor :\n\n$source\n\nbecause :\n\n$_", + error => $_, + data => $source, + ); + }; $self->{'body'} = $code; } diff --git a/lib/Moose/Meta/Method/Destructor.pm b/lib/Moose/Meta/Method/Destructor.pm index 4cc8129..44d3e76 100644 --- a/lib/Moose/Meta/Method/Destructor.pm +++ b/lib/Moose/Meta/Method/Destructor.pm @@ -6,7 +6,7 @@ use warnings; use Devel::GlobalDestruction (); use Scalar::Util 'blessed', 'weaken'; -use Try::Tiny (); +use Try::Tiny; our $VERSION = '1.19'; $VERSION = eval $VERSION; @@ -108,15 +108,16 @@ sub _initialize_body { warn $source if $self->options->{debug}; - my ( $code, $e ) = $self->_compile_code( - environment => {}, - code => $source, - ); - - $self->throw_error( - "Could not eval the destructor :\n\n$source\n\nbecause :\n\n$e", - error => $e, data => $source ) - if $e; + my $code = try { + $self->_compile_code(source => $source); + } + catch { + $self->throw_error( + "Could not eval the destructor :\n\n$source\n\nbecause :\n\n$_", + error => $_, + data => $source, + ); + }; $self->{'body'} = $code; }