Fixes for ext/compress
[p5sagit/p5-mst-13.2.git] / lib / Module / Load.pm
CommitLineData
0819efc5 1package Module::Load;
2
26f467e2 3$VERSION = '0.16';
0819efc5 4
5use strict;
6use File::Spec ();
7
8sub import {
9 my $who = _who();
10
11 { no strict 'refs';
12 *{"${who}::load"} = *load;
13 }
14}
15
16sub load (*;@) {
17 my $mod = shift or return;
18 my $who = _who();
19
20 if( _is_file( $mod ) ) {
21 require $mod;
22 } else {
23 LOAD: {
24 my $err;
25 for my $flag ( qw[1 0] ) {
26 my $file = _to_file( $mod, $flag);
27 eval { require $file };
28 $@ ? $err .= $@ : last LOAD;
29 }
30 die $err if $err;
31 }
32 }
85a8a980 33
34 ### This addresses #41883: Module::Load cannot import
35 ### non-Exporter module. ->import() routines weren't
36 ### properly called when load() was used.
37 { no strict 'refs';
38 my $import;
39 if (@_ and $import = $mod->can('import')) {
40 unshift @_, $mod;
41 goto &$import;
42 }
43 }
0819efc5 44}
45
46sub _to_file{
47 local $_ = shift;
48 my $pm = shift || '';
49
50 my @parts = split /::/;
51
52 ### because of [perl #19213], see caveats ###
53 my $file = $^O eq 'MSWin32'
54 ? join "/", @parts
55 : File::Spec->catfile( @parts );
56
57 $file .= '.pm' if $pm;
0df8d4ee 58
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';
0819efc5 65
66 return $file;
67}
68
69sub _who { (caller(1))[0] }
70
71sub _is_file {
72 local $_ = shift;
73 return /^\./ ? 1 :
74 /[^\w:']/ ? 1 :
75 undef
76 #' silly bbedit..
77}
78
79
801;
81
82__END__
83
84=pod
85
86=head1 NAME
87
88Module::Load - runtime require of both modules and files
89
90=head1 SYNOPSIS
91
92 use Module::Load;
93
94 my $module = 'Data:Dumper';
95 load Data::Dumper; # loads that module
96 load 'Data::Dumper'; # ditto
97 load $module # tritto
98
99 my $script = 'some/script.pl'
100 load $script;
101 load 'some/script.pl'; # use quotes because of punctuations
102
103 load thing; # try 'thing' first, then 'thing.pm'
104
105 load CGI, ':standard' # like 'use CGI qw[:standard]'
106
107
108=head1 DESCRIPTION
109
110C<load> eliminates the need to know whether you are trying to require
111either a file or a module.
112
113If you consult C<perldoc -f require> you will see that C<require> will
114behave differently when given a bareword or a string.
115
116In the case of a string, C<require> assumes you are wanting to load a
117file. But in the case of a bareword, it assumes you mean a module.
118
119This gives nasty overhead when you are trying to dynamically require
120modules at runtime, since you will need to change the module notation
121(C<Acme::Comment>) to a file notation fitting the particular platform
122you are on.
123
124C<load> eliminates the need for this overhead and will just DWYM.
125
126=head1 Rules
127
128C<load> has the following rules to decide what it thinks you want:
129
130=over 4
131
132=item *
133
134If the argument has any characters in it other than those matching
135C<\w>, C<:> or C<'>, it must be a file
136
137=item *
138
139If the argument matches only C<[\w:']>, it must be a module
140
141=item *
142
143If the argument matches only C<\w>, it could either be a module or a
144file. We will try to find C<file> first in C<@INC> and if that fails,
145we will try to find C<file.pm> in @INC.
146If both fail, we die with the respective error messages.
147
148=back
149
150=head1 Caveats
151
152Because of a bug in perl (#19213), at least in version 5.6.1, we have
153to hardcode the path separator for a require on Win32 to be C</>, like
154on Unix rather than the Win32 C<\>. Otherwise perl will not read its
155own %INC accurately double load files if they are required again, or
156in the worst case, core dump.
157
158C<Module::Load> cannot do implicit imports, only explicit imports.
159(in other words, you always have to specify explicitly what you wish
160to import from a module, even if the functions are in that modules'
161C<@EXPORT>)
162
0df8d4ee 163=head1 ACKNOWLEDGEMENTS
164
165Thanks to Jonas B. Nielsen for making explicit imports work.
166
167=head1 BUG REPORTS
168
169Please report bugs or other issues to E<lt>bug-module-load@rt.cpan.org<gt>.
170
0819efc5 171=head1 AUTHOR
172
173This module by Jos Boumans E<lt>kane@cpan.orgE<gt>.
174
0819efc5 175=head1 COPYRIGHT
176
0df8d4ee 177This library is free software; you may redistribute and/or modify it
178under the same terms as Perl itself.
0819efc5 179
0819efc5 180
181=cut