GSP
Quick Navigator

Search Site

Unix VPS
A - Starter
B - Basic
C - Preferred
D - Commercial
MPS - Dedicated
Previous VPSs
* Sign Up! *

Support
Contact Us
Online Help
Handbooks
Domain Status
Man Pages

FAQ
Virtual Servers
Pricing
Billing
Technical

Network
Facilities
Connectivity
Topology Map

Miscellaneous
Server Agreement
Year 2038
Credits
 

USA Flag

 

 

Man Pages
Module::Installed::Tiny(3) User Contributed Perl Documentation Module::Installed::Tiny(3)

Module::Installed::Tiny - Check if a module is installed, with as little code as possible

This document describes version 0.005 of Module::Installed::Tiny (from Perl distribution Module-Installed-Tiny), released on 2021-05-19.

 use Module::Installed::Tiny qw(module_installed module_source);

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

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

To check if a module is installed (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. There are some cases where this is not desirable: 1) we have to check a lot of modules (actually loading the modules will take a lot of CPU time and memory; 2) some of the modules conflict with one another and cannot all be loaded; 3) the module is OS specific and might not load under another OS; 4) we simply do not want to execute the module, for security or other reasons.

"Module::Installed::Tiny" provides a routine "module_installed()" which works like Perl's "require" but does not actually load the module.

This module does not require any other module except Exporter.

Check that module named $name is available to load. This means that: either the module file exists on the filesystem and searchable in @INC and the contents of the file can be retrieved, or when there is a require hook in @INC, the module's source can be retrieved from the hook.

Note that this does not guarantee that the module can eventually be loaded successfully, as there might be syntax or runtime errors in the module's source. To check for that, one would need to actually load the module using "require".

Return module's source code, without actually loading it. Die on failure (e.g. module named $name not found in @INC).

In list context:

 my @res = module_source($name);

will return the list:

(str, source_name)

where "str" is the module source code and "source_name" is source information (file path, or the @INC ref entry when entry is a ref).

Wrap in "eval()" or "try/catch" (Perl 5.34+):

 my $src;
 eval { $src = module_source $name };
 # $src contains the module source or undef if not available

This is what "module_installed()" does.

Please visit the project's homepage at <https://metacpan.org/release/Module-Installed-Tiny>.

Source repository is at <https://github.com/perlancar/perl-Module-Installed-Tiny>.

Please report any bugs or feature requests on the bugtracker website <https://github.com/perlancar/perl-Module-Installed-Tiny/issues>

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.

Module::Load::Conditional provides "check_install" which also does what "module_installed" does, plus can check module version. It also has a couple other knobs to customize its behavior. It's less tiny than Module::Installed::Tiny though.

Module::Path and Module::Path::More. These modules can also be used to check if a module on the filesystem is available. They do not handle require hooks, nor do they actually check that the module file is readable.

perlancar <perlancar@cpan.org>

This software is copyright (c) 2021, 2020, 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.

2021-05-19 perl v5.32.1

Search for    or go to Top of page |  Section 3 |  Main Index

Powered by GSP Visit the GSP FreeBSD Man Page Interface.
Output converted with ManDoc.