Plugin::Simple - Load plugins from files or modules.
use Plugin::Simple; # load a plugin module from a file @plugins = plugins('/path/to/MyModule.pm'); # load all modules under '__PACKAGE__::Plugin' namespace my @plugins = plugins(); # call in scalar context to retrieve the first one # load all plugins under a specific namespace (note the trailing ::) @plugins = plugins('Any::Namespace::'); # load/return only the plugins that can perform specific functions @plugins = plugins(can => ['foo', 'bar']); # foo and bar # instead of importing 'plugins()', change the name: use Plugin::Simple sub_name => 'foo'; @plugins = foo(...); # set a default fallback plugin if searching turns up nothing use Plugin::Simple default => 'My::Module::Plugin::DefaultPlugin' # do something with the plugins for my $plugin (@plugins){ $plugin->plugin_func(@args); } # works in OO modules too simply by using it my @plugins = $self->plugins();
There are many plugin modules available on the CPAN, but I wrote this one just for fun. It's very simple, extremely lightweight, and is extremely minimalistic in what it does.
It searches for modules in installed packages or non-installed files, and loads them (without string eval). You can optionally have us return only the plugins that can() perform a specific task.
eval
can()
By default, we force plugins() into your namespace. To change this name:
plugins()
use Plugin::Simple sub_name => 'other_name';
If searching fails, you can ensure a default known plugin gets loaded:
use Plugin::Simple default => 'My::Plugin';
To use both options, simply separate them with a comma.
None. We simply install a plugins() function within the namespace of the package that used us.
use
This example simply uses a single plugin module with a plugin_function() function. In the script, we load this file, and check to ensure the plugin does in fact have that sub available.
plugin_function()
We then call the plugins in a loop (even though in this case there's only one), and send in an argument for the plugin to do work on.
use warnings; use strict; use lib '.'; use Plugin::Simple; my @plugins = plugins( 'examples/TestPlugin.pm', can => ['plugin_function'] ); my $plugin_arg = 'Hello!'; for my $plugin (@plugins){ $plugin->plugin_function($plugin_arg); }
package TestPlugin; sub plugin_function { shift; # throw away class/obj my ($str) = @_; print "in " . __PACKAGE__ . ", arg is: $str\n"; } 1;
in TestPlugin, arg is: Hello!
Steve Bertrand, <steveb at cpan.org>
<steveb at cpan.org>
https://github.com/stevieb9/p5-plugin-simple/issues
There are far too many plugin import modules on the CPAN to mention here.
Copyright 2016,2017,2018 Steve Bertrand.
This program is free software; you can redistribute it and/or modify it under the terms of either: the GNU General Public License as published by the Free Software Foundation; or the Artistic License.
See http://dev.perl.org/licenses/ for more information.
To install Plugin::Simple, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Plugin::Simple
CPAN shell
perl -MCPAN -e shell install Plugin::Simple
For more information on module installation, please visit the detailed CPAN module installation guide.