From: Yuval Kogman Date: Fri, 25 Sep 2009 19:27:44 +0000 (+0300) Subject: refactor load_first_existing_class X-Git-Tag: 0.95~13 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=a02f24cbc720ab9fd5e04ed96f36406654f3740a;p=gitmo%2FClass-MOP.git refactor load_first_existing_class --- diff --git a/lib/Class/MOP.pm b/lib/Class/MOP.pm index e7ee385..bb475b2 100644 --- a/lib/Class/MOP.pm +++ b/lib/Class/MOP.pm @@ -76,7 +76,7 @@ sub _class_to_pmfile { sub load_first_existing_class { my @classes = @_ - or return; + or return; foreach my $class (@classes) { unless ( _is_valid_class_name($class) ) { @@ -87,54 +87,31 @@ sub load_first_existing_class { my $found; my %exceptions; - for my $class (@classes) { - my ($fail, $e) = _try_load_one_class($class); - if ($fail) { - my $pmfile = _class_to_pmfile($class); - $exceptions{$class} = $e; - last if $e !~ /^Can't locate \Q$pmfile\E in \@INC/; - } - else { - $found = $class; - last; - } - } + for my $class (@classes) { + my $file = _class_to_pmfile($class); - return $found if $found; + return $class if is_class_loaded($class);; - confess join( - "\n", - map { - sprintf( - "Could not load class (%s) because : %s", $_, - $exceptions{$_} - ) + return $class if try { + local $SIG{__DIE__}; + require $file; + return 1; + } + catch { + unless (/^Can't locate \Q$file\E in \@INC/) { + confess "Couldn't load class ($class) because: $_"; } - grep { - exists $exceptions{$_} - } @classes - ); -} - -sub _try_load_one_class { - my $class = shift; - return if is_class_loaded($class); - - my $file = _class_to_pmfile($class); - - my ($failed, $error); - try { - local $SIG{__DIE__}; - require($file); + return; + }; } - catch { - $failed = 1; - $error = $_; - }; - return $failed, $error; + if ( @classes > 1 ) { + confess "Can't locate any of @classes in \@INC (\@INC contains: @INC)."; + } else { + confess "Can't locate " . _class_to_pmfile($classes[0]) . " in \@INC (\@INC contains: @INC)."; + } } sub load_class { diff --git a/t/083_load_class.t b/t/083_load_class.t index fb802bf..e990351 100644 --- a/t/083_load_class.t +++ b/t/083_load_class.t @@ -62,8 +62,8 @@ qr/Missing right curly/, throws_ok { Class::MOP::load_class('This::Does::Not::Exist'); } -qr/Could not load class \(This::Does::Not::Exist\) because :/, - 'Many Moose tests rely on the exact formatting of this error'; +qr{Can't locate This/Does/Not/Exist\.pm in \@INC}, + 'load_first_existing_class throws a familiar error for a single module'; { package Other; @@ -87,7 +87,7 @@ lives_ok { throws_ok { Class::MOP::load_first_existing_class("Does::Not::Exist", "Also::Does::Not::Exist") -} qr/Could not load class \(Does::Not::Exist.*Could not load class \(Also::Does::Not::Exist/s, 'Multiple non-existant classes cause exception'; +} qr/Does::Not::Exist.*Also::Does::Not::Exist/s, 'Multiple non-existant classes cause exception'; { sub whatever {