Pluggable - dynamically find modules or classes under a given namespace
This is a modified version orginally based on https://github.com/tony-o/perl6-pluggable.
Given a set of plugins in your library search path:
a::Plugins::Plugin1
a::Plugins::Plugin2
a::Plugins::PluginClass1::PluginClass2::Plugin3
And an invocation of Pluggable like this:
use Pluggable;
class a does Pluggable {
method listplugins () {
@($.plugins).map({.perl}).join("\n").say;
}
}
a.new.listplugins;
The following output would be produced:
a::Plugins::Plugin1
a::Plugins::Plugin2
a::Plugins::PluginClass1::PluginClass2::Plugin3
-
Role as well as procedural interface
-
Custom module name matching
-
Finding plugins outside of the current modules namespace
When "doing" the Pluggable role, a class can use the "plugins" method:
$.plugins(:$base = Nil, :$plugins-namespace = 'Plugins', :$name-matcher = Nil)
The base namespace to look for plugins under, if not provided then the namespace from which pluggable is invoked is used.
The name of the namespace within $base that contains plugins.
If present, the name of any module found will be compared with this and only returned if they match.
In a similar fashion, the module can be used in a non-OO environment, it exports a single sub:
plugins($base, :$plugins-namespace = 'Plugins', :$name-matcher = Nil)
The base namespace to look for plugins under. Unlike in the OO case, this is required in the procedural interface.
The name of the namespace within $base that contains plugins.
If present, the name of any module found will be compared with this and only returned if they match.
If you're having trouble with an object you think should be loading but would like to find out what is bonking in require
, set $*DEBUG-PLUGINS
to a truthy value.
Released under the Artistic License 2.0 http://www.perlfoundation.org/artistic_license_2_0
-
Robert Lemmen robertle@semistable.com