This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Update Module-Load to CPAN version 0.18
[perl5.git] / cpan / Module-Load / lib / Module / Load.pm
1 package Module::Load;
2
3 $VERSION = '0.18';
4
5 use strict;
6 use File::Spec ();
7
8 sub import {
9     my $who = _who();
10
11     {   no strict 'refs';
12         *{"${who}::load"} = *load;
13     }
14 }
15
16 sub 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     }
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     }
44 }
45
46 sub _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;
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';
65
66     return $file;
67 }
68
69 sub _who { (caller(1))[0] }
70
71 sub _is_file {
72     local $_ = shift;
73     return  /^\./               ? 1 :
74             /[^\w:']/           ? 1 :
75             undef
76     #' silly bbedit..
77 }
78
79
80 1;
81
82 __END__
83
84 =pod
85
86 =head1 NAME
87
88 Module::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
110 C<load> eliminates the need to know whether you are trying to require
111 either a file or a module.
112
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.
115
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.
118
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
122 you are on.
123
124 C<load> eliminates the need for this overhead and will just DWYM.
125
126 =head1 Rules
127
128 C<load> has the following rules to decide what it thinks you want:
129
130 =over 4
131
132 =item *
133
134 If the argument has any characters in it other than those matching
135 C<\w>, C<:> or C<'>, it must be a file
136
137 =item *
138
139 If the argument matches only C<[\w:']>, it must be a module
140
141 =item *
142
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.pm> first in C<@INC> and if that
145 fails, we will try to find C<file> in @INC.  If both fail, we die with
146 the respective error messages.
147
148 =back
149
150 =head1 Caveats
151
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.
157
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'
161 C<@EXPORT>)
162
163 =head1 ACKNOWLEDGEMENTS
164
165 Thanks to Jonas B. Nielsen for making explicit imports work.
166
167 =head1 BUG REPORTS
168
169 Please report bugs or other issues to E<lt>bug-module-load@rt.cpan.org<gt>.
170
171 =head1 AUTHOR
172
173 This module by Jos Boumans E<lt>kane@cpan.orgE<gt>.
174
175 =head1 COPYRIGHT
176
177 This library is free software; you may redistribute and/or modify it 
178 under the same terms as Perl itself.
179
180
181 =cut