1Module::Load(3) User Contributed Perl Documentation Module::Load(3)
2
3
4
6 Module::Load - runtime require of both modules and files
7
9 use Module::Load;
10
11 my $module = 'Data::Dumper';
12
13 load Data::Dumper; # loads that module, but not import any functions
14 # -> cannot use 'Dumper' function
15
16 load 'Data::Dumper'; # ditto
17 load $module # tritto
18
19 autoload Data::Dumper; # loads that module and imports the default functions
20 # -> can use 'Dumper' function
21
22 my $script = 'some/script.pl'
23 load $script;
24 load 'some/script.pl'; # use quotes because of punctuations
25
26 load thing; # try 'thing' first, then 'thing.pm'
27
28 load CGI, ':all'; # like 'use CGI qw[:standard]'
29
31 "Module::Load" eliminates the need to know whether you are trying to
32 require either a file or a module.
33
34 If you consult "perldoc -f require" you will see that "require" will
35 behave differently when given a bareword or a string.
36
37 In the case of a string, "require" assumes you are wanting to load a
38 file. But in the case of a bareword, it assumes you mean a module.
39
40 This gives nasty overhead when you are trying to dynamically require
41 modules at runtime, since you will need to change the module notation
42 ("Acme::Comment") to a file notation fitting the particular platform
43 you are on.
44
45 "Module::Load" eliminates the need for this overhead and will just
46 DWYM.
47
48 Difference between "load" and "autoload"
49 "Module::Load" imports the two functions - "load" and "autoload"
50
51 "autoload" imports the default functions automatically, but "load" do
52 not import any functions.
53
54 "autoload" is usable under "BEGIN{};".
55
56 Both the functions can import the functions that are specified.
57
58 Following codes are same.
59
60 load File::Spec::Functions, qw/splitpath/;
61
62 autoload File::Spec::Functions, qw/splitpath/;
63
65 load
66 Loads a specified module.
67
68 See "Rules" for detailed loading rule.
69
70 autoload
71 Loads a specified module and imports the default functions.
72
73 Except importing the functions, 'autoload' is same as 'load'.
74
75 load_remote
76 Loads a specified module to the specified package.
77
78 use Module::Load 'load_remote';
79
80 my $pkg = 'Other::Package';
81
82 load_remote $pkg, 'Data::Dumper'; # load a module to 'Other::Package'
83 # but do not import 'Dumper' function
84
85 A module for loading must be quoted.
86
87 Except specifing the package and quoting module name, 'load_remote'
88 is same as 'load'.
89
90 autoload_remote
91 Loads a specified module and imports the default functions to the
92 specified package.
93
94 use Module::Load 'autoload_remote';
95
96 my $pkg = 'Other::Package';
97
98 autoload_remote $pkg, 'Data::Dumper'; # load a module to 'Other::Package'
99 # and imports 'Dumper' function
100
101 A module for loading must be quoted.
102
103 Except specifing the package and quoting module name,
104 'autoload_remote' is same as 'load_remote'.
105
107 All functions have the following rules to decide what it thinks you
108 want:
109
110 • If the argument has any characters in it other than those matching
111 "\w", ":" or "'", it must be a file
112
113 • If the argument matches only "[\w:']", it must be a module
114
115 • If the argument matches only "\w", it could either be a module or a
116 file. We will try to find "file.pm" first in @INC and if that
117 fails, we will try to find "file" in @INC. If both fail, we die
118 with the respective error messages.
119
121 'load' and 'autoload' are imported by default, but 'load_remote' and
122 'autoload_remote' are not imported.
123
124 To use 'load_remote' or 'autoload_remote', specify at 'use'.
125
126 "load","autoload","load_remote","autoload_remote"
127 Imports the selected functions.
128
129 # imports 'load' and 'autoload' (default)
130 use Module::Load;
131
132 # imports 'autoload' only
133 use Module::Load 'autoload';
134
135 # imports 'autoload' and 'autoload_remote', but don't import 'load';
136 use Module::Load qw/autoload autoload_remote/;
137
138 'all'
139 Imports all the functions.
140
141 use Module::Load 'all'; # imports load, autoload, load_remote, autoload_remote
142
143 '','none',undef
144 Not import any functions ("load" and "autoload" are not imported).
145
146 use Module::Load '';
147
148 use Module::Load 'none';
149
150 use Module::Load undef;
151
153 Because of a bug in perl (#19213), at least in version 5.6.1, we have
154 to hardcode the path separator for a require on Win32 to be "/", like
155 on Unix rather than the Win32 "\". Otherwise perl will not read its own
156 %INC accurately double load files if they are required again, or in the
157 worst case, core dump.
158
159 "Module::Load" cannot do implicit imports, only explicit imports. (in
160 other words, you always have to specify explicitly what you wish to
161 import from a module, even if the functions are in that modules'
162 @EXPORT)
163
165 Module::Runtime provides functions for loading modules, checking the
166 validity of a module name, converting a module name to partial ".pm"
167 path, and related utility functions.
168
169 "require" in perlfunc <https://metacpan.org/pod/perlfunc#require> and
170 "use" in perlfunc <https://metacpan.org/pod/perlfunc#use>.
171
172 Mojo::Loader is a "class loader and plugin framework", and is included
173 in the Mojolicious <https://metacpan.org/release/Mojolicious>
174 distribution.
175
176 Module::Loader is a module for finding and loading modules in a given
177 namespace, inspired by "Mojo::Loader".
178
180 Thanks to Jonas B. Nielsen for making explicit imports work.
181
183 Please report bugs or other issues to <bug-module-load@rt.cpan.org>.
184
186 This module by Jos Boumans <kane@cpan.org>.
187
189 This library is free software; you may redistribute and/or modify it
190 under the same terms as Perl itself.
191
192
193
194perl v5.38.0 2023-07-20 Module::Load(3)