xref: /openbsd-src/gnu/usr.bin/perl/cpan/Module-Load/lib/Module/Load.pm (revision 256a93a44f36679bee503f12e49566c2183f6181)
1b39c5158Smillertpackage Module::Load;
2b39c5158Smillert
3b39c5158Smillertuse strict;
46fb12b70Safresh1use warnings;
5b39c5158Smillertuse File::Spec ();
6b39c5158Smillert
7*256a93a4Safresh1our $VERSION = '0.36';
8*256a93a4Safresh1
9*256a93a4Safresh1
10b39c5158Smillertsub import {
11b39c5158Smillert    my $who = _who();
126fb12b70Safresh1    my $h; shift;
13b39c5158Smillert
14b39c5158Smillert    {   no strict 'refs';
156fb12b70Safresh1
166fb12b70Safresh1        @_ or (
176fb12b70Safresh1            *{"${who}::load"} = \&load, # compat to prev version
186fb12b70Safresh1            *{"${who}::autoload"} = \&autoload,
196fb12b70Safresh1            return
206fb12b70Safresh1        );
216fb12b70Safresh1
226fb12b70Safresh1        map { $h->{$_} = () if defined $_ } @_;
236fb12b70Safresh1
246fb12b70Safresh1        (exists $h->{none} or exists $h->{''})
256fb12b70Safresh1            and shift, last;
266fb12b70Safresh1
276fb12b70Safresh1        ((exists $h->{autoload} and shift,1) or (exists $h->{all} and shift))
286fb12b70Safresh1            and *{"${who}::autoload"} = \&autoload;
296fb12b70Safresh1
306fb12b70Safresh1        ((exists $h->{load} and shift,1) or exists $h->{all})
316fb12b70Safresh1            and *{"${who}::load"} = \&load;
326fb12b70Safresh1
336fb12b70Safresh1        ((exists $h->{load_remote} and shift,1) or exists $h->{all})
346fb12b70Safresh1            and *{"${who}::load_remote"} = \&load_remote;
356fb12b70Safresh1
366fb12b70Safresh1        ((exists $h->{autoload_remote} and shift,1) or exists $h->{all})
376fb12b70Safresh1            and *{"${who}::autoload_remote"} = \&autoload_remote;
386fb12b70Safresh1
39b39c5158Smillert    }
406fb12b70Safresh1
41b39c5158Smillert}
42b39c5158Smillert
43b39c5158Smillertsub load(*;@){
446fb12b70Safresh1    goto &_load;
456fb12b70Safresh1}
466fb12b70Safresh1
476fb12b70Safresh1sub autoload(*;@){
486fb12b70Safresh1    unshift @_, 'autoimport';
496fb12b70Safresh1    goto &_load;
506fb12b70Safresh1}
516fb12b70Safresh1
526fb12b70Safresh1sub load_remote($$;@){
536fb12b70Safresh1    my ($dst, $src, @exp) = @_;
546fb12b70Safresh1
556fb12b70Safresh1    eval "package $dst;Module::Load::load('$src', qw/@exp/);";
566fb12b70Safresh1    $@ && die "$@";
576fb12b70Safresh1}
586fb12b70Safresh1
596fb12b70Safresh1sub autoload_remote($$;@){
606fb12b70Safresh1    my ($dst, $src, @exp) = @_;
616fb12b70Safresh1
626fb12b70Safresh1    eval "package $dst;Module::Load::autoload('$src', qw/@exp/);";
636fb12b70Safresh1    $@ && die "$@";
646fb12b70Safresh1}
656fb12b70Safresh1
666fb12b70Safresh1sub _load{
676fb12b70Safresh1    my $autoimport = $_[0] eq 'autoimport' and shift;
68b39c5158Smillert    my $mod = shift or return;
69b39c5158Smillert    my $who = _who();
70b39c5158Smillert
71b39c5158Smillert    if( _is_file( $mod ) ) {
72b39c5158Smillert        require $mod;
73b39c5158Smillert    } else {
74b39c5158Smillert        LOAD: {
75b39c5158Smillert            my $err;
76b39c5158Smillert            for my $flag ( qw[1 0] ) {
77b39c5158Smillert                my $file = _to_file( $mod, $flag);
78b39c5158Smillert                eval { require $file };
79b39c5158Smillert                $@ ? $err .= $@ : last LOAD;
80b39c5158Smillert            }
81b39c5158Smillert            die $err if $err;
82b39c5158Smillert        }
83b39c5158Smillert    }
84b39c5158Smillert
85b39c5158Smillert    ### This addresses #41883: Module::Load cannot import
86b39c5158Smillert    ### non-Exporter module. ->import() routines weren't
87b39c5158Smillert    ### properly called when load() was used.
886fb12b70Safresh1
89b39c5158Smillert    {   no strict 'refs';
90b39c5158Smillert        my $import;
916fb12b70Safresh1
926fb12b70Safresh1    ((@_ or $autoimport) and (
936fb12b70Safresh1        $import = $mod->can('import')
946fb12b70Safresh1        ) and (
956fb12b70Safresh1        unshift(@_, $mod),
96f3efcd01Safresh1        goto &$import
976fb12b70Safresh1        )
986fb12b70Safresh1    );
99b39c5158Smillert    }
1006fb12b70Safresh1
101b39c5158Smillert}
102b39c5158Smillert
103b39c5158Smillertsub _to_file{
104b39c5158Smillert    local $_    = shift;
105b39c5158Smillert    my $pm      = shift || '';
106b39c5158Smillert
107898184e3Ssthen    ## trailing blanks ignored by default. [rt #69886]
10891f110e0Safresh1    my @parts = split /::|'/, $_, -1;
109898184e3Ssthen    ## make sure that we can't hop out of @INC
110898184e3Ssthen    shift @parts if @parts && !$parts[0];
111b39c5158Smillert
112b39c5158Smillert    ### because of [perl #19213], see caveats ###
113b39c5158Smillert    my $file = $^O eq 'MSWin32'
114b39c5158Smillert                    ? join "/", @parts
115b39c5158Smillert                    : File::Spec->catfile( @parts );
116b39c5158Smillert
117b39c5158Smillert    $file   .= '.pm' if $pm;
118b39c5158Smillert
119b39c5158Smillert    ### on perl's before 5.10 (5.9.5@31746) if you require
120b39c5158Smillert    ### a file in VMS format, it's stored in %INC in VMS
121b39c5158Smillert    ### format. Therefor, better unixify it first
122b39c5158Smillert    ### Patch in reply to John Malmbergs patch (as mentioned
123b39c5158Smillert    ### above) on p5p Tue 21 Aug 2007 04:55:07
124b39c5158Smillert    $file = VMS::Filespec::unixify($file) if $^O eq 'VMS';
125b39c5158Smillert
126b39c5158Smillert    return $file;
127b39c5158Smillert}
128b39c5158Smillert
129b39c5158Smillertsub _who { (caller(1))[0] }
130b39c5158Smillert
131b39c5158Smillertsub _is_file {
132b39c5158Smillert    local $_ = shift;
133b39c5158Smillert    return  /^\./               ? 1 :
134b39c5158Smillert            /[^\w:']/           ? 1 :
135b39c5158Smillert            undef
136b39c5158Smillert    #' silly bbedit..
137b39c5158Smillert}
138b39c5158Smillert
139b39c5158Smillert
140b39c5158Smillert1;
141b39c5158Smillert
142b39c5158Smillert__END__
143b39c5158Smillert
144b39c5158Smillert=pod
145b39c5158Smillert
146b39c5158Smillert=head1 NAME
147b39c5158Smillert
148b39c5158SmillertModule::Load - runtime require of both modules and files
149b39c5158Smillert
150b39c5158Smillert=head1 SYNOPSIS
151b39c5158Smillert
152b39c5158Smillert  use Module::Load;
153b39c5158Smillert
1546fb12b70Safresh1  my $module = 'Data::Dumper';
1556fb12b70Safresh1
1566fb12b70Safresh1  load Data::Dumper;     # loads that module, but not import any functions
1576fb12b70Safresh1                         # -> cannot use 'Dumper' function
1586fb12b70Safresh1
159b39c5158Smillert  load 'Data::Dumper';   # ditto
160b39c5158Smillert  load $module           # tritto
161b39c5158Smillert
1626fb12b70Safresh1  autoload Data::Dumper; # loads that module and imports the default functions
1636fb12b70Safresh1                         # -> can use 'Dumper' function
1646fb12b70Safresh1
165b39c5158Smillert  my $script = 'some/script.pl'
166b39c5158Smillert  load $script;
167b39c5158Smillert  load 'some/script.pl';  # use quotes because of punctuations
168b39c5158Smillert
169b39c5158Smillert  load thing;             # try 'thing' first, then 'thing.pm'
170b39c5158Smillert
1716fb12b70Safresh1  load CGI, ':all';       # like 'use CGI qw[:standard]'
172b39c5158Smillert
173b39c5158Smillert=head1 DESCRIPTION
174b39c5158Smillert
1756fb12b70Safresh1C<Module::Load> eliminates the need to know whether you are trying
1766fb12b70Safresh1to require either a file or a module.
177b39c5158Smillert
178b39c5158SmillertIf you consult C<perldoc -f require> you will see that C<require> will
179b39c5158Smillertbehave differently when given a bareword or a string.
180b39c5158Smillert
181b39c5158SmillertIn the case of a string, C<require> assumes you are wanting to load a
182b39c5158Smillertfile. But in the case of a bareword, it assumes you mean a module.
183b39c5158Smillert
184b39c5158SmillertThis gives nasty overhead when you are trying to dynamically require
185b39c5158Smillertmodules at runtime, since you will need to change the module notation
186b39c5158Smillert(C<Acme::Comment>) to a file notation fitting the particular platform
187b39c5158Smillertyou are on.
188b39c5158Smillert
1896fb12b70Safresh1C<Module::Load> eliminates the need for this overhead and will
1906fb12b70Safresh1just DWYM.
1916fb12b70Safresh1
1926fb12b70Safresh1=head2 Difference between C<load> and C<autoload>
1936fb12b70Safresh1
1946fb12b70Safresh1C<Module::Load> imports the two functions - C<load> and C<autoload>
1956fb12b70Safresh1
1966fb12b70Safresh1C<autoload> imports the default functions automatically,
1976fb12b70Safresh1but C<load> do not import any functions.
1986fb12b70Safresh1
1996fb12b70Safresh1C<autoload> is usable under C<BEGIN{};>.
2006fb12b70Safresh1
2016fb12b70Safresh1Both the functions can import the functions that are specified.
2026fb12b70Safresh1
2036fb12b70Safresh1Following codes are same.
2046fb12b70Safresh1
2056fb12b70Safresh1  load File::Spec::Functions, qw/splitpath/;
2066fb12b70Safresh1
2076fb12b70Safresh1  autoload File::Spec::Functions, qw/splitpath/;
2086fb12b70Safresh1
2096fb12b70Safresh1=head1 FUNCTIONS
2106fb12b70Safresh1
2116fb12b70Safresh1=over 4
2126fb12b70Safresh1
2136fb12b70Safresh1=item load
2146fb12b70Safresh1
2156fb12b70Safresh1Loads a specified module.
2166fb12b70Safresh1
2176fb12b70Safresh1See L</Rules> for detailed loading rule.
2186fb12b70Safresh1
2196fb12b70Safresh1=item autoload
2206fb12b70Safresh1
2216fb12b70Safresh1Loads a specified module and imports the default functions.
2226fb12b70Safresh1
2236fb12b70Safresh1Except importing the functions, 'autoload' is same as 'load'.
2246fb12b70Safresh1
2256fb12b70Safresh1=item load_remote
2266fb12b70Safresh1
2276fb12b70Safresh1Loads a specified module to the specified package.
2286fb12b70Safresh1
2296fb12b70Safresh1  use Module::Load 'load_remote';
2306fb12b70Safresh1
2316fb12b70Safresh1  my $pkg = 'Other::Package';
2326fb12b70Safresh1
2336fb12b70Safresh1  load_remote $pkg, 'Data::Dumper'; # load a module to 'Other::Package'
2346fb12b70Safresh1                                    # but do not import 'Dumper' function
2356fb12b70Safresh1
2366fb12b70Safresh1A module for loading must be quoted.
2376fb12b70Safresh1
2386fb12b70Safresh1Except specifing the package and quoting module name,
2396fb12b70Safresh1'load_remote' is same as 'load'.
2406fb12b70Safresh1
2416fb12b70Safresh1=item autoload_remote
2426fb12b70Safresh1
2436fb12b70Safresh1Loads a specified module and imports the default functions to the specified package.
2446fb12b70Safresh1
2456fb12b70Safresh1  use Module::Load 'autoload_remote';
2466fb12b70Safresh1
2476fb12b70Safresh1  my $pkg = 'Other::Package';
2486fb12b70Safresh1
2496fb12b70Safresh1  autoload_remote $pkg, 'Data::Dumper'; # load a module to 'Other::Package'
2506fb12b70Safresh1                                        # and imports 'Dumper' function
2516fb12b70Safresh1
2526fb12b70Safresh1A module for loading must be quoted.
2536fb12b70Safresh1
2546fb12b70Safresh1Except specifing the package and quoting module name,
2556fb12b70Safresh1'autoload_remote' is same as 'load_remote'.
2566fb12b70Safresh1
2576fb12b70Safresh1=back
258b39c5158Smillert
259b39c5158Smillert=head1 Rules
260b39c5158Smillert
2616fb12b70Safresh1All functions have the following rules to decide what it thinks
2626fb12b70Safresh1you want:
263b39c5158Smillert
264b39c5158Smillert=over 4
265b39c5158Smillert
266b39c5158Smillert=item *
267b39c5158Smillert
268b39c5158SmillertIf the argument has any characters in it other than those matching
269b39c5158SmillertC<\w>, C<:> or C<'>, it must be a file
270b39c5158Smillert
271b39c5158Smillert=item *
272b39c5158Smillert
273b39c5158SmillertIf the argument matches only C<[\w:']>, it must be a module
274b39c5158Smillert
275b39c5158Smillert=item *
276b39c5158Smillert
277b39c5158SmillertIf the argument matches only C<\w>, it could either be a module or a
278898184e3Ssthenfile. We will try to find C<file.pm> first in C<@INC> and if that
279898184e3Ssthenfails, we will try to find C<file> in @INC.  If both fail, we die with
280898184e3Ssthenthe respective error messages.
281b39c5158Smillert
282b39c5158Smillert=back
283b39c5158Smillert
2846fb12b70Safresh1=head1 IMPORTS THE FUNCTIONS
2856fb12b70Safresh1
2866fb12b70Safresh1'load' and 'autoload' are imported by default, but 'load_remote' and
2876fb12b70Safresh1'autoload_remote' are not imported.
2886fb12b70Safresh1
2896fb12b70Safresh1To use 'load_remote' or 'autoload_remote', specify at 'use'.
2906fb12b70Safresh1
2916fb12b70Safresh1=over 4
2926fb12b70Safresh1
2936fb12b70Safresh1=item "load","autoload","load_remote","autoload_remote"
2946fb12b70Safresh1
2956fb12b70Safresh1Imports the selected functions.
2966fb12b70Safresh1
2976fb12b70Safresh1  # imports 'load' and 'autoload' (default)
2986fb12b70Safresh1  use Module::Load;
2996fb12b70Safresh1
3006fb12b70Safresh1  # imports 'autoload' only
3016fb12b70Safresh1  use Module::Load 'autoload';
3026fb12b70Safresh1
3036fb12b70Safresh1  # imports 'autoload' and 'autoload_remote', but don't import 'load';
3046fb12b70Safresh1  use Module::Load qw/autoload autoload_remote/;
3056fb12b70Safresh1
3066fb12b70Safresh1=item 'all'
3076fb12b70Safresh1
3086fb12b70Safresh1Imports all the functions.
3096fb12b70Safresh1
3106fb12b70Safresh1  use Module::Load 'all'; # imports load, autoload, load_remote, autoload_remote
3116fb12b70Safresh1
3126fb12b70Safresh1=item '','none',undef
3136fb12b70Safresh1
3146fb12b70Safresh1Not import any functions (C<load> and C<autoload> are not imported).
3156fb12b70Safresh1
3166fb12b70Safresh1  use Module::Load '';
3176fb12b70Safresh1
3186fb12b70Safresh1  use Module::Load 'none';
3196fb12b70Safresh1
3206fb12b70Safresh1  use Module::Load undef;
3216fb12b70Safresh1
3226fb12b70Safresh1=back
3236fb12b70Safresh1
324b39c5158Smillert=head1 Caveats
325b39c5158Smillert
326b39c5158SmillertBecause of a bug in perl (#19213), at least in version 5.6.1, we have
327b39c5158Smillertto hardcode the path separator for a require on Win32 to be C</>, like
328b39c5158Smillerton Unix rather than the Win32 C<\>. Otherwise perl will not read its
329b39c5158Smillertown %INC accurately double load files if they are required again, or
330b39c5158Smillertin the worst case, core dump.
331b39c5158Smillert
332b39c5158SmillertC<Module::Load> cannot do implicit imports, only explicit imports.
333b39c5158Smillert(in other words, you always have to specify explicitly what you wish
334b39c5158Smillertto import from a module, even if the functions are in that modules'
335b39c5158SmillertC<@EXPORT>)
336b39c5158Smillert
337f3efcd01Safresh1=head1 SEE ALSO
338f3efcd01Safresh1
339f3efcd01Safresh1L<Module::Runtime> provides functions for loading modules,
340f3efcd01Safresh1checking the validity of a module name,
341f3efcd01Safresh1converting a module name to partial C<.pm> path,
342f3efcd01Safresh1and related utility functions.
343f3efcd01Safresh1
344f3efcd01Safresh1L<"require" in perlfunc|https://metacpan.org/pod/perlfunc#require>
345f3efcd01Safresh1and
346f3efcd01Safresh1L<"use" in perlfunc|https://metacpan.org/pod/perlfunc#use>.
347f3efcd01Safresh1
348f3efcd01Safresh1L<Mojo::Loader> is a "class loader and plugin framework",
349f3efcd01Safresh1and is included in the
350f3efcd01Safresh1L<Mojolicious|https://metacpan.org/release/Mojolicious> distribution.
351f3efcd01Safresh1
352f3efcd01Safresh1L<Module::Loader> is a module for finding and loading modules
353f3efcd01Safresh1in a given namespace, inspired by C<Mojo::Loader>.
354f3efcd01Safresh1
355f3efcd01Safresh1
356b39c5158Smillert=head1 ACKNOWLEDGEMENTS
357b39c5158Smillert
358b39c5158SmillertThanks to Jonas B. Nielsen for making explicit imports work.
359b39c5158Smillert
360b39c5158Smillert=head1 BUG REPORTS
361b39c5158Smillert
362*256a93a4Safresh1Please report bugs or other issues to E<lt>bug-module-load@rt.cpan.orgE<gt>.
363b39c5158Smillert
364b39c5158Smillert=head1 AUTHOR
365b39c5158Smillert
366b39c5158SmillertThis module by Jos Boumans E<lt>kane@cpan.orgE<gt>.
367b39c5158Smillert
368b39c5158Smillert=head1 COPYRIGHT
369b39c5158Smillert
370b39c5158SmillertThis library is free software; you may redistribute and/or modify it
371b39c5158Smillertunder the same terms as Perl itself.
372b39c5158Smillert
373b39c5158Smillert=cut
374