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);
This function can be used in place of tricks like
C<eval "use $module"> or using C<require>.
+=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<< <sartak at gmail.com> >>
#!/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');