Breaking module dependency chains
use Bar::Dummy qw(); use Module::Optional Bar;
This module provides a way of using a module which may or may not be installed on the target machine. If the module is available it behaves as a straight use. If the module is not available, subs are repointed to their equivalents in a dummy namespace.
Suppose you are the developer of module \*(C`Foo\*(C', which uses functionality from the highly controversial module \*(C`Bar\*(C'. You actually quite like \*(C`Bar\*(C', and want to reuse its functionality in your \*(C`Foo\*(C' module. But, many people will refuse to install \*(C`Foo\*(C' as it needs \*(C`Bar\*(C'. Maybe \*(C`Bar\*(C' is failing tests or is misbehaving on some platforms.
Making \*(C`Bar\*(C' an optional module will allow users to run \*(C`Foo\*(C' that don't have \*(C`Bar\*(C' installed. For Module::Build users, this involves changing the status of the \*(C`Bar\*(C' dependency from \*(C`requires\*(C' to \*(C`recommends\*(C'.
To use this module, you need to set up a namespace \*(C`Bar::Dummy\*(C'. 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 \*(C`Params::Validate\*(C' is shipped with Module::Optional, as this was the original motivation for the module. If there are other common candidates for dummying, petition me, and I'll include them in the Module::Optional distribution.
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 - this is to avoid warnings about redefined subroutines if the real Bar is installed on the target machine. Module::Optional will do the importing: quux wibble and wobble from the real Bar if it exists, or from Bar::Dummy if it doesn't.
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.
You will probably be developing your module on a platform that does have Bar installed (I hope). However, 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); }
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.
An example of a dummy module is Params::Validate::Dummy, provided in this distribution.
Module::Optional performs two types of redirection for the missing module. Firstly via @ISA inheritance - Foo::Bar inherits from Foo::Bar::Dummy.
Secondly, an \s-1AUTOLOAD\s0 method is added to Foo::Bar, which will catch calls to subs in this namespace.
Please report bugs to rt.cpan.org by posting to [email protected] or visiting https://rt.cpan.org/Public/Dist/Display.html?Name=Module-Optional.
Ivor Williams ivorw-mod-opt at xemaps.com
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 \s-1LICENSE\s0 file included with this module.
Test::MockModule, Module::Pluggable, Module::Build.