Module::Installed - Check whether a module, or a file's list of includes are installed.
use Module::Installed qw(module_installed includes_installed) my $module = 'Mock::Sub'; # module_installed() if (module_installed($module)) { require $module; $module->import; ... } else { warn "$module is not installed..."; } # includes_installed() my $includes = includes_installed('perl_file_with_includes.pl'); for my $inc_name (keys %$includes) { my $statement = $includes->{$inc_name} ? "is installed" : "isn't installed"; print "$inc_name $statement\n"; }
Verifies whether or not a module or a file's list of includes are installed.
Checks whether a module is installed on your system.
Parameters:
$name
Mandatory, String: The name of the module to check against (eg: Mock::Sub).
Mock::Sub
Returns: True (1) if the module is found, and false (0) if not.
1
0
$callback
Optional, code reference: A callback that we'll execute on each call.
The callback will receive three parameters:
$module: The name of the module in question (eg: Mock::Sub).
$module_file: The file name of the module (can be used with require).
require
$installed: Bool indicating whether the module is installed or not.
my @modules = qw(Mock::Sub Devel::Trace); for (@modules) { module_installed($_, \&cb); } sub cb { my ($module, $module_file, $installed) = @_; if ($installed) { require $module_file; $module->import; } else { warn "Module $module not installed... skipping"; } }
This function reads in a Perl file, strips out all of its includes (use and require), and checks whether each one is installed on the system.
use
Note: This function requires PPI to be installed. If it is, we'll load it and proceed. If it isn't, we croak().
croak()
$file
Mandatory, String: The name of a Perl file.
Optional, code reference: A reference to a subroutine where you can perform actions on the modules you're checking. See "module_installed($name, $callback)" in callback.
Returns: A hash reference where the found included modules' name as the key, and for the value, true (1) if the module is installed and false (0) if not.
This module was pretty well copy/pasted from Module::Installed::Tiny, but without the significant dependency chain required by that distribution's test suite.
Steve Bertrand <steveb@cpan.org>
This software is copyright (c) 2020 by Steve Bertrand
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
To install Module::Installed, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Module::Installed
CPAN shell
perl -MCPAN -e shell install Module::Installed
For more information on module installation, please visit the detailed CPAN module installation guide.