View on
MetaCPAN is shutting down
For details read Perl NOC. After June 25th this page will redirect to
Shawn M Moore > Class-Load-0.06 > Class::Load



Annotate this POD (1)


New  1
Open  2
View/Report Bugs
Module Version: 0.06   Source   Latest Release: Class-Load-0.25


Class::Load - a working (require "Class::Name") and more


    use Class::Load ':all';

        or plan skip_all => "Class::Name required to run these tests";



    my $baseclass = load_optional_class('Class::Name::MightExist')
        ? 'Class::Name::MightExist'
        : 'Class::Name::Default';


require EXPR only accepts Class/ style module names, not Class::Name. How frustrating! For that, we provide load_class 'Class::Name'.

It's often useful to test whether a module can be loaded, instead of throwing an error when it's not available. For that, we provide try_load_class 'Class::Name'.

Finally, sometimes we need to know whether a particular class has been loaded. Asking %INC is an option, but that will miss inner packages and any class for which the filename does not correspond to the package name. For that, we provide is_class_loaded 'Class::Name'.


load_class Class::Name

load_class will load Class::Name or throw an error, much like require.

If Class::Name is already loaded (checked with is_class_loaded) then it will not try to load the class. This is useful when you have inner packages which require does not check.

try_load_class Class::Name -> 0|1 =head2 try_load_class Class::Name -> (0|1, error message)

Returns 1 if the class was loaded, 0 if it was not. If the class was not loaded, the error will be returned as a second return value in list context.

Again, if Class::Name is already loaded (checked with is_class_loaded) then it will not try to load the class. This is useful when you have inner packages which require does not check.

is_class_loaded Class::Name -> 0|1

This uses a number of heuristics to determine if the class Class::Name is loaded. There heuristics were taken from Class::MOP's old pure-perl implementation.

load_optional_class Class::Name -> 0|1

load_optional_class is a lot like try_load_class, but also a lot like load_class.

If the class exists, and it works, then it will return 1.

If the class doesn't exist, and it appears to not exist on disk either, it will return 0.

If the class exists on disk, but loading from disk results in an error ( ie: a syntax error ), then it will croak with that error.

This is useful for using if you want a fallback module system, ie:

    my $class = load_optional_class($foo) ? $foo : $default;

That way, if $foo does exist, but can't be loaded due to error, you won't get the behaviour of it simply not existing.



Adds a require method to UNIVERSAL so that you can say Class::Name->require. I personally dislike the pollution.


Supports Class::Name and Class/ formats, no try_to_load or is_class_loaded.

Moose, Jifty, Prophet, etc

This module was designed to be used anywhere you have if (eval "require $module"; 1), which occurs in many large projects.


Shawn M Moore, <sartak at>

The implementation of is_class_loaded has been taken from Class::MOP.


Please report any bugs or feature requests to bug-class-load at, or through the web interface at


Copyright 2008-2009 Best Practical Solutions.

This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

syntax highlighting: