From: Jesse Luehrs Date: Wed, 23 Sep 2009 02:04:26 +0000 (-0500) Subject: replace several uses of eval with try X-Git-Tag: 0.95~14 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=a834450571c76eaf152ecf163fe4b6ca40921eab;p=gitmo%2FClass-MOP.git replace several uses of eval with try --- diff --git a/Makefile.PL b/Makefile.PL index 7edc92e..38b00dc 100644 --- a/Makefile.PL +++ b/Makefile.PL @@ -22,6 +22,7 @@ requires 'Devel::GlobalDestruction'; requires 'MRO::Compat' => '0.05'; requires 'Scalar::Util' => '1.18'; requires 'Sub::Name' => '0.04'; +requires 'Try::Tiny' => '0.02'; requires 'Task::Weaken'; test_requires 'File::Spec'; diff --git a/lib/Class/MOP.pm b/lib/Class/MOP.pm index 67ade82..e7ee385 100644 --- a/lib/Class/MOP.pm +++ b/lib/Class/MOP.pm @@ -10,6 +10,7 @@ use MRO::Compat; use Carp 'confess'; use Scalar::Util 'weaken', 'reftype', 'blessed'; +use Try::Tiny; use Class::MOP::Class; use Class::MOP::Attribute; @@ -87,9 +88,9 @@ sub load_first_existing_class { my $found; my %exceptions; for my $class (@classes) { - my $e = _try_load_one_class($class); + my ($fail, $e) = _try_load_one_class($class); - if ($e) { + if ($fail) { my $pmfile = _class_to_pmfile($class); $exceptions{$class} = $e; last if $e !~ /^Can't locate \Q$pmfile\E in \@INC/; @@ -123,12 +124,17 @@ sub _try_load_one_class { my $file = _class_to_pmfile($class); - return do { - local $@; + my ($failed, $error); + try { local $SIG{__DIE__}; - eval { require($file) }; - $@; + require($file); + } + catch { + $failed = 1; + $error = $_; }; + + return $failed, $error; } sub load_class { diff --git a/lib/Class/MOP/Attribute.pm b/lib/Class/MOP/Attribute.pm index c58b7d8..1c3e407 100644 --- a/lib/Class/MOP/Attribute.pm +++ b/lib/Class/MOP/Attribute.pm @@ -8,6 +8,7 @@ use Class::MOP::Method::Accessor; use Carp 'confess'; use Scalar::Util 'blessed', 'weaken'; +use Try::Tiny; our $VERSION = '0.94'; $VERSION = eval $VERSION; @@ -369,7 +370,7 @@ sub _process_accessors { else { my $inline_me = ($generate_as_inline_methods && $self->associated_class->instance_metaclass->is_inlinable); my $method; - eval { + try { if ( $method_ctx ) { my $desc = "accessor $accessor"; if ( $accessor ne $self->name ) { @@ -387,8 +388,10 @@ sub _process_accessors { name => $accessor, definition_context => $method_ctx, ); + } + catch { + confess "Could not create the '$type' method for " . $self->name . " because : $_"; }; - confess "Could not create the '$type' method for " . $self->name . " because : $@" if $@; $self->associate_method($method); return ($accessor, $method); } diff --git a/lib/Class/MOP/Class.pm b/lib/Class/MOP/Class.pm index 52228dc..1eb958d 100644 --- a/lib/Class/MOP/Class.pm +++ b/lib/Class/MOP/Class.pm @@ -13,6 +13,7 @@ use Carp 'confess'; use Scalar::Util 'blessed', 'reftype', 'weaken'; use Sub::Name 'subname'; use Devel::GlobalDestruction 'in_global_destruction'; +use Try::Tiny; our $VERSION = '0.94'; $VERSION = eval $VERSION; @@ -735,16 +736,14 @@ sub add_attribute { $self->get_attribute_map->{$attr_name} = $attribute; # invalidate package flag here - my $e = do { - local $@; + try { local $SIG{__DIE__}; - eval { $attribute->install_accessors() }; - $@; - }; - if ( $e ) { - $self->remove_attribute($attr_name); - die $e; + $attribute->install_accessors(); } + catch { + $self->remove_attribute($attr_name); + die $_; + }; return $attribute; }