Class::Load(3pm) User Contributed Perl Documentation Class::Load(3pm)
Class::Load - A working (require "Class::Name") and more
| Use Case | Command | Description |
|---|---|---|
| Load a class and die on failure | load_class('Class::Name') |
Loads the class, throws error if not found. Optionally checks version. |
| Try to load a class without dying | try_load_class('Class::Name') |
Returns 1 if loaded, 0 if not, with error message in list context. |
| Check if a class is already loaded | is_class_loaded('Class::Name') |
Returns 1 if loaded, 0 if not, using heuristics (handles inner packages). |
| Load first available class from list | load_first_existing_class(@classes) |
Loads first class that exists and passes version check; throws error if none. |
| Load optional class with fallback | load_optional_class('Class::Name') |
Returns 1 if exists and loads, 0 if not; croaks on syntax errors. |
version 0.25
use Class::Load ':all';
try_load_class('Class::Name')
or plan skip_all => "Class::Name required to run these tests";
load_class('Class::Name');
is_class_loaded('Class::Name');
my $baseclass = load_optional_class('Class::Name::MightExist')
? 'Class::Name::MightExist'
: 'Class::Name::Default';
require EXPR only accepts Class/Name.pm 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, \%optionsload_class will load Class::Name or throw an error, much like require.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.-version. If you specify a version, then this subroutine will call Class::Name->VERSION( $options{-version} ) internally, which will throw an error if the class's version is not equal to or greater than the version you requested.try_load_class Class::Name, \%options -> (0|1, error message)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.load_class, you can pass a -version in %options. If the version is not sufficient, then this subroutine will return false.
is_class_loaded Class::Name, \%options -> 0|1Class::Name is loaded. There heuristics were taken from Class::MOP's old pure-perl implementation.load_class, you can pass a -version in %options. If the version is not sufficient, then this subroutine will return false.
load_first_existing_class Class::Name, \%options, ...load_optional_class Class::Name, \%options -> 0|1load_optional_class is a lot like try_load_class, but also a lot like load_class.croak with that error.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.
Because of some of the heuristics that this module uses to infer whether a module has been loaded, some false positives may occur in is_class_loaded checks (which are also performed internally in other interfaces) -- if a class has started to be loaded but then dies, it may appear that it has already been loaded, which can cause other things to make the wrong decision. Module::Runtime doesn't have this issue, but it also doesn't do some things that this module does -- for example gracefully handle packages that have been defined inline in the same file as another package.
if (eval "require $module"; 1), which occurs in many large projects.Bugs may be submitted through the RT bug tracker https://rt.cpan.org/Public/Dist/Display.html?Name=Class-Load (or bug-Class-Load@rt.org).
There is also a mailing list available for users of this distribution, at http://lists.perl.org/list/moose.html.
There is also an irc channel available for users of this distribution, at #moose on irc.perl.org irc://irc.perl.org/#moose.
Shawn M Moore <sartak at bestpractical.com>
This software is copyright (c) 2008 by Shawn M Moore.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
Generated by phpman v4.9.26-1-g511901d Author: Che Dong Under GNU General Public License
2026-08-09 09:12 @2600:1f28:365:80b0:50b3:453e:ff52:20f7
CrawledBy CCBot/2.0 (https://commoncrawl.org/faq/)
Enhanced by LLM: deepseek-v4-flash / taotoken.net / www.chedong.com - original format