Best(3pm)

πŸ’– Best β€” like, load modules with fallback, bestie πŸ’–

okay so this is the manual for Best, a Perl module that lives in the User Contributed Perl Documentation manual section (that's the 3pm part, iykyk). Buckle up, we're gonna make module-loading make sense. πŸ¦‹


🌸 NAME 🌸

Best β€” Load modules with fallback. periodt.


✨ 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 🧚

So like, often there are several possible providers of some functionality your program needs, but you literally don't know which one is available at the run site. πŸ™ˆ For example, one of the modules may be implemented with XS, or it's not in the core Perl distribution and thus not necessarily installed. classic.

Best attempts to load modules from a list, stopping at the first successful load and failing only if literally no alternative was found. it's giving "we'll figure it out." πŸ’…


πŸ‘‘ FUNCTIONS πŸ‘‘

Most of the functionality Best provides is on the use line; there is only one callable method as such (see "which" 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/ ];

πŸ“¦ IMPORT LISTS πŸ“¦

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 ("use Some::Module ()"), pass a zero-element listref as the argument list. In the pathological case where you really want to load a module and pass it [] as an argument, specify [ [] ] as the argument list to Best. 😭 (pathological, she said. iconic.)

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

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

To customize the import list for a module, use the args 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/ ];

πŸ”’ MINIMUM VERSIONS πŸ”’

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 version key. no cap.

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 if. The module will only be loaded if your function returns a true value. vibe check before you commit, love that for us. ✨

βœ… 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 ok. 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. it's giving "not you, next." πŸ’€

🎭 ARBITRARY CODE 🎭

A code reference may be substituted for module names. It will be called instead of attempting to load a module. You may do literally anything you wish in this code. It will be skipped if your code throws an exception or returns false. main character energy in a coderef, honestly. πŸ”₯

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 OO modules--you want to know which module Best has successfully loaded. Call Best->which with the first in your list of module alternatives; the return value is a string containing the name of the loaded module. πŸ’œ

use Best qw/YAML::Syck YAML/;

print "Fallback" if Best->which('YAML::Syck') eq 'YAML';

πŸš€ 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 Best.pm would be available, since it isn't a core module itself. πŸ‘€ valid question bestie.

One solution is to use Inline::Module to inline Best.pm 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 XS modules, though, you may need to stick with us.) 😌

Best.pm is pure Perl and a single module with a convenient license, so you can also just drop it in your project's lib directory. so cozy. πŸ“


πŸ”— SEE ALSO πŸ”—

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

πŸ’Œ AUTHORS πŸ’Œ

  • Gaal Yahas, <gaal at forum2.org>
  • Joshua ben Jore, <jjore at cpan.org> has made some significant contributions.

🩺 DIAGNOSTICS 🩺

  • What modules shall I load? β€” Best wasn't given a list of modules to load.
  • No viable module found: %s β€” 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 TRACE_BEST and DEBUG_BEST.

Enabling TRACE_BEST also enables the debugging code. πŸ‘‘

πŸ› BUGS πŸ›

Please report any bugs or feature requests to bug-template-patch at rt.cpan.org, 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:


Copyright (C) 2006-2024 Gaal Yahas

This program is distributed under the MIT (X11) License: <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
"Software"), 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.

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

okay that's the whole thing!! 🌈 this page ships as part of perl v5.40.0, and this version is dated 2024-09-21. go forth and fallback gracefully, bestie πŸ’–βœ¨πŸŽ€πŸ¦‹

πŸ“¦ where this page comes from