Module::Pluggable(3)
π Module::Pluggable(3) π
okay so this is the User Contributed Perl Documentation page for Module::Pluggable β and bestie, it is literally the module that automatically gives your module the ability to have plugins. π
β¨ NAME β¨
Module::Pluggable β automatically give your module the ability to have plugins. no cap. π
β¨ SYNOPSIS β¨
Simple use Module::Pluggable β
package MyClass;
use Module::Pluggable;
and then later ...
use MyClass;
my $mc = MyClass->new();
# returns the names of all plugins installed under MyClass::Plugin::*
my @plugins = $mc->plugins();
πΈ EXAMPLE πΈ
Why would you even want to do this? okay so imagine you have something that wants to pass an object to a bunch of different plugins in turn. For example, maybe you want to extract meta-data from every email you get sent and do something with it. Plugins make sense here because then you can keep adding new meta-data parsers, and all the logic and docs for each one will be self-contained, and new handlers are easy to add without changing the core code. For that, you might do something like ...
package Email::Examiner;
use strict;
use Email::Simple;
use Module::Pluggable require => 1;
sub handle_email {
my $self = shift;
my $email = shift;
foreach my $plugin ($self->plugins) {
$plugin->examine($email);
}
return 1;
}
.. and all the plugins will get a chance in turn to look at it.
This can be trivially extended so that plugins could save the email somewhere and then no other plugin should try and do that. Simply have it so that the examine method returns 1 if it has saved the email somewhere. You might also want to be paranoid and check to see if the plugin has an examine method.
foreach my $plugin ($self->plugins) {
next unless $plugin->can('examine');
last if $plugin->examine($email);
}
And so on. The sky's the limit. π¦
π DESCRIPTION π
Provides a simple but, hopefully, extensible way of having 'plugins' for your module. Obviously this isn't going to be the be-all and end-all of solutions, but it works for me.
Essentially all it does is export a method into your namespace that looks through a search path for .pm files and turns those into class names.
Optionally it instantiates those classes for you.
π ADVANCED USAGE π
Alternatively, if you don't want to use 'plugins' as the method ...
package MyClass;
use Module::Pluggable sub_name => 'foo';
and then later ...
my @plugins = $mc->foo();
Or if you want to look in another namespace:
package MyClass;
use Module::Pluggable search_path => ['Acme::MyClass::Plugin', 'MyClass::Extend'];
or directory:
use Module::Pluggable search_dirs => ['mylibs/Foo'];
Or if you want to instantiate each plugin rather than just return the name:
package MyClass;
use Module::Pluggable instantiate => 'new';
and then
# whatever is passed to 'plugins' will be passed
# to 'new' for each plugin
my @plugins = $mc->plugins(@options);
alternatively you can just require the module without instantiating it:
package MyClass;
use Module::Pluggable require => 1;
since requiring automatically searches inner packages, which may not be desirable, you can turn this off:
package MyClass;
use Module::Pluggable require => 1, inner => 0;
You can limit the plugins loaded using the except option, either as a string, array ref or regex:
package MyClass;
use Module::Pluggable except => 'MyClass::Plugin::Foo';
or
package MyClass;
use Module::Pluggable except => ['MyClass::Plugin::Foo', 'MyClass::Plugin::Bar'];
or
package MyClass;
use Module::Pluggable except => qr/^MyClass::Plugin::(Foo|Bar)$/;
and similarly for only, which will only load plugins which match.
Remember you can use the module more than once:
package MyClass;
use Module::Pluggable search_path => 'MyClass::Filters', sub_name => 'filters';
use Module::Pluggable search_path => 'MyClass::Plugins', sub_name => 'plugins';
and then later ...
my @filters = $self->filters;
my @plugins = $self->plugins;
π PLUGIN SEARCHING π
Every time you call 'plugins', the whole search path is walked again. This allows for dynamically loading plugins even at run time. However, this can get expensive, and so if you don't expect to want to add new plugins at run time you could do:
package Foo;
use strict;
use Module::Pluggable sub_name => '_plugins';
our @PLUGINS;
sub plugins { @PLUGINS ||= shift->_plugins }
1;
π§ INNER PACKAGES π§
If you have, for example, a file lib/Something/Plugin/Foo.pm that contains package definitions for both Something::Plugin::Foo and Something::Plugin::Bar, then as long as you either have either the require or instantiate option set, then we'll also find Something::Plugin::Bar. Nifty! β¨
π OPTIONS π
You can pass a hash of options when importing this module.
The options can be ...
πΈ sub_name πΈ
The name of the subroutine to create in your namespace.
By default this is 'plugins'.
πΈ search_path πΈ
An array ref of namespaces to look in.
πΈ search_dirs πΈ
An array ref of directories to look in before @INC.
πΈ instantiate πΈ
Call this method on the class. In general this will probably be 'new', but it can be whatever you want. Whatever arguments are passed to 'plugins' will be passed to the method.
The default is 'undef', i.e. just return the class name.
πΈ require πΈ
Just require the class, don't instantiate (overrides 'instantiate').
πΈ inner πΈ
If set to 0, will not search inner packages. If set to 1, will override require.
πΈ only πΈ
Takes a string, array ref or regex describing the names of the only plugins to return. Whilst this may seem perverse ... well, it is. But it also makes sense. Trust me.
πΈ except πΈ
Similar to only, it takes a description of plugins to exclude from returning. This is slightly less perverse.
πΈ package πΈ
This is for use by extension modules which build on Module::Pluggable: passing a package option allows you to place the plugin method in a different package other than your own.
πΈ file_regex πΈ
By default Module::Pluggable only looks for .pm files.
By supplying a new file_regex, you can change this behaviour, e.g.
file_regex => qr/\.plugin$/
πΈ include_editor_junk πΈ
By default Module::Pluggable ignores files that look like they were left behind by editors. Currently this means files ending in ~ (~), the extensions .swp or .swo, or files beginning with .#.
Setting include_editor_junk changes Module::Pluggable so it does not ignore any files it finds.
πΈ follow_symlinks πΈ
Whether, when searching directories, to follow symlinks.
Defaults to 1, i.e. do follow symlinks.
πΈ min_depth, max_depth πΈ
This will allow you to set what 'depth' of plugin will be allowed.
So, for example, MyClass::Plugin::Foo will have a depth of 3, and MyClass::Plugin::Foo::Bar will have a depth of 4. So to only get the former (i.e. MyClass::Plugin::Foo), do:
package MyClass;
use Module::Pluggable max_depth => 3;
and to only get the latter (i.e. MyClass::Plugin::Foo::Bar):
package MyClass;
use Module::Pluggable min_depth => 4;
β‘ TRIGGERS β‘
Various triggers can also be passed in to the options.
If any of these triggers return 0, then the plugin will not be returned.
β‘ before_require <plugin> β‘
Gets passed the plugin name.
If 0 is returned, then this plugin will not be required either.
β‘ on_require_error <plugin> <err> β‘
Gets called when there's an error on requiring the plugin.
Gets passed the plugin name and the error.
The default on_require_error handler is to carp the error and return 0.
β‘ after_require <plugin> β‘
Gets passed the plugin name.
If 0 is returned, then this plugin will be required but not instantiated or returned as a plugin.
β‘ before_instantiate <plugin> β‘
Gets passed the plugin name.
If 0 is returned, then instantiation will not occur.
β‘ on_instantiate_error <plugin> <err> β‘
Gets called when there's an error on instantiating the plugin.
Gets passed the plugin name and the error.
The default on_instantiate_error handler is to carp the error and return 0.
β‘ after_instantiate <plugin> <obj> β‘
Gets passed the plugin name and the object that was instentiated. i.e. the return value of "Plugin-"new()>.
The return value must be <obj> and will be returned AS the plugin. If the return value is not true, then nothing will be returned as a plugin.
π οΈ METHODs π οΈ
π search_path π
The method search_path is exported into your namespace as well. You can call that at any time to change or replace the search_path.
$self->search_path( add => "New::Path" ); # add
$self->search_path( new => "New::Path" ); # replace
π§ͺ BEHAVIOUR UNDER TEST ENVIRONMENT π§ͺ
In order to make testing reliable, we exclude anything not from blib if blib.pm is in %INC.
However, if the module being tested used another module that itself used Module::Pluggable, then the second module would fail. This was fixed by checking to see if the caller had (^|/)blib/ in their filename.
There's an argument that this is the wrong behaviour and that modules should explicitly trigger this behaviour, but that particular code has been around for 7 years now and I'm reluctant to change the default behaviour.
You can now (as of version 4.1) force Module::Pluggable to look outside blib in a test environment by doing either:
require Module::Pluggable;
$Module::Pluggable::FORCE_SEARCH_ALL_PATHS = 1;
import Module::Pluggable;
or
use Module::Pluggable force_search_all_paths => 1;
π @INC hooks and App::FatPacker π
If a module's @INC has a hook and that hook is an object which has a files() method, then we will try and require those files too. See t/26inc_hook.t for an example.
This has allowed App::FatPacker (as of version 0.10.0) to provide support for Module::Pluggable.
This should also, theoretically, allow someone to modify PAR to do the same thing.
π§ Module::Require recommended π§
Up until version 5.2, Module::Pluggable used a string eval to require plugins.
This has now been changed to optionally use Module::Runtime and its require_module method when available, and fall back to using a path-based require when not.
It's recommended, but not required, that you install Module::Runtime.
π FUTURE PLANS π
This does everything I need, and I can't really think of any other features I want to add. Famous last words, of course (not least because we're up to version 5.0 at the time of writing).
However, suggestions (and patches) are always welcome.
π DEVELOPMENT π
The master repo for this module is at
https://github.com/simonwistow/Module-Pluggable
π AUTHOR π
Simon Wistow <simon@thegestalt.org>
π COPYING π
okay so legal stuff, but make it cute π β this whole thing is Copyright, 2006 Simon Wistow. Shoutout to Simon, icon behaviour fr fr π.
And it's Distributed under the same terms as Perl itself β so like, same vibe as Perl, same rules as Perl, iykyk. Go read Perl's terms if you need the deets, bestie, they're literally the same ones π«Ά.
π BUGS π
None known.
That's it. That's the whole bugs section. Not me being like "wow, flawless" but also likeβ¦ yeah, that's genuinely what it says πβ¨. No known bugs, no cap. (If you find one, that's a you discovering history moment, bestie π¦.)
π SEE ALSO π
Like, go read these too, they're all part of the ecosystem and they're all giving main character energy in their own way π :
File::Specβ for messing with file paths in a portable way, lowkey essential πFile::Findβ walking directory trees, classic, does exactly what it says πΈFile::Basenameβ pulling the filename bits out of a path, tiny but mighty πClass::Factory::Utilβ the class factory vibes π§Module::Pluggable::Orderedβ the ordered cousin, when you care about the sequence π₯
okay that's genuinely the end of the page, bestie. you made it. go write some plugins π
β¨ and that's a wrap! this lil page is from perl v5.42.0, dated 2025-01-11, and you've been reading Module::Pluggable(3) β slay, periodt, bye πππ¦
π¦ where this page comes from
- alpine 3.23: perl-module-pluggable-doc 6.3-r0 Β· GPL-1.0-or-later OR Artistic-1.0-Perl
- opensuse-leap 16.0: perl-Module-Pluggable 6.300.0-160000.2.2 Β· Artistic-1.0 OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms