12 *{"${who}::load"} = *load;
17 my $mod = shift or return;
20 if( _is_file( $mod ) ) {
25 for my $flag ( qw[1 0] ) {
26 my $file = _to_file( $mod, $flag);
27 eval { require $file };
28 $@ ? $err .= $@ : last LOAD;
34 ### This addresses #41883: Module::Load cannot import
35 ### non-Exporter module. ->import() routines weren't
36 ### properly called when load() was used.
39 if (@_ and $import = $mod->can('import')) {
50 my @parts = split /::/;
52 ### because of [perl #19213], see caveats ###
53 my $file = $^O eq 'MSWin32'
55 : File::Spec->catfile( @parts );
57 $file .= '.pm' if $pm;
59 ### on perl's before 5.10 (5.9.5@31746) if you require
60 ### a file in VMS format, it's stored in %INC in VMS
61 ### format. Therefor, better unixify it first
62 ### Patch in reply to John Malmbergs patch (as mentioned
63 ### above) on p5p Tue 21 Aug 2007 04:55:07
64 $file = VMS::Filespec::unixify($file) if $^O eq 'VMS';
69 sub _who { (caller(1))[0] }
88 Module::Load - runtime require of both modules and files
94 my $module = 'Data:Dumper';
95 load Data::Dumper; # loads that module
96 load 'Data::Dumper'; # ditto
99 my $script = 'some/script.pl'
101 load 'some/script.pl'; # use quotes because of punctuations
103 load thing; # try 'thing' first, then 'thing.pm'
105 load CGI, ':standard' # like 'use CGI qw[:standard]'
110 C<load> eliminates the need to know whether you are trying to require
111 either a file or a module.
113 If you consult C<perldoc -f require> you will see that C<require> will
114 behave differently when given a bareword or a string.
116 In the case of a string, C<require> assumes you are wanting to load a
117 file. But in the case of a bareword, it assumes you mean a module.
119 This gives nasty overhead when you are trying to dynamically require
120 modules at runtime, since you will need to change the module notation
121 (C<Acme::Comment>) to a file notation fitting the particular platform
124 C<load> eliminates the need for this overhead and will just DWYM.
128 C<load> has the following rules to decide what it thinks you want:
134 If the argument has any characters in it other than those matching
135 C<\w>, C<:> or C<'>, it must be a file
139 If the argument matches only C<[\w:']>, it must be a module
143 If the argument matches only C<\w>, it could either be a module or a
144 file. We will try to find C<file> first in C<@INC> and if that fails,
145 we will try to find C<file.pm> in @INC.
146 If both fail, we die with the respective error messages.
152 Because of a bug in perl (#19213), at least in version 5.6.1, we have
153 to hardcode the path separator for a require on Win32 to be C</>, like
154 on Unix rather than the Win32 C<\>. Otherwise perl will not read its
155 own %INC accurately double load files if they are required again, or
156 in the worst case, core dump.
158 C<Module::Load> cannot do implicit imports, only explicit imports.
159 (in other words, you always have to specify explicitly what you wish
160 to import from a module, even if the functions are in that modules'
163 =head1 ACKNOWLEDGEMENTS
165 Thanks to Jonas B. Nielsen for making explicit imports work.
169 Please report bugs or other issues to E<lt>bug-module-load@rt.cpan.org<gt>.
173 This module by Jos Boumans E<lt>kane@cpan.orgE<gt>.
177 This library is free software; you may redistribute and/or modify it
178 under the same terms as Perl itself.