Module:...ntation(3)
bestie, welcome to the Module:...ntation(3) page from User Contributed Perl Documentation β we're about to make implementation-loading make sense, no cap. π
π NAME π
Module::Implementation β loads one of several alternate underlying implementations for a module. That's the whole main-character job. π«Ά
β¨ VERSION β¨
version 0.09
π¦ SYNOPSIS π¦
package Foo::Bar;
use Module::Implementation;
BEGIN {
my $loader = Module::Implementation::build_loader_sub(
implementations => [ 'XS', 'PurePerl' ],
symbols => [ 'run', 'check' ],
);
$loader->();
}
package Consumer;
# loads the first viable implementation
use Foo::Bar;
πΈ DESCRIPTION πΈ
okay so Module::Implementation abstracts out the process of choosing one of several underlying implementations for a module. It can be used to provide XS and pure Perl implementations of a module, or it could be used to load an implementation for a given OS or any other case of needing to provide multiple implementations. literally versatile icon behavior. β¨
This module is only useful when you know all the implementations ahead of time. If you want to load arbitrary implementations then you probably want something like a plugin system, not this module. fr fr β it's not trying to be a whole plugin situation. π
π API π
This module provides two subroutines, neither of which are exported. iykyk β you call them fully qualified. π
π« Module::Implementation::build_loader_sub(...)
This subroutine takes the following arguments.
implementationsβ this should be an array reference of implementation names. Each name should correspond to a module in the caller's namespace. In other words, using the example in the"SYNOPSIS", this module will look for the"Foo::Bar::XS"and"Foo::Bar::PurePerl"modules. this argument is required. πsymbolsβ a list of symbols to copy from the implementation package to the calling package. These can be prefixed with a variable type:"$","@","%","&", or"*)". If no prefix is given, the symbol is assumed to be a subroutine. this argument is optional. π
This subroutine returns the implementation loader as a sub reference. It is up to you to call this loader sub in your code. π
π real talk: I recommend that you do not call this loader in animport()sub. If a caller explicitly requests no imports, yourimport()sub will not be run at all, which can cause weird breakage. not me warning you about chaos and then handing you the vibe check anyway. π«Ά
π§ Module::Implementation::implementation_for($package)
Given a package name, this subroutine returns the implementation that was loaded for the package. This is not a full package name, just the suffix that identifies the implementation. For the "SYNOPSIS" example, this subroutine would be called as Module::Implementation::implementation_for('Foo::Bar'), and it would return "XS" or "PurePerl". periodt. π
π₯ HOW THE IMPLEMENTATION LOADER WORKS π₯
The implementation loader works like this ... π
First, it checks for an %ENV var specifying the implementation to load. The env var is based on the package name which loads the implementations. The "::" package separator is replaced with "_", and made entirely upper-case. Finally, we append "_IMPLEMENTATION" to this name. the way that naming scheme is doing the most, honestly. β¨
So in our "SYNOPSIS" example, the corresponding %ENV key would be "FOO_BAR_IMPLEMENTATION". π«Ά
If this is set, then the loader will only try to load this one implementation. If the env var requests an implementation which doesn't match one of the implementations specified when the loader was created, an error is thrown. and not the cute kind of thrown. π
If this one implementation fails to load then the loader throws an error. This is useful for testing. You can request a specific implementation in a test file by writing something like this:
BEGIN { $ENV{FOO_BAR_IMPLEMENTATION} = 'XS' }
use Foo::Bar;
If the environment variable is not set, then the loader simply tries the implementations originally passed to "Module::Implementation". The implementations are tried in the order in which they were originally passed. order matters, bestie. β
The loader will use the first implementation that loads without an error. It will copy any requested symbols from this implementation. slay, first viable implementation. π
If none of the implementations can be loaded, then the loader throws an exception. π
The loader returns the name of the package it loaded. that's the whole flow, and it ate. π
π AUTHOR π
Dave Rolsky <autarch@urth.org> π
π COPYRIGHT AND LICENSE π
This software is Copyright (c) 2014 by Dave Rolsky.
This is free software, licensed under:
The Artistic License 2.0 (GPL Compatible)
π that's the whole page, bestie β this one's from perl v5.42.0, dated 2015-04-13. now go pick an implementation and be iconic. π§β¨
π¦ where this page comes from
- opensuse-leap 16.0: perl-Module-Implementation 0.09-160000.2.2 Β· Artistic-2.0
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms