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.20
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 ( i.e.: 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 <http://blog.fox.geek.nz/2010/11/searching-design-spec-for-ultimate.html>
110 This blog post is a good overview of the current state of the
111 existing modules for loading other modules in various ways.
112
113 <http://blog.fox.geek.nz/2010/11/handling-optional-requirements-with.html>
114 This blog post describes how to handle optional modules with
115 Class::Load.
116
117 <http://d.hatena.ne.jp/tokuhirom/20110202/1296598578>
118 This Japanese blog post describes why DBIx::Skinny now uses
119 Class::Load over its competitors.
120
121 Moose, Jifty, Prophet, etc
122 This module was designed to be used anywhere you have "if (eval
123 "require $module"; 1)", which occurs in many large projects.
124
126 Shawn M Moore <sartak at bestpractical.com>
127
129 This software is copyright (c) 2012 by Shawn M Moore.
130
131 This is free software; you can redistribute it and/or modify it under
132 the same terms as the Perl 5 programming language system itself.
133
134
135
136perl v5.16.3 2012-07-15 Class::Load(3)