Revision history for Perl extension Class-MOP.
0.37
- ~~ Many documentation updates ~~
+ ~~ Many, many documentation updates ~~
+
+ * Class::MOP
+ - added &load_class and &is_class_loaded
+ - added tests and docs for these
* Class::MOP::Attribute
- default now checks the instance with defined to
# because I don't yet see a good reason to do so.
}
+sub load_class {
+ my $class = shift;
+ # 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 $@;
+ 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
+}
+
+sub is_class_loaded {
+ my $class = shift;
+ no strict 'refs';
+ return 1 if defined ${"${class}::VERSION"} || defined @{"${class}::ISA"};
+ foreach (keys %{"${class}::"}) {
+ next if substr($_, -2, 2) eq '::';
+ return 1 if defined &{"${class}::$_"};
+ }
+ return 0;
+}
+
+
## ----------------------------------------------------------------------------
## Setting up our environment ...
## ----------------------------------------------------------------------------
=head1 FUNCTIONS
+=head2 Utility functions
+
+=over 4
+
+=item B<load_class ($class_name)>
+
+This will load a given C<$class_name> and if it does not have an
+already initialized metaclass, then it will intialize one for it.
+
+=item B<is_class_loaded ($class_name)>
+
+This will return a boolean depending on if the C<$class_name> has
+been loaded.
+
+NOTE: This does a basic check of the symbol table to try and
+determine as best it can if the C<$class_name> is loaded, it
+is probably correct about 99% of the time.
+
+=back
+
+=head2 Metaclass cache functions
+
Class::MOP holds a cache of metaclasses, the following are functions
(B<not methods>) which can be used to access that cache. It is not
recommended that you mess with this, bad things could happen. But if
use strict;
use warnings;
-use Test::More tests => 29;
+use Test::More tests => 39;
BEGIN {
use_ok('Class::MOP');
'Class::MOP::Object' => Class::MOP::Object->meta,
);
+ok(Class::MOP::is_class_loaded($_), '... ' . $_ . ' is loaded') for keys %METAS;
+
ok($_->is_immutable(), '... ' . $_->name . ' is immutable') for values %METAS;
is_deeply(
use strict;
use warnings;
-use Test::More tests => 68;
+use FindBin;
+use File::Spec::Functions;
+
+use Test::More tests => 70;
+use Test::Exception;
BEGIN {
use_ok('Class::MOP');
- use_ok('t::lib::BinaryTree');
}
+use lib catdir($FindBin::Bin, 'lib');
+
## ----------------------------------------------------------------------------
## These are all tests which are derived from the Tree::Binary test suite
## ----------------------------------------------------------------------------
+ok(!Class::MOP::is_class_loaded('BinaryTree'), '... the binary tree class is not loaded');
+
+lives_ok {
+ Class::MOP::load_class('BinaryTree');
+} '... loaded the BinaryTree class without dying';
+
+ok(Class::MOP::is_class_loaded('BinaryTree'), '... the binary tree class is now loaded');
+
## ----------------------------------------------------------------------------
## t/10_Tree_Binary_test.t