Module::Optional(3pm)
π Module::Optional π
sooo this is the <strong>Module::Optional</strong> page from the User Contributed Perl Documentation π β breaking module dependency chains, bestie. no cap, it's giving freedom from That One Module.
β¨ NAME β¨
Module::Optional β breaking module dependency chains π
πΈ SYNOPSIS πΈ
use Bar::Dummy qw();
use Module::Optional Bar;
that's it. that's the whole vibe. π¦
π«Ά ABSTRACT π«Ά
okay so basically: this module lets you use a module that might or might not be installed on the target machine. vibe check time.
- if the module IS there β it behaves like a straight
use, literally no drama - if the module is NOT there β the subs get repointed to their equivalents in a dummy namespace πͺ
π DESCRIPTION π
so picture this. you're the developer of module Foo, right? and Foo uses functionality from the highly controversial module Bar. π
and like β you actually quite like Bar! you wanna reuse its functionality in your Foo module. but SO many people refuse to install Foo because it needs Bar. maybe Bar is failing tests. maybe Bar is misbehaving on some platforms. it's giving messy. π
making Bar an optional module will let users run Foo even when they don't have Bar installed. iconic behaviour honestly β¨
for Module::Build users, this involves changing the status of the Bar dependency from requires to recommends. π
to use this module, you need to set up a namespace Bar::Dummy. the recommended way of doing this is to ship lib/Bar/Dummy.pm with your module. π this could be shipped as a standalone module. a dummy module for Params::Validate is shipped with Module::Optional, because that was the original motivation for the module, fr fr. and if there are other common candidates for dummying β petition me π β and I'll include them in the Module::Optional distribution.
π Using an optional module π
place the lines of code in the following order:
use Bar::Dummy qw();
use Module::Optional qw(Bar quux wibble wobble);
ALWAYS set up the dummy module first β but don't import anything!! π
ββοΈ that's specifically to avoid warnings about redefined subroutines if the real Bar is installed on the target machine. the way that Module::Optional will do the importing for you: quux, wibble and wobble from the real Bar if it exists, or from Bar::Dummy if it doesn't. π
π Asking for a module version π
if you need a version of the module or later, this can be done thus:
use Bar::Dummy qw();
use Module::Optional qw(Bar 0.07 quux wibble wobble);
if version 0.07 or later of Bar is not available, the dummy is used. no version, no problem, we pivot. π
π¦ Suppressing the module π¦
you will probably be developing your module on a platform that does have Bar installed (I hope π). BUT β you need to be able to tell what happens on systems without Bar. to do this, run the following (example is Unix):
MODULE_OPTIONAL_SKIP=1 make test
you also want to do this in tests for the dummy module that you are providing. (you are providing tests for this module? π) this can easily be done with a BEGIN block at the top of the test:
BEGIN {
local $ENV{MODULE_OPTIONAL_SKIP} = 1;
use Module::Optional qw(Params::Validate);
}
π§ Writing a ::Dummy Module π§
you provide a namespace suffixed with ::Dummy containing subs corresponding to all the subs and method calls for the optional module. you should also provide the same exports as the module itself performs. π¦
Adhere strictly to any prototypes in the optional module. this is not a drill bestie, do not freestyle the prototypes. π€
an example of a dummy module is Params::Validate::Dummy, provided in this distribution. β¨
π₯ INTERNALS π₯
Module::Optional performs two types of redirection for the missing module:
- firstly via
@ISAinheritance βFoo::Barinherits fromFoo::Bar::Dummy. 𧬠- secondly, an
AUTOLOADmethod is added toFoo::Bar, which will catch calls to subs in this namespace. πΈοΈ
π BUGS π
please report bugs to rt.cpan.org by posting to bugs-module-optional@rt.cpan.org or visiting https://rt.cpan.org/Public/Dist/Display.html?Name=Module-Optional. the maintainers will love you for it, genuinely. π«Ά
π AUTHOR π
Ivor Williams
ivorw-mod-opt at xemaps.com
π COPYRIGHT π
this program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. π
the full text of the license can be found in the LICENSE file included with this module. π
β SEE ALSO β
Test::MockModuleModule::PluggableModule::Build
okay that's the whole page, bestie, go forth and make your dependencies optional π β¨ this was <strong>perl v5.32.0</strong>, dated <strong>2021-01-07</strong>, and it lives in the <strong>Module::Optional(3pm)</strong> household β slay, periodt, see you in the next manpage ππ¦π
π¦ where this page comes from
- debian bookworm: libmodule-optional-perl 0.03-1.1
- debian trixie: libmodule-optional-perl 0.03-1.1
- ubuntu jammy: libmodule-optional-perl 0.03-1.1
- ubuntu noble: libmodule-optional-perl 0.03-1.1
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms