The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Module::Loadable - Check if a module is loadable without actually loading it

VERSION

This document describes version 0.001 of Module::Loadable (from Perl distribution Module-Loadable), released on 2016-08-02.

SYNOPSIS

 use Module::Loadable qw(module_loadable module_source);

 # check if a module is available
 if (module_loadable "Foo::Bar") {
     # Foo::Bar is available
 } elsif (module_loadable "Foo/Baz.pm") {
     # Foo::Baz is available
 }

 # get a module's source code, dies on failure
 my $src = module_source("Foo/Baz.pm");

DESCRIPTION

To check if a module is loadable (available), generally the simplest way is to try to require() it:

 if (eval { require Foo::Bar; 1 }) {
     # Foo::Bar is available
 }

However, this actually loads the module. If a large number of modules need to be checked, this can potentially consume a lot of CPU time and memory.

Module::Loadable provides a routine module_loadable() which works like Perl's require but does not actually load the module.

FUNCTIONS

module_loadable($name) => bool

Check that module named $name is loadable, without actually loading it. $name will be converted from Foo::Bar format to Foo/Bar.pm.

It works by following the behavior of Perl's require, except the actual loading/executing part. First, it checks if $name is already in %INC, returning true immediately if that is the case. Then it will iterate each entry in @INC. If the entry is a coderef or object or arrayref, module_loadable() will treat it like a hook and call it like Perl's require() does as described in perlfunc. Otherwise, the entry will be treated like a directory name and the module's file will be searched on the filesystem.

module_source($name) => str

Return module's source code, without actually loading it. Die on failure.

HOMEPAGE

Please visit the project's homepage at https://metacpan.org/release/Module-Loadable.

SOURCE

Source repository is at https://github.com/perlancar/perl-Module-Loadable.

BUGS

Please report any bugs or feature requests on the bugtracker website https://rt.cpan.org/Public/Dist/Display.html?Name=Module-Loadable

When submitting a bug or request, please include a test-file or a patch to an existing test-file that illustrates the bug or desired feature.

SEE ALSO

Module::Path and Module::Path::More. These modules can also be used to check if a module on the filesystem is available. It iterates directories in @INC to try to find the module's file, but will not work with fatpacked (see App::FatPacker or Module::FatPack) or datapacked (see Module::DataPack) scripts or generally when there is a hook in @INC. Module::Loadable, on the other hand, handles require hook like Perl's require().

Also, those two modules at the time of this writing currently does not actually read the module file. In the case of, say, permission problem, those two will still return the path but the module might not actually readable.

AUTHOR

perlancar <perlancar@cpan.org>

COPYRIGHT AND LICENSE

This software is copyright (c) 2016 by perlancar@cpan.org.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.