1Module::Implementation(U3s)er Contributed Perl DocumentatMioodnule::Implementation(3)
2
3
4
6 Module::Implementation - Loads one of several alternate underlying
7 implementations for a module
8
10 version 0.09
11
13 package Foo::Bar;
14
15 use Module::Implementation;
16
17 BEGIN {
18 my $loader = Module::Implementation::build_loader_sub(
19 implementations => [ 'XS', 'PurePerl' ],
20 symbols => [ 'run', 'check' ],
21 );
22
23 $loader->();
24 }
25
26 package Consumer;
27
28 # loads the first viable implementation
29 use Foo::Bar;
30
32 This module abstracts out the process of choosing one of several
33 underlying implementations for a module. This can be used to provide XS
34 and pure Perl implementations of a module, or it could be used to load
35 an implementation for a given OS or any other case of needing to
36 provide multiple implementations.
37
38 This module is only useful when you know all the implementations ahead
39 of time. If you want to load arbitrary implementations then you
40 probably want something like a plugin system, not this module.
41
43 This module provides two subroutines, neither of which are exported.
44
45 Module::Implementation::build_loader_sub(...)
46 This subroutine takes the following arguments.
47
48 · implementations
49
50 This should be an array reference of implementation names. Each
51 name should correspond to a module in the caller's namespace.
52
53 In other words, using the example in the "SYNOPSIS", this module
54 will look for the "Foo::Bar::XS" and "Foo::Bar::PurePerl" modules.
55
56 This argument is required.
57
58 · symbols
59
60 A list of symbols to copy from the implementation package to the
61 calling package.
62
63 These can be prefixed with a variable type: "$", "@", "%", "&", or
64 "*)". If no prefix is given, the symbol is assumed to be a
65 subroutine.
66
67 This argument is optional.
68
69 This subroutine returns the implementation loader as a sub reference.
70
71 It is up to you to call this loader sub in your code.
72
73 I recommend that you do not call this loader in an "import()" sub. If a
74 caller explicitly requests no imports, your "import()" sub will not be
75 run at all, which can cause weird breakage.
76
77 Module::Implementation::implementation_for($package)
78 Given a package name, this subroutine returns the implementation that
79 was loaded for the package. This is not a full package name, just the
80 suffix that identifies the implementation. For the "SYNOPSIS" example,
81 this subroutine would be called as
82 "Module::Implementation::implementation_for('Foo::Bar')", and it would
83 return "XS" or "PurePerl".
84
86 The implementation loader works like this ...
87
88 First, it checks for an %ENV var specifying the implementation to load.
89 The env var is based on the package name which loads the
90 implementations. The "::" package separator is replaced with "_", and
91 made entirely upper-case. Finally, we append "_IMPLEMENTATION" to this
92 name.
93
94 So in our "SYNOPSIS" example, the corresponding %ENV key would be
95 "FOO_BAR_IMPLEMENTATION".
96
97 If this is set, then the loader will only try to load this one
98 implementation.
99
100 If the env var requests an implementation which doesn't match one of
101 the implementations specified when the loader was created, an error is
102 thrown.
103
104 If this one implementation fails to load then loader throws an error.
105 This is useful for testing. You can request a specific implementation
106 in a test file by writing something like this:
107
108 BEGIN { $ENV{FOO_BAR_IMPLEMENTATION} = 'XS' }
109 use Foo::Bar;
110
111 If the environment variable is not set, then the loader simply tries
112 the implementations originally passed to "Module::Implementation". The
113 implementations are tried in the order in which they were originally
114 passed.
115
116 The loader will use the first implementation that loads without an
117 error. It will copy any requested symbols from this implementation.
118
119 If none of the implementations can be loaded, then the loader throws an
120 exception.
121
122 The loader returns the name of the package it loaded.
123
125 Dave Rolsky <autarch@urth.org>
126
128 This software is Copyright (c) 2014 by Dave Rolsky.
129
130 This is free software, licensed under:
131
132 The Artistic License 2.0 (GPL Compatible)
133
134
135
136perl v5.28.1 2014-08-24 Module::Implementation(3)