NAME

Require::Util - Utilities related to require()

VERSION

This document describes version 0.002 of Require::Util (from Perl distribution Require-Util), released on 2021-06-29.

SYNOPSIS

use Require::Util qw(require_any try_require);

if (try_require "Foo::Bar") {
    # use Foo::Bar's function
}

my $modname = require_any "Foo::Bar", "Baz/Qux.pm", "Quux";

DESCRIPTION

EXPERIMENTAL.

FUNCTIONS

require_any

Usage:

my $res = require_any $modname1, $modname2, ...;

Example:

my $res = require_any "Foo::Bar", "Baz/Qux.pm";

Require modules listed as arguments, stopping after the first success. If all modules cannot be loaded, will die. Module name can be specified as Foo::Bar syntax or as Foo/Bar.pm syntax. Unlike require() which just returns true, upon success the function will return the module name that gets loaded.

try_require

Usage:

my $res = try_require $modname;

Example:

my $res = try_require "Foo::Bar";
my $res = try_require "Foo/Bar.pm";

Try requiring specified module (wrapping the require statement in a try/catch block), returning true on success or false on failure (detail error message is stored in $Require::Util::err. Module name can be specified as Foo::Bar syntax or as Foo/Bar.pm syntax.

HOMEPAGE

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

SOURCE

Source repository is at https://github.com/perlancar/perl-Require-Util.

BUGS

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

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

Alternatives for try_require

To simply check whether the module source is available without actually loading a file, use module_installed from Module::Installed::Tiny or check_install from Module::Load::Conditional. Module like Module::Path::More can also be used if you want to ignore @INC hooks.

Alternatives for require_any

Note that can_load from Module::Load::Conditional loads all modules instead of just one.

AUTHOR

perlancar <perlancar@cpan.org>

COPYRIGHT AND LICENSE

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