™.. Module::Pluggable - Online Linux Manual PageSection : 3pm
Updated : 2009-07-03
Source : perl v5.10.1
Note : Perl Programmers Reference Guide
NAMEModule::Pluggable − automatically give your module the ability to have plugins
SYNOPSISSimple 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();
EXAMPLEWhy would you want to do this? Say you have something that wants to pass an object to a number of different plugins in turn. For example you may 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 trivally 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 \*(C`examine\*(C'\fR method returns \f(CW1\fR if it has saved the email somewhere. You might also wnat to be paranoid and check to see if the plugin has an \*(C`examine\*(C'\fR method. foreach my $plugin ($self−>plugins) {
next unless $plugin−>can('examine');
last if $plugin−>examine($email);
}
And so on. The sky's the limit.
DESCRIPTIONProvides 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 turn those into class names. Optionally it instantiates those classes for you.
ADVANCED USAGEAlternatively, 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;
INNER PACKAGESIf you have, for example, a file lib/Something/Plugin/Foo.pm that contains package definitions for both \*(C`Something::Plugin::Foo\*(C'\fR and \*(C`Something::Plugin::Bar\*(C'\fR then as long as you either have either the require or instantiate option set then we'll also find \*(C`Something::Plugin::Bar\*(C'\fR. Nifty!
OPTIONSYou can pass a hash of options when importing this module. The options can be ...
sub_nameThe name of the subroutine to create in your namespace. By default this is 'plugins'
search_pathAn array ref of namespaces to look in.
search_dirsAn array ref of directorys to look in before @INC.
instantiateCall 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.
requireJust require the class, don't instantiate (overrides 'instantiate');
innerIf set to 0 will not search inner packages. If set to 1 will override \*(C`require\*(C'\fR.
onlyTakes 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.
exceptSimilar to \*(C`only\*(C'\fR it takes a description of plugins to exclude from returning. This is slightly less perverse.
packageThis is for use by extension modules which build on \*(C`Module::Pluggable\*(C'\fR: passing a \*(C`package\*(C'\fR option allows you to place the plugin method in a different package other than your own.
file_regexBy default \*(C`Module::Pluggable\*(C'\fR only looks for \fI.pm\fR files. By supplying a new \*(C`file_regex\*(C'\fR then you can change this behaviour e.g file_regex => qr/\.plugin$/
include_editor_junkBy default \*(C`Module::Pluggable\*(C'\fR 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 \*(C`include_editor_junk\*(C'\fR changes \f(CW\*(C`Module::Pluggable\*(C'\fR so it does not ignore any files it finds.
METHODs
search_pathThe method \*(C`search_path\*(C'\fR is exported into you 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
FUTURE PLANSThis does everything I need and I can't really think of any other features I want to add. Famous last words of course Recently tried fixed to find inner packages and to make it 'just work' with PAR but there are still some issues. However suggestions (and patches) are welcome.
AUTHORSimon Wistow <simon@thegestalt.org>
COPYINGCopyright, 2006 Simon Wistow Distributed under the same terms as Perl itself.
BUGSNone known.
SEE ALSOFile::Spec, File::Find, File::Basename, Class::Factory::Util, Module::Pluggable::Ordered 0
Johanes Gumabo
Data Size : 26,951 byte
man-Module::Pluggable.3pmBuild : 2024-12-05, 20:55 :
Visitor Screen : x
Visitor Counter ( page / site ) : 4 / 175,202
Visitor ID : :
Visitor IP : 13.59.91.59 :
Visitor Provider : AMAZON-02 :
Provider Position ( lat x lon ) : 39.962500 x -83.006100 : x
Provider Accuracy Radius ( km ) : 1000 :
Provider City : Columbus :
Provider Province : Ohio , : ,
Provider Country : United States :
Provider Continent : North America :
Visitor Recorder : Version :
Visitor Recorder : Library :
Online Linux Manual Page : Version : Online Linux Manual Page - Fedora.40 - march=x86-64 - mtune=generic - 24.12.05
Online Linux Manual Page : Library : lib_c - 24.10.03 - march=x86-64 - mtune=generic - Fedora.40
Online Linux Manual Page : Library : lib_m - 24.10.03 - march=x86-64 - mtune=generic - Fedora.40
Data Base : Version : Online Linux Manual Page Database - 24.04.13 - march=x86-64 - mtune=generic - fedora-38
Data Base : Library : lib_c - 23.02.07 - march=x86-64 - mtune=generic - fedora.36
Very long time ago, I have the best tutor, Wenzel Svojanovsky . If someone knows the email address of Wenzel Svojanovsky , please send an email to johanes_gumabo@yahoo.co.id .
If error, please print screen and send to johanes_gumabo@yahoo.co.id
Under development. Support me via PayPal.
ERROR : Need New Coding : (parse_manual_page_|249|Module::Pluggable.3pm|36/37|el══─{─══.|.el══─{─══. ds -- \|\(em\|
) (htmlprn|149|Module::Pluggable.3pm|36/37|.el══─{─══. ds -- — |.el══─{─══. ds -- \|\(em\|
) (parse_manual_page_|249|Module::Pluggable.3pm|41|br══─}─══|'br══─}─══
) (htmlprn|149|Module::Pluggable.3pm|41|'br══─}─══ |'br══─}─══
) (rof_nr_x|149|Module::Pluggable.3pm|51/52|\nF|.ie \nF ══─{─══. de IX
) (rof_unit_scale_px|41|Module::Pluggable.3pm|51/52|F|.ie \nF ══─{─══. de IX
) (rof_if|19|Module::Pluggable.3pm|51/52|\nF|.ie \nF ══─{─══. de IX
) (htmlprn|149|Module::Pluggable.3pm|51/52|.ie \nF ══─{─══. de IX|.ie \nF ══─{─══. de IX
) (rof_escape_sequence|91|Module::Pluggable.3pm|53|\$1\t\\n%\t"\\$2" |. tm Index:\\$1\t\\n%\t"\\$2"
) (parse_manual_page_|249|Module::Pluggable.3pm|57|══─}─══|.══─}─══
) (htmlprn|149|Module::Pluggable.3pm|57|.══─}─══ |.══─}─══
) (rof_escape_sequence|91|Module::Pluggable.3pm|184|\*(C`examine\*(C'\fR method returns \f(CW1\fR if |Simply have it so that the \f(CW\*(C`examine\*(C'\fR method returns \f(CW1\fR if
) (rof_escape_sequence|91|Module::Pluggable.3pm|186|\*(C`examine\*(C'\fR method. |and check to see if the plugin has an \f(CW\*(C`examine\*(C'\fR method.
) (rof_escape_sequence|91|Module::Pluggable.3pm|304|\*(C`Something::Plugin::Foo\*(C'\fR and |contains package definitions for both \f(CW\*(C`Something::Plugin::Foo\*(C'\fR and
) (rof_escape_sequence|91|Module::Pluggable.3pm|305|\*(C`Something::Plugin::Bar\*(C'\fR then as long as you either have either |\&\f(CW\*(C`Something::Plugin::Bar\*(C'\fR then as long as you either have either
) (rof_escape_sequence|91|Module::Pluggable.3pm|307|\*(C`Something::Plugin::Bar\*(C'\fR. Nifty! |\&\f(CW\*(C`Something::Plugin::Bar\*(C'\fR. Nifty!
) (rof_escape_sequence|91|Module::Pluggable.3pm|337|\*(C`require\*(C'\fR. |If set to 1 will override \f(CW\*(C`require\*(C'\fR.
) (rof_escape_sequence|91|Module::Pluggable.3pm|345|\*(C`only\*(C'\fR it takes a description of plugins to exclude |Similar to \f(CW\*(C`only\*(C'\fR it takes a description of plugins to exclude
) (rof_escape_sequence|91|Module::Pluggable.3pm|349|\*(C`Module::Pluggable\*(C'\fR: |This is for use by extension modules which build on \f(CW\*(C`Module::Pluggable\*(C'\fR:
) (rof_escape_sequence|91|Module::Pluggable.3pm|350|\*(C`package\*(C'\fR option allows you to place the plugin method in a |passing a \f(CW\*(C`package\*(C'\fR option allows you to place the plugin method in a
) (rof_escape_sequence|91|Module::Pluggable.3pm|354|\*(C`Module::Pluggable\*(C'\fR only looks for \fI.pm\fR files. |By default \f(CW\*(C`Module::Pluggable\*(C'\fR only looks for \fI.pm\fR files.
) (rof_escape_sequence|91|Module::Pluggable.3pm|356|\*(C`file_regex\*(C'\fR then you can change this behaviour e.g |By supplying a new \f(CW\*(C`file_regex\*(C'\fR then you can change this behaviour e.g
) (rof_escape_sequence|91|Module::Pluggable.3pm|363|\*(C`Module::Pluggable\*(C'\fR ignores files that look like they were |By default \f(CW\*(C`Module::Pluggable\*(C'\fR ignores files that look like they were
) (rof_escape_sequence|91|Module::Pluggable.3pm|367|\*(C`include_editor_junk\*(C'\fR changes \f(CW\*(C`Module::Pluggable\*(C'\fR so it does |Setting \f(CW\*(C`include_editor_junk\*(C'\fR changes \f(CW\*(C`Module::Pluggable\*(C'\fR so it does
) (rof_escape_sequence|91|Module::Pluggable.3pm|373|\*(C`search_path\*(C'\fR is exported into you namespace as well. |The method \f(CW\*(C`search_path\*(C'\fR is exported into you namespace as well.
)