sub load_first_existing_class {
my @classes = @_
- or return;
+ or return;
foreach my $class (@classes) {
unless ( _is_valid_class_name($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 {
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;
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 {