SYNOPSIS

    # Load the best available YAML module with default imports
    use Best qw/YAML::Syck YAML/;
    use Best [ qw/YAML::Syck YAML/ ];   # also works

    # Load a YAML module and import some symbols
    use Best [ [ qw/YAML::Syck YAML/ ], qw/DumpFile LoadFile/ ];

    # And fancier stuff...

    # Load a new enough YAML module
    use Best qw/YAML 0.58 YAML::Syck/;
    use Best [ qw/YAML 0.58 YAML::Syck/ ];
    use Best [ [ 'YAML' => { version => '0.58' },
                 'YAML::Syck' ] ];

    # Don't load too-new YAML module and import DumpFile
    use Best [ [ 'YAML' => { ok => sub { YAML->VERSION <= 0.23 } },
                 'YAML::Syck', ],
               qw/DumpFile/ ];

    # Use the best Carp module w/ different parameter lists
    use Best [ [ 'Carp::Clan' => { args => [] },
                 'Carp' ],
               qw/croak confess carp cluck/ ];

    # Choose alternate implementations
    use Best [ [ 'My::Memoize' => { if => sub { $] <= 5.006 } },
                 'Memoize' ],
               qw/memoize/ ];

    # Load a CGI module but import nothing
    use Best [ [ qw/CGI::Simple CGI/ ], [] ];      # akin to 'use CGI ()'

DESCRIPTION

Often there are several possible providers of some functionality your program needs, but you don't know which is available at the run site. For example, one of the modules may be implemented with \s-1XS\s0, or not in the core Perl distribution and thus not necessarily installed.

Best attempts to load modules from a list, stopping at the first successful load and failing only if no alternative was found.

FUNCTIONS

Most of the functionality Best provides is on the \*(C`use\*(C' line; there is only one callable functions as such (see \*(C`which\*(C' below)

If the arguments are either a simple list or a reference to a simple list, the elements are taken to be module names and are loaded in order with their default import function called. Any exported symbols are installed in the caller package.

use Best qw/A Simple List/; use Best [ qw/A Simple List/ ];

\s-1IMPORT\s0 \s-1LISTS\s0

If the arguments are a listref with a listref as its first element, this interior list is treated as the specification of modules to attempt loading, in order; the rest of the arguments are treated as options to pass on to the loaded module's import function.

use Best [ [ qw/A Simple List/ ], qw/Argument list goes here/ ]; use Best [ [ qw/A Simple List/ ], [ qw/Argument list goes here/ ] ];

To specify a null import (\*(C`use Some::Module ()\*(C'), pass a zero-element listref as the argument list. In the pathological case where you really want to load a module and pass it \*(C`[]\*(C' as an argument, specify \*(C`[ [] ]\*(C' as the argument list to Best.

# use Module (); use Best [ [ 'Module' ], [] ];

# use Module ( [] ); use Best [ [ 'Module' ], [[]] ];

To customize the import list for a module, use the \*(C`args\*(C' parameter in a hash reference following the module's name.

# use Carp::Clan; # use Carp qw/carp croak confess cluck/; use Best [ [ 'Carp::Clan' => { args => [] }, 'Carp' ], qw/carp croak confess cluck/ ];

\s-1MINIMUM\s0 \s-1VERSIONS\s0

You can specify a minimum version for a module by following the module name with something that looks like a number or by a hash reference with a \*(C`version\*(C' key.

use Best [ [ YAML => '0.58', 'YAML::Syck' ] ];

use Best [ [ YAML => { version => '0.58' }, 'YAML::Syck' ] ];

PRE-VALIDATION

use Best Module => { if => CODEREF };

You may prevent Best from attempting to load a module by providing a function as a parameter to \*(C`if\*(C'. The module will only be loaded if your function returns a true value.

POST-VALIDATION

use Best Module => { ok => CODEREF };

You may prevent Best from settling on a successfully loaded module by providing a function as a parameter to \*(C`ok\*(C'. Best will follow all of its normal rules to attempt to load your module but can be told to continue retrying if your function returns false.

\s-1ARBITRARY\s0 \s-1CODE\s0

A code reference may be substituted for module names. It will be called instead of attempting to load a module. You may do anything you wish in this code. It will be skipped if your code throws an exception or returns false.

use Best [ sub { # Decline return; }, sub { # Oops! die 'Some error'; }, 'Bad::Module', sub { # Ok! return 1; }, ];

which

In some cases\*(--for example, class methods in \s-1OO\s0 modules\*(--you want to know which module Best has successfully loaded. Call \*(C`Best->which\*(C' with the first in your list of module alternatives; the return value is a string containing the name of the loaded module.

DEPLOYMENT ISSUES

If you want to use Best because you aren't sure your target machine has some modules installed, you may wonder what might warrant the assumption that \*(C`Best.pm\*(C' would be available, since it isn't a core module itself.

One solution is to use Inline::Module to inline \*(C`Best.pm\*(C' in your source code. If you don't know this module, check it out \*(-- after you learn what it does, you may decide you don't need Best at all! (If your fallback list includes \s-1XS\s0 modules, though, you may need to stick with us.)

\*(C`Best.pm\*(C' is pure Perl and a single module with a convenient license, so you can also just drop it in your project's \*(C`lib\*(C' directory.

RELATED TO Best…

Module::Load
UNIVERSAL::require
Inline::Module

AUTHORS

Gaal Yahas, \*(C`<gaal at forum2.org>\*(C'

Joshua ben Jore, \*(C`<jjore at cpan.org>\*(C' has made some significant contributions.

DIAGNOSTICS

What modules shall I load?

\*(C`Best\*(C' wasn't given a list of modules to load. None of the module alternatives loaded.

Something's wrong!

An assertion failed. This means that either there is a bug in the data you fed to Best or a bug in Best.

The code is scattered with assertions and debugging output that can be enabled by putting a true value in the environment variables \*(C`TRACE_BEST\*(C' and \*(C`DEBUG_BEST\*(C'.

Enabling \*(C`TRACE_BEST\*(C' also enables the debugging code.

BUGS

Please report any bugs or feature requests to \*(C`bug-template-patch at rt.cpan.org\*(C', or through the web interface at <http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Best>. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.

SUPPORT

You can find documentation for this module with the perldoc command.

perldoc Best

You can also contact the maintainer at the address above or look for information at:

  • AnnoCPAN: Annotated \s-1CPAN\s0 documentation <http://annocpan.org/dist/Best/>

  • \s-1CPAN\s0 Ratings <http://cpanratings.perl.org/d/Best/>

  • \s-1RT:\s0 \s-1CPAN\s0's request tracker <http://rt.cpan.org/NoAuth/Bugs.html?Dist=Best>

  • Search \s-1CPAN\s0 <http://search.cpan.org/dist/Best/>

  • Source repository <https://github.com/gaal/best> Copyright (C) 2006-2012 Gaal Yahas

This program is distributed under the \s-1MIT\s0 (X11) License: http://www.opensource.org/licenses/mit-license.php <http://www.opensource.org/licenses/mit-license.php>

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the \*(L"Software\*(R"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

\s-1THE\s0 \s-1SOFTWARE\s0 \s-1IS\s0 \s-1PROVIDED\s0 \*(L"\s-1AS\s0 \s-1IS\s0\*(R", \s-1WITHOUT\s0 \s-1WARRANTY\s0 \s-1OF\s0 \s-1ANY\s0 \s-1KIND\s0, \s-1EXPRESS\s0 \s-1OR\s0 \s-1IMPLIED\s0, \s-1INCLUDING\s0 \s-1BUT\s0 \s-1NOT\s0 \s-1LIMITED\s0 \s-1TO\s0 \s-1THE\s0 \s-1WARRANTIES\s0 \s-1OF\s0 \s-1MERCHANTABILITY\s0, \s-1FITNESS\s0 \s-1FOR\s0 A \s-1PARTICULAR\s0 \s-1PURPOSE\s0 \s-1AND\s0 \s-1NONINFRINGEMENT\s0. \s-1IN\s0 \s-1NO\s0 \s-1EVENT\s0 \s-1SHALL\s0 \s-1THE\s0 \s-1AUTHORS\s0 \s-1OR\s0 \s-1COPYRIGHT\s0 \s-1HOLDERS\s0 \s-1BE\s0 \s-1LIABLE\s0 \s-1FOR\s0 \s-1ANY\s0 \s-1CLAIM\s0, \s-1DAMAGES\s0 \s-1OR\s0 \s-1OTHER\s0 \s-1LIABILITY\s0, \s-1WHETHER\s0 \s-1IN\s0 \s-1AN\s0 \s-1ACTION\s0 \s-1OF\s0 \s-1CONTRACT\s0, \s-1TORT\s0 \s-1OR\s0 \s-1OTHERWISE\s0, \s-1ARISING\s0 \s-1FROM\s0, \s-1OUT\s0 \s-1OF\s0 \s-1OR\s0 \s-1IN\s0 \s-1CONNECTION\s0 \s-1WITH\s0 \s-1THE\s0 \s-1SOFTWARE\s0 \s-1OR\s0 \s-1THE\s0 \s-1USE\s0 \s-1OR\s0 \s-1OTHER\s0 \s-1DEALINGS\s0 \s-1IN\s0 \s-1THE\s0 \s-1SOFTWARE\s0.