NAME

optional - Pragma to optionally load a module (or pick from a list of modules) and provide a constant and some tools for taking action depending on if it loaded or not.

DESCRIPTION

Helps write code that has optional dependencies. Will load (or not load) the module, then provide you with some tools that help you write logic that depends on the result.

If a module fails to load for any reason other than its absence then the exception will be rethrown to show the error.

SYNOPSIS

# Will try to load Optional::Module::Foo, naming tools with the 'opt_foo' name
use optional opt_foo => qw/Optional::Module::Foo/;

# Will try to load Optional::Module::Bar first, but will fallback to
# Optional::Module::Foo if the first is not installed.
use optional opt_any => qw/Optional::Module::Bar Optional::Module::Foo/;

# You get a constant (capitlized version of name) that you can use in conditionals
if (OPT_FOO) { ... }

# The constant will return the module name that was loaded, if any, undef
# if it was not loaded.
if (my $mod = OPT_FOO) { ... }

# Quickly write code that will only execute if the module was loaded
# If the module was not loaded this always returns undef, so it is safe to
# use in a hash building list.
my $result = if_opt_foo { ... };

# Quickly write code that will only execute if the module was NOT loaded If
# the module was loaded this always returns undef, so it is safe to use in
# a hash building list.
my $result = unless_opt_foo { ... };

sub feature_that_requires_foo {
    need_opt_foo();                                       # Throws an error telling the user they need to install Optional::Module::Foo to use this feature
    need_opt_foo(feature => 'widget');                    # Same, but names the feature
    need_opt_foo(trace   => 1);                           # Add a stack trace
    need_opt_foo(message => ...);                         # Write a custom message
    need_opt_foo(message => ..., append_modules => 1);    # Write a custom message, and add the module/s that need to be installed
}

EXPORTS

For each use of this module you will get 4 subs exported into your namespace, all contain the NAME intially provided.

SOURCE

The source code repository for 'optional' can be found at https://github.com/exodist/optional/.

MAINTAINERS

AUTHORS

COPYRIGHT

Copyright Chad Granum exodist7@gmail.com.

This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

See http://dev.perl.org/licenses/