NAME

Module::Installed - Check whether a module, or a file's list of includes are installed.

Coverage Status

SYNOPSIS

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";
}

DESCRIPTION

Verifies whether or not a module or a file's list of includes are installed.

FUNCTIONS

module_installed($name, $callback)

Checks whether a module is installed on your system.

Parameters:

$name

Mandatory, String: The name of the module to check against (eg: Mock::Sub).

Returns: True (1) if the module is found, and false (0) if not.

$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).

$installed: Bool indicating whether the module is installed or not.

Callback Example

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";
    }
}

includes_installed($file, $callback)

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.

Note: This function requires PPI to be installed. If it is, we'll load it and proceed. If it isn't, we croak().

Parameters:

$file

Mandatory, String: The name of a Perl file.

$callback

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.

SEE ALSO

This module was pretty well copy/pasted from Module::Installed::Tiny, but without the significant dependency chain required by that distribution's test suite.

AUTHOR

Steve Bertrand <steveb@cpan.org>

COPYRIGHT AND LICENSE

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.