MODULE::LOAD::CONDITIONAL(3perl)

๐ŸŒธ NAME ๐ŸŒธ

Module::Load::Conditional โ€” looking up module information, and loading modules at runtime ๐Ÿ’…โœจ

(Yeah, the header up top squishes it to Module...itional(3perl) because terminals have no chill about column width. It's still our girl. And it lives in the Perl Programmers Reference Guide ๐ŸŽ€)


โœจ SYNOPSIS โœจ

use Module::Load::Conditional qw[can_load check_install requires];

my $use_list = {
        CPANPLUS        => 0.05,
        LWP             => 5.60,
        'Test::More'    => undef,
};

print can_load( modules => $use_list )
        ? 'all modules loaded successfully'
        : 'failed to load required modules';

my $rv = check_install( module => 'LWP', version => 5.60 )
            or print 'LWP is not installed!';

print 'LWP up to date' if $rv->{uptodate};
print "LWP version is $rv->{version}\n";
print "LWP is installed as file $rv->{file}\n";

print "LWP requires the following modules to be installed:\n";
print join "\n", requires('LWP');

### allow M::L::C to peek in your %INC rather than just
### scanning @INC
$Module::Load::Conditional::CHECK_INC_HASH = 1;

### reset the 'can_load' cache
undef $Module::Load::Conditional::CACHE;

### don't have Module::Load::Conditional issue warnings --
### default is '1'
$Module::Load::Conditional::VERBOSE = 0;

### The last error that happened during a call to 'can_load'
my $err = $Module::Load::Conditional::ERROR;

๐Ÿ’– DESCRIPTION ๐Ÿ’–

Okay so Module::Load::Conditional gives you the cutest little ways to query โ€” and then possibly load โ€” literally any of the modules you've got installed on your system, all at runtime. No cap. ๐Ÿซถ

And here's the iconic part: it can load a whole squad of modules at once, or it can load none of them if even one of them refused to load. It also handles all the error checking and so forth for you, so you don't have to do it yourself. Main character energy, honestly. ๐Ÿ‘‘


๐Ÿ”ฎ Methods ๐Ÿ”ฎ

๐Ÿ’ซ $href = check_install( module => NAME [, version => VERSION, verbose => BOOL ] );

check_install is your vibe check for one specific question: is this module installed or not? You call it with these args, bestie:

  • module โ€” the name of the module you wanna verify. This one is a required key, no skipping ๐Ÿ’…
  • version โ€” the version this module needs to be. This one is optional ๐ŸŽ€
  • verbose โ€” whether or not to be verbose about what it's doing. Defaults to $Module::Load::Conditional::VERBOSE

So what does it give back? If it couldn't find where the module was installed, you get undef. But if it did find the file, you get a hash reference with these keys:

  • file โ€” full path to the file that contains the module ๐Ÿ—‚๏ธ
  • dir โ€” the directory, or more exactly the @INC entry, the module was loaded from
  • version โ€” the version number of the installed module. This is undef if the module had no version number (or an unparsable one), or if the variable $Module::Load::Conditional::FIND_VERSION was set to true. (Scroll down to the GLOBAL VARIABLES section for the tea ๐Ÿต)
  • uptodate โ€” a boolean telling you whether the module was found to be at least the version you specified. If you didn't specify a version, uptodate is always true as long as the module was found. And if no parsable version was found in the module, uptodate will also be true, because check_install had no way to verify it clearly. ๐Ÿคทโ€โ™€๏ธ
Also peep $Module::Load::Conditional::DEPRECATED, which affects the outcome of this value ๐Ÿ‘€

๐Ÿ’ซ $bool = can_load( modules => { NAME => VERSION [,NAME => VERSION] }, [verbose => BOOL, nocache => BOOL, autoload => BOOL] )

can_load takes a list of modules, optionally with version numbers attached, and figures out if it can load them. And the rule is ALL or NOTHING: if it can load ALL of them, it will. If one or more of them are unloadable, then none get loaded. Periodt. ๐Ÿ’€

This is so useful when you've got More Than One Way (tm) to solve a problem in your program, and you only wanna keep walking down a path if every module on that path could be loaded โ€” and not load them at all if they couldn't.

Under the hood it uses the load function or the autoload_remote function from Module::Load ๐Ÿงš

can_load takes these arguments:

  • modules โ€” a hashref of module/version pairs. The version is the minimum version to load. If no version is given, any version is assumed to be good enough. Slay. โœจ
  • verbose โ€” controls whether warnings get printed when a module fails to load. Default is to use the value of $Module::Load::Conditional::VERBOSE.
  • nocache โ€” can_load keeps its results in a cache, so it won't load the same module twice, and it won't even try to load a module that already failed to load before. By default can_load checks that cache, but you can override it by setting nocache to true. ๐Ÿšซ๐Ÿ—„๏ธ
  • autoload โ€” controls whether it imports the functions of a loaded module into the caller package. The default is no importing of any functions. For the details, go read the autoload function and the autoload_remote function from Module::Load ๐Ÿ‘‘

๐Ÿ’ซ @list = requires( MODULE );

requires tells you what other modules a particular module requires. This is lowkey perfect when you're writing a module for public release and you're listing out its prerequisites ๐Ÿ“

requires takes but one argument: the name of a module. It will then first check if it can actually load that module, and return undef if it can't. Otherwise, it returns a list of modules and pragmas that would have been loaded on that module's behalf. ๐ŸŒˆ

Note: the list require returns originated from your current perl and your current install. iykyk.

๐Ÿงš Global Variables ๐Ÿงš

The behaviour of Module::Load::Conditional can be altered by changing these global variables:

๐ŸŒŸ $Module::Load::Conditional::VERBOSE

This controls whether Module::Load::Conditional issues warnings and explanations about why certain things may have failed. Set it to 0 and Module::Load::Conditional will not output any warnings. The default is 0;

๐Ÿ™ˆ Okay so fun little detail: the SYNOPSIS up top says the default is '1', and the POD down here says the default is 0. Both of those sentences are straight from the source, so make of that what you will. Not my business, I just report. ๐Ÿ’…

๐ŸŒŸ $Module::Load::Conditional::FIND_VERSION

This controls whether Module::Load::Conditional tries to parse (and eval) the version out of the module you're trying to load. ๐Ÿ”

If you don't wanna do that, set this variable to false. But understand: version comparisons become impossible, and Module::Load::Conditional can't tell you what module version you have installed. From a security or performance point of view, that might be exactly what you want. Note that the $FIND_VERSION code runs safely under taint mode. ๐Ÿ›ก๏ธ

The default is 1;

๐ŸŒŸ $Module::Load::Conditional::CHECK_INC_HASH

This controls whether Module::Load::Conditional checks your %INC hash to see if a module is available. By default, only @INC gets scanned to see if a module is physically on your filesystem, or available via an @INC-hook. Set this variable to true and it'll trust any entries in %INC and hand them right back to you. ๐Ÿซถ

The default is 0;

๐ŸŒŸ $Module::Load::Conditional::FORCE_SAFE_INC

This controls whether Module::Load::Conditional sanitises @INC by removing ".". The current default setting is 0, but heads up โ€” this may change in a future release. ๐Ÿ‘€

๐ŸŒŸ $Module::Load::Conditional::CACHE

This holds the cache of the can_load function. If you explicitly wanna nuke the current cache, just set this variable to undef. ๐Ÿ’ฅ

๐ŸŒŸ $Module::Load::Conditional::ERROR

This holds a string of the last error that happened during a call to can_load. Super useful to inspect when can_load returns undef. ๐Ÿšจ

๐ŸŒŸ $Module::Load::Conditional::DEPRECATED

This controls whether Module::Load::Conditional checks if a dual-life core module has been deprecated. Set it to true and check_install will return false for uptodate if a dual-life module is found to be loaded from $Config{privlibexp} ๐Ÿ˜ญ

The default is 0;


๐ŸŒˆ See Also ๐ŸŒˆ

Module::Load


๐Ÿ› BUG REPORTS ๐Ÿ›

Found a bug or some other issue? Please report it to <bug-module-load-conditional@rt.cpan.org>. Do it, bestie, they wanna hear from you ๐Ÿซถ


๐Ÿ‘‘ AUTHOR ๐Ÿ‘‘

This module is by Jos Boumans <kane@cpan.org>. Iconic behaviour.


This library is free software; you may redistribute and/or modify it under the same terms as Perl itself. โœจ


Okay that's the whole page, bestie! ๐ŸŽ€ This one's from perl v5.44.0, dated 2026-10-17 โ€” the Module...itional(3perl) page from the Perl Programmers Reference Guide. Go forth and conditionally load things, you absolute legend ๐Ÿ’…๐Ÿ’–๐Ÿฆ‹

๐Ÿ“ฆ where this page comes from
  • arch rolling: perl 5.44.0-1 ยท Artistic-1.0-Perl AND GPL-1.0-or-later
  • ๐Ÿ’– girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms