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;
33 __PACKAGE__->_export_to_level(1, $mod, @_) if @_;
36 ### 5.004's Exporter doesn't have export_to_level.
37 ### Taken from Michael Schwerns Test::More and slightly modified
38 sub _export_to_level {
42 my $callpkg = caller($level);
44 $mod->export($callpkg, @_);
51 my @parts = split /::/;
53 ### because of [perl #19213], see caveats ###
54 my $file = $^O eq 'MSWin32'
56 : File::Spec->catfile( @parts );
58 $file .= '.pm' if $pm;
63 sub _who { (caller(1))[0] }
82 Module::Load - runtime require of both modules and files
88 my $module = 'Data:Dumper';
89 load Data::Dumper; # loads that module
90 load 'Data::Dumper'; # ditto
93 my $script = 'some/script.pl'
95 load 'some/script.pl'; # use quotes because of punctuations
97 load thing; # try 'thing' first, then 'thing.pm'
99 load CGI, ':standard' # like 'use CGI qw[:standard]'
104 C<load> eliminates the need to know whether you are trying to require
105 either a file or a module.
107 If you consult C<perldoc -f require> you will see that C<require> will
108 behave differently when given a bareword or a string.
110 In the case of a string, C<require> assumes you are wanting to load a
111 file. But in the case of a bareword, it assumes you mean a module.
113 This gives nasty overhead when you are trying to dynamically require
114 modules at runtime, since you will need to change the module notation
115 (C<Acme::Comment>) to a file notation fitting the particular platform
118 C<load> eliminates the need for this overhead and will just DWYM.
122 C<load> has the following rules to decide what it thinks you want:
128 If the argument has any characters in it other than those matching
129 C<\w>, C<:> or C<'>, it must be a file
133 If the argument matches only C<[\w:']>, it must be a module
137 If the argument matches only C<\w>, it could either be a module or a
138 file. We will try to find C<file> first in C<@INC> and if that fails,
139 we will try to find C<file.pm> in @INC.
140 If both fail, we die with the respective error messages.
146 Because of a bug in perl (#19213), at least in version 5.6.1, we have
147 to hardcode the path separator for a require on Win32 to be C</>, like
148 on Unix rather than the Win32 C<\>. Otherwise perl will not read its
149 own %INC accurately double load files if they are required again, or
150 in the worst case, core dump.
152 C<Module::Load> cannot do implicit imports, only explicit imports.
153 (in other words, you always have to specify explicitly what you wish
154 to import from a module, even if the functions are in that modules'
159 This module by Jos Boumans E<lt>kane@cpan.orgE<gt>.
161 Thanks to Jonas B. Nielsen for making explicit imports work.
166 copyright (c) 2002 Jos Boumans E<lt>kane@cpan.orgE<gt>.
169 This library is free software;
170 you may redistribute and/or modify it under the same
171 terms as Perl itself.