Require::Util - Utilities related to require()
This document describes version 0.002 of Require::Util (from Perl distribution Require-Util), released on 2021-06-29.
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";
EXPERIMENTAL.
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.
Foo::Bar
Foo/Bar.pm
require()
my $res = try_require $modname;
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.
require
try/catch
$Require::Util::err
Please visit the project's homepage at https://metacpan.org/release/Require-Util.
Source repository is at https://github.com/perlancar/perl-Require-Util.
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.
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.
module_installed
check_install
@INC
require_any
Note that can_load from Module::Load::Conditional loads all modules instead of just one.
can_load
Module::Load::Conditional
perlancar <perlancar@cpan.org>
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.
To install Require::Util, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Require::Util
CPAN shell
perl -MCPAN -e shell install Require::Util
For more information on module installation, please visit the detailed CPAN module installation guide.