From: Shawn M Moore Date: Tue, 10 Jun 2008 03:46:21 +0000 (+0000) Subject: Moose compat: the details of load_class. Check whether the package has any methods... X-Git-Tag: 0.04~57 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=2a674d232b1060884cceddaa23be19aa7b335a33;p=gitmo%2FMouse.git Moose compat: the details of load_class. Check whether the package has any methods, etc --- diff --git a/lib/Mouse.pm b/lib/Mouse.pm index ee5a823..2dad7f7 100644 --- a/lib/Mouse.pm +++ b/lib/Mouse.pm @@ -83,16 +83,28 @@ do { sub load_class { my $class = shift; + return 1 if is_class_loaded($class); + (my $file = "$class.pm") =~ s{::}{/}g; eval { CORE::require($file) }; - confess "Could not load class ($class) because : $@" - if $@ - && $@ !~ /^Can't locate .*? at /; + confess "Could not load class ($class) because : $@" if $@; return 1; } +sub is_class_loaded { + my $class = shift; + + no strict 'refs'; + return 1 if defined ${"${class}::VERSION"} || defined @{"${class}::ISA"}; + foreach my $symbol (keys %{"${class}::"}) { + next if substr($symbol, -2, 2) eq '::'; + return 1 if defined &{"${class}::${symbol}"}; + } + return 0; +} + 1; __END__ diff --git a/t/020-load-class.t b/t/020-load-class.t index dd27587..0373459 100644 --- a/t/020-load-class.t +++ b/t/020-load-class.t @@ -12,16 +12,14 @@ can_ok('Anti::Mouse' => 'antimouse'); do { package Class; + sub yay {} }; ok(Mouse::load_class('Class'), "this should not die!"); -TODO: { - local $TODO = "can't have the previous test and this test pass.. yet"; - throws_ok { - Mouse::load_class('FakeClassOhNo'); - } qr/Can't locate /; -}; +throws_ok { + Mouse::load_class('FakeClassOhNo'); +} qr/Can't locate /; throws_ok { Mouse::load_class('Anti::MouseError');