From: Shawn M Moore Date: Tue, 10 Jun 2008 04:50:35 +0000 (+0000) Subject: Various is_class_loaded/load_class nits X-Git-Tag: 0.04~45 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=gitmo%2FMouse.git;a=commitdiff_plain;h=262801ef3236bc935538bc2eff263df1368830ec;hp=3e8e32b552d7bd8a55309759466b94e0e88d2f99 Various is_class_loaded/load_class nits --- diff --git a/lib/Mouse.pm b/lib/Mouse.pm index ff3da1e..59ddd5c 100644 --- a/lib/Mouse.pm +++ b/lib/Mouse.pm @@ -82,8 +82,8 @@ do { sub load_class { my $class = shift; - return if ref($class); - return unless defined($class) && length($class); + + return 0 if ref($class) || !defined($class) || !length($class); return 1 if is_class_loaded($class); @@ -210,6 +210,12 @@ This will load a given C (or die if it's not loadable). This function can be used in place of tricks like C or using C. +=head2 is_class_loaded Class::Name -> Bool + +Returns whether this class is actually loaded or not. It uses a heuristic which +involves checking for the existence of C<$VERSION>, C<@ISA>, and any +locally-defined method. + =head1 AUTHOR Shawn M Moore, C<< >> diff --git a/t/020-load-class.t b/t/020-load-class.t index 0373459..479bc08 100644 --- a/t/020-load-class.t +++ b/t/020-load-class.t @@ -1,12 +1,19 @@ #!/usr/bin/env perl use strict; use warnings; -use Test::More tests => 5; +use Test::More tests => 11; use Test::Exception; require Mouse; use lib 't/lib'; +for my $method ('load_class', 'is_class_loaded') { + my $code = Mouse->can($method); + ok(!$code->(), "$method with no argument returns false"); + ok(!$code->(''), "can't load the empty class"); + ok(!$code->(\"foo"), "can't load a class name reference??"); +} + ok(Mouse::load_class('Anti::Mouse')); can_ok('Anti::Mouse' => 'antimouse');