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