From: Dave Rolsky Date: Tue, 21 Oct 2008 19:36:47 +0000 (+0000) Subject: Rename load_first_class_of to load_first_existing_class and so some X-Git-Tag: 0.68~9 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=063ad0c5732b1431b6b0cfdba6c99deb66f1b9cf;p=gitmo%2FClass-MOP.git Rename load_first_class_of to load_first_existing_class and so some cleanup on the code. --- diff --git a/lib/Class/MOP.pm b/lib/Class/MOP.pm index 0dde670..99894d0 100644 --- a/lib/Class/MOP.pm +++ b/lib/Class/MOP.pm @@ -100,8 +100,7 @@ sub _load_pure_perl { # because I don't yet see a good reason to do so. } -sub load_one_class_of { - use List::Util qw/first/; +sub load_first_existing_class { my @classes = @_; foreach my $class (@classes) { @@ -111,34 +110,52 @@ sub load_one_class_of { } } + my $found; my %exceptions; - my $name = first { - return $_ if is_class_loaded($_); - # require it - my $file = $_ . '.pm'; - $file =~ s{::}{/}g; - my $e = do { local $@; eval { require($file) }; $@ }; + for my $class (@classes) { + my $e = _try_load_one_class($class); + if ($e) { - $exceptions{$_} = $e; - return; + $exceptions{$class} = $e; } else { - return $_; + $found = $class; + last; } - } @classes; + } - if ($name) { - return get_metaclass_by_name($name) || $name; + if ($found) { + return get_metaclass_by_name($found) || $found; } - # Could load no classes. - confess join("\n", - map { sprintf("Could not load class (%s) because : %s", $_, $exceptions{$_}) } @classes + confess join( + "\n", + map { + sprintf( + "Could not load class (%s) because : %s", $_, + $exceptions{$_} + ) + } @classes ) if keys %exceptions; } +sub _try_load_one_class { + my $class = shift; + + return if is_class_loaded($class); + + my $file = $class . '.pm'; + $file =~ s{::}{/}g; + + return do { + local $@; + eval { require($file) }; + $@; + }; +} + sub load_class { - load_one_class_of($_[0]); + load_first_existing_class($_[0]); } sub _is_valid_class_name { @@ -881,14 +898,6 @@ already initialized metaclass, then it will intialize one for it. This function can be used in place of tricks like C or using C. -=item B - -This will attempt to load the list of classes given as parameters. -The first class successfully found or loaded will have it's metaclass -initialized (if needed) and returned. Subsequent classes to the first -loaded class will be ignored, and an exception will be thrown if none -of the supplied class names can be loaded. - =item B This will return a boolean depending on if the C<$class_name> has @@ -934,6 +943,17 @@ destruction. Otherwise it's a constant returning false. +=item B + +B + +Given a list of class names, this function will attempt to load each +one in turn. + +If it finds a class it can load, it will return that class's +metaclass. If none of the classes can be loaded, it will throw an +exception. + =back =head2 Metaclass cache functions diff --git a/t/083_load_class.t b/t/083_load_class.t index 38816e9..06ff0c1 100644 --- a/t/083_load_class.t +++ b/t/083_load_class.t @@ -67,11 +67,11 @@ lives_ok { isa_ok( Class::MOP::load_class("Lala"), "Class::MOP::Class", "when an object has a metaclass it is returned" ); lives_ok { - isa_ok(Class::MOP::load_one_class_of("Lala", "Does::Not::Exist"), "Class::MOP::Class", 'Load_classes first param ok, metaclass returned'); - isa_ok(Class::MOP::load_one_class_of("Does::Not::Exist", "Lala"), "Class::MOP::Class", 'Load_classes second param ok, metaclass returned'); + isa_ok(Class::MOP::load_first_existing_class("Lala", "Does::Not::Exist"), "Class::MOP::Class", 'Load_classes first param ok, metaclass returned'); + isa_ok(Class::MOP::load_first_existing_class("Does::Not::Exist", "Lala"), "Class::MOP::Class", 'Load_classes second param ok, metaclass returned'); } 'load_classes works'; throws_ok { - Class::MOP::load_one_class_of("Does::Not::Exist", "Also::Does::Not::Exist") + 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';