From: Shawn M Moore Date: Tue, 1 Jul 2008 03:48:31 +0000 (+0000) Subject: Have load_class return the metaclass, initializing it if necessary X-Git-Tag: 0_64~7 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=0794096892c9a72256d9227cf44f62a6090d1cad;p=gitmo%2FClass-MOP.git Have load_class return the metaclass, initializing it if necessary --- diff --git a/Changes b/Changes index bfc99a3..4b5175d 100644 --- a/Changes +++ b/Changes @@ -1,6 +1,11 @@ Revision history for Perl extension Class-MOP. 0.63 + * Class::MOP + - load_class will initialize a metaclass even if + the class is already loaded (sartak) + - load_class now returns the metaclass instance + instead of just 1 (sartak) 0.62 Wed June 18, 2008 - in is_class_loaded, recognize scalar references (as opposed to globs) in diff --git a/lib/Class/MOP.pm b/lib/Class/MOP.pm index 4265ca8..0792be8 100644 --- a/lib/Class/MOP.pm +++ b/lib/Class/MOP.pm @@ -131,19 +131,22 @@ sub load_class { confess "Invalid class name ($display)"; } - # see if this is already - # loaded in the symbol table - return 1 if is_class_loaded($class); - # otherwise require it ... - my $file = $class . '.pm'; - $file =~ s{::}{/}g; - eval { CORE::require($file) }; - confess "Could not load class ($class) because : $@" if $@; + # if the class is not already loaded in the symbol table.. + unless (is_class_loaded($class)) { + # require it + my $file = $class . '.pm'; + $file =~ s{::}{/}g; + eval { CORE::require($file) }; + confess "Could not load class ($class) because : $@" if $@; + } + + # initialize a metaclass if necessary unless (does_metaclass_exist($class)) { eval { Class::MOP::Class->initialize($class) }; confess "Could not initialize class ($class) because : $@" if $@; } - 1; # return true if it worked + + return get_metaclass_by_name($class); } sub is_class_loaded { diff --git a/t/083_load_class.t b/t/083_load_class.t index c6f816a..dc3ed5a 100644 --- a/t/083_load_class.t +++ b/t/083_load_class.t @@ -1,7 +1,7 @@ #!/usr/bin/env perl use strict; use warnings; -use Test::More tests => 13; +use Test::More tests => 14; use Test::Exception; require Class::MOP; @@ -15,7 +15,9 @@ throws_ok { Class::MOP::load_class() } qr/Invalid class name \(undef\)/; throws_ok { Class::MOP::load_class('') } qr/Invalid class name \(\)/; throws_ok { Class::MOP::load_class(\"foo") } qr/Invalid class name \(SCALAR\(\w+\)\)/; -ok(Class::MOP::load_class('BinaryTree')); +my $meta = Class::MOP::load_class('BinaryTree'); +ok($meta, "successfully loaded the class BinaryTree"); +is($meta->name, "BinaryTree", "load_class returns the metaclass"); can_ok('BinaryTree' => 'traverse'); do {