Require-Util
view release on metacpan or search on metacpan
lib/Require/Util.pm view on Meta::CPAN
my $func_err;
for my $mod (@_) {
my $mod_pm = $mod;
if ($mod =~ /\A\w+(::\w+)*\z/) {
($mod_pm = "$mod.pm") =~ s!::!/!g;
}
try {
require $mod_pm;
} catch ($e) {
$func_err = $e;
next;
};
return $mod;
}
die $func_err;
}
sub try_require {
my $mod = shift;
my $mod_pm = $mod;
if ($mod =~ /\A\w+(::\w+)*\z/) {
($mod_pm = "$mod.pm") =~ s!::!/!g;
}
try {
require $mod_pm;
} catch ($e) {
$Require::Util::err = $e;
return 0;
};
1;
}
1;
# ABSTRACT: Utilities related to require()
__END__
=pod
=encoding UTF-8
=head1 NAME
Require::Util - Utilities related to require()
=head1 VERSION
This document describes version 0.002 of Require::Util (from Perl distribution Require-Util), released on 2021-06-29.
=head1 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";
=head1 DESCRIPTION
B<EXPERIMENTAL.>
=head1 FUNCTIONS
=head2 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 C<Foo::Bar>
syntax or as C<Foo/Bar.pm> syntax. Unlike C<require()> which just returns true,
upon success the function will return the module name that gets loaded.
=head2 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 C<require> statement in a
C<try/catch> block), returning true on success or false on failure (detail error
message is stored in C<$Require::Util::err>. Module name can be specified as
C<Foo::Bar> syntax or as C<Foo/Bar.pm> syntax.
=head1 HOMEPAGE
Please visit the project's homepage at L<https://metacpan.org/release/Require-Util>.
=head1 SOURCE
Source repository is at L<https://github.com/perlancar/perl-Require-Util>.
=head1 BUGS
Please report any bugs or feature requests on the bugtracker website L<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.
=head1 SEE ALSO
=head2 Alternatives for C<try_require>
To simply check whether the module source is available without actually loading
a file, use C<module_installed> from L<Module::Installed::Tiny> or
C<check_install> from L<Module::Load::Conditional>. Module like
L<Module::Path::More> can also be used if you want to ignore C<@INC> hooks.
=head2 Alternatives for C<require_any>
Note that C<can_load> from C<Module::Load::Conditional> loads I<all> modules
instead of just one.
=head1 AUTHOR
perlancar <perlancar@cpan.org>
=head1 COPYRIGHT AND LICENSE
This software is copyright (c) 2021 by perlancar@cpan.org.
( run in 1.202 second using v1.01-cache-2.11-cpan-39bf76dae61 )