1Class::Load(3) User Contributed Perl Documentation Class::Load(3)
2
3
4
6 Class::Load - A working (require "Class::Name") and more
7
9 version 0.25
10
12 use Class::Load ':all';
13
14 try_load_class('Class::Name')
15 or plan skip_all => "Class::Name required to run these tests";
16
17 load_class('Class::Name');
18
19 is_class_loaded('Class::Name');
20
21 my $baseclass = load_optional_class('Class::Name::MightExist')
22 ? 'Class::Name::MightExist'
23 : 'Class::Name::Default';
24
26 "require EXPR" only accepts "Class/Name.pm" style module names, not
27 "Class::Name". How frustrating! For that, we provide "load_class
28 'Class::Name'".
29
30 It's often useful to test whether a module can be loaded, instead of
31 throwing an error when it's not available. For that, we provide
32 "try_load_class 'Class::Name'".
33
34 Finally, sometimes we need to know whether a particular class has been
35 loaded. Asking %INC is an option, but that will miss inner packages
36 and any class for which the filename does not correspond to the package
37 name. For that, we provide "is_class_loaded 'Class::Name'".
38
40 load_class Class::Name, \%options
41 "load_class" will load "Class::Name" or throw an error, much like
42 "require".
43
44 If "Class::Name" is already loaded (checked with "is_class_loaded")
45 then it will not try to load the class. This is useful when you have
46 inner packages which "require" does not check.
47
48 The %options hash currently accepts one key, "-version". If you specify
49 a version, then this subroutine will call "Class::Name->VERSION(
50 $options{-version} )" internally, which will throw an error if the
51 class's version is not equal to or greater than the version you
52 requested.
53
54 This method will return the name of the class on success.
55
56 try_load_class Class::Name, \%options -> (0|1, error message)
57 Returns 1 if the class was loaded, 0 if it was not. If the class was
58 not loaded, the error will be returned as a second return value in list
59 context.
60
61 Again, if "Class::Name" is already loaded (checked with
62 "is_class_loaded") then it will not try to load the class. This is
63 useful when you have inner packages which "require" does not check.
64
65 Like "load_class", you can pass a "-version" in %options. If the
66 version is not sufficient, then this subroutine will return false.
67
68 is_class_loaded Class::Name, \%options -> 0|1
69 This uses a number of heuristics to determine if the class
70 "Class::Name" is loaded. There heuristics were taken from Class::MOP's
71 old pure-perl implementation.
72
73 Like "load_class", you can pass a "-version" in %options. If the
74 version is not sufficient, then this subroutine will return false.
75
76 load_first_existing_class Class::Name, \%options, ...
77 This attempts to load the first loadable class in the list of classes
78 given. Each class name can be followed by an options hash reference.
79
80 If any one of the classes loads and passes the optional version check,
81 that class name will be returned. If none of the classes can be loaded
82 (or none pass their version check), then an error will be thrown.
83
84 If, when attempting to load a class, it fails to load because of a
85 syntax error, then an error will be thrown immediately.
86
87 load_optional_class Class::Name, \%options -> 0|1
88 "load_optional_class" is a lot like "try_load_class", but also a lot
89 like "load_class".
90
91 If the class exists, and it works, then it will return 1. If you
92 specify a version in %options, then the version check must succeed or
93 it will return 0.
94
95 If the class doesn't exist, and it appears to not exist on disk either,
96 it will return 0.
97
98 If the class exists on disk, but loading from disk results in an error
99 (e.g.: a syntax error), then it will "croak" with that error.
100
101 This is useful for using if you want a fallback module system, i.e.:
102
103 my $class = load_optional_class($foo) ? $foo : $default;
104
105 That way, if $foo does exist, but can't be loaded due to error, you
106 won't get the behaviour of it simply not existing.
107
109 Because of some of the heuristics that this module uses to infer
110 whether a module has been loaded, some false positives may occur in
111 "is_class_loaded" checks (which are also performed internally in other
112 interfaces) -- if a class has started to be loaded but then dies, it
113 may appear that it has already been loaded, which can cause other
114 things to make the wrong decision. Module::Runtime doesn't have this
115 issue, but it also doesn't do some things that this module does -- for
116 example gracefully handle packages that have been defined inline in the
117 same file as another package.
118
120 <http://blog.fox.geek.nz/2010/11/searching-design-spec-for-ultimate.html>
121 This blog post is a good overview of the current state of the
122 existing modules for loading other modules in various ways.
123
124 <http://blog.fox.geek.nz/2010/11/handling-optional-requirements-with.html>
125 This blog post describes how to handle optional modules with
126 Class::Load.
127
128 <http://d.hatena.ne.jp/tokuhirom/20110202/1296598578>
129 This Japanese blog post describes why DBIx::Skinny now uses
130 Class::Load over its competitors.
131
132 Moose, Jifty, Prophet, etc
133 This module was designed to be used anywhere you have "if (eval
134 "require $module"; 1)", which occurs in many large projects.
135
136 Module::Runtime
137 A leaner approach to loading modules
138
140 Bugs may be submitted through the RT bug tracker
141 <https://rt.cpan.org/Public/Dist/Display.html?Name=Class-Load> (or
142 bug-Class-Load@rt.cpan.org <mailto:bug-Class-Load@rt.cpan.org>).
143
144 There is also a mailing list available for users of this distribution,
145 at <http://lists.perl.org/list/moose.html>.
146
147 There is also an irc channel available for users of this distribution,
148 at "#moose" on "irc.perl.org" <irc://irc.perl.org/#moose>.
149
151 Shawn M Moore <sartak at bestpractical.com>
152
154 • Dave Rolsky <autarch@urth.org>
155
156 • Karen Etheridge <ether@cpan.org>
157
158 • Shawn Moore <sartak@bestpractical.com>
159
160 • Jesse Luehrs <doy@tozt.net>
161
162 • Kent Fredric <kentfredric@gmail.com>
163
164 • Paul Howarth <paul@city-fan.org>
165
166 • Olivier Mengué <dolmen@cpan.org>
167
168 • Caleb Cushing <xenoterracide@gmail.com>
169
171 This software is copyright (c) 2008 by Shawn M Moore.
172
173 This is free software; you can redistribute it and/or modify it under
174 the same terms as the Perl 5 programming language system itself.
175
176
177
178perl v5.32.1 2021-01-27 Class::Load(3)