.\" Automatically generated by Pod::Man 2.28 (Pod::Simple 3.28) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" Set up some character translations and predefined strings. \*(-- will .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left .\" double quote, and \*(R" will give a right double quote. \*(C+ will .\" give a nicer C++. Capital omega is used to do unbreakable dashes and .\" therefore won't be available. \*(C` and \*(C' expand to `' in nroff, .\" nothing in troff, for use with C<>. .tr \(*W- .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p' .ie n \{\ . ds -- \(*W- . ds PI pi . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch . ds L" "" . ds R" "" . ds C` "" . ds C' "" 'br\} .el\{\ . ds -- \|\(em\| . ds PI \(*p . ds L" `` . ds R" '' . ds C` . ds C' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is turned on, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .\" .\" Avoid warning from groff about undefined register 'F'. .de IX .. .nr rF 0 .if \n(.g .if rF .nr rF 1 .if (\n(rF:(\n(.g==0)) \{ . if \nF \{ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{ . nr % 0 . nr F 2 . \} . \} .\} .rr rF .\" .\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2). .\" Fear. Run. Save yourself. No user-serviceable parts. . \" fudge factors for nroff and troff .if n \{\ . ds #H 0 . ds #V .8m . ds #F .3m . ds #[ \f1 . ds #] \fP .\} .if t \{\ . ds #H ((1u-(\\\\n(.fu%2u))*.13m) . ds #V .6m . ds #F 0 . ds #[ \& . ds #] \& .\} . \" simple accents for nroff and troff .if n \{\ . ds ' \& . ds ` \& . ds ^ \& . ds , \& . ds ~ ~ . ds / .\} .if t \{\ . ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u" . ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u' . ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u' . ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u' . ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u' . ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u' .\} . \" troff and (daisy-wheel) nroff accents .ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V' .ds 8 \h'\*(#H'\(*b\h'-\*(#H' .ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#] .ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H' .ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u' .ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#] .ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#] .ds ae a\h'-(\w'a'u*4/10)'e .ds Ae A\h'-(\w'A'u*4/10)'E . \" corrections for vroff .if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u' .if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u' . \" for low resolution devices (crt and lpr) .if \n(.H>23 .if \n(.V>19 \ \{\ . ds : e . ds 8 ss . ds o a . ds d- d\h'-1'\(ga . ds D- D\h'-1'\(hy . ds th \o'bp' . ds Th \o'LP' . ds ae ae . ds Ae AE .\} .rm #[ #] #H #V #F C .\" ======================================================================== .\" .IX Title "Module::Pluggable 3pm" .TH Module::Pluggable 3pm "2015-08-04" "perl v5.20.2" "User Contributed Perl Documentation" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH "NAME" Module::Pluggable \- automatically give your module the ability to have plugins .SH "SYNOPSIS" .IX Header "SYNOPSIS" Simple use Module::Pluggable \- .PP .Vb 2 \& package MyClass; \& use Module::Pluggable; .Ve .PP and then later ... .PP .Vb 4 \& use MyClass; \& my $mc = MyClass\->new(); \& # returns the names of all plugins installed under MyClass::Plugin::* \& my @plugins = $mc\->plugins(); .Ve .SH "EXAMPLE" .IX Header "EXAMPLE" Why 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 ... .PP .Vb 1 \& 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; \& } .Ve .PP \&.. and all the plugins will get a chance in turn to look at it. .PP 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 \f(CW\*(C`examine\*(C'\fR method returns \f(CW1\fR if it has saved the email somewhere. You might also want to be paranoid and check to see if the plugin has an \f(CW\*(C`examine\*(C'\fR method. .PP .Vb 4 \& foreach my $plugin ($self\->plugins) { \& next unless $plugin\->can(\*(Aqexamine\*(Aq); \& last if $plugin\->examine($email); \& } .Ve .PP And so on. The sky's the limit. .SH "DESCRIPTION" .IX Header "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. .PP 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. .PP Optionally it instantiates those classes for you. .SH "ADVANCED USAGE" .IX Header "ADVANCED USAGE" Alternatively, if you don't want to use 'plugins' as the method ... .PP .Vb 2 \& package MyClass; \& use Module::Pluggable sub_name => \*(Aqfoo\*(Aq; .Ve .PP and then later ... .PP .Vb 1 \& my @plugins = $mc\->foo(); .Ve .PP Or if you want to look in another namespace .PP .Vb 2 \& package MyClass; \& use Module::Pluggable search_path => [\*(AqAcme::MyClass::Plugin\*(Aq, \*(AqMyClass::Extend\*(Aq]; .Ve .PP or directory .PP .Vb 1 \& use Module::Pluggable search_dirs => [\*(Aqmylibs/Foo\*(Aq]; .Ve .PP Or if you want to instantiate each plugin rather than just return the name .PP .Vb 2 \& package MyClass; \& use Module::Pluggable instantiate => \*(Aqnew\*(Aq; .Ve .PP and then .PP .Vb 3 \& # whatever is passed to \*(Aqplugins\*(Aq will be passed \& # to \*(Aqnew\*(Aq for each plugin \& my @plugins = $mc\->plugins(@options); .Ve .PP alternatively you can just require the module without instantiating it .PP .Vb 2 \& package MyClass; \& use Module::Pluggable require => 1; .Ve .PP since requiring automatically searches inner packages, which may not be desirable, you can turn this off .PP .Vb 2 \& package MyClass; \& use Module::Pluggable require => 1, inner => 0; .Ve .PP You can limit the plugins loaded using the except option, either as a string, array ref or regex .PP .Vb 2 \& package MyClass; \& use Module::Pluggable except => \*(AqMyClass::Plugin::Foo\*(Aq; .Ve .PP or .PP .Vb 2 \& package MyClass; \& use Module::Pluggable except => [\*(AqMyClass::Plugin::Foo\*(Aq, \*(AqMyClass::Plugin::Bar\*(Aq]; .Ve .PP or .PP .Vb 2 \& package MyClass; \& use Module::Pluggable except => qr/^MyClass::Plugin::(Foo|Bar)$/; .Ve .PP and similarly for only which will only load plugins which match. .PP Remember you can use the module more than once .PP .Vb 3 \& package MyClass; \& use Module::Pluggable search_path => \*(AqMyClass::Filters\*(Aq sub_name => \*(Aqfilters\*(Aq; \& use Module::Pluggable search_path => \*(AqMyClass::Plugins\*(Aq sub_name => \*(Aqplugins\*(Aq; .Ve .PP and then later ... .PP .Vb 2 \& my @filters = $self\->filters; \& my @plugins = $self\->plugins; .Ve .SH "PLUGIN SEARCHING" .IX Header "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 .PP .Vb 3 \& package Foo; \& use strict; \& use Module::Pluggable sub_name => \*(Aq_plugins\*(Aq; \& \& our @PLUGINS; \& sub plugins { @PLUGINS ||= shift\->_plugins } \& 1; .Ve .SH "INNER PACKAGES" .IX Header "INNER PACKAGES" If you have, for example, a file \fBlib/Something/Plugin/Foo.pm\fR that contains package definitions for both \f(CW\*(C`Something::Plugin::Foo\*(C'\fR and \&\f(CW\*(C`Something::Plugin::Bar\*(C'\fR then as long as you either have either the \fBrequire\fR or \fBinstantiate\fR option set then we'll also find \&\f(CW\*(C`Something::Plugin::Bar\*(C'\fR. Nifty! .SH "OPTIONS" .IX Header "OPTIONS" You can pass a hash of options when importing this module. .PP The options can be ... .SS "sub_name" .IX Subsection "sub_name" The name of the subroutine to create in your namespace. .PP By default this is 'plugins' .SS "search_path" .IX Subsection "search_path" An array ref of namespaces to look in. .SS "search_dirs" .IX Subsection "search_dirs" An array ref of directories to look in before \f(CW@INC\fR. .SS "instantiate" .IX Subsection "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. .PP The default is 'undef' i.e just return the class name. .SS "require" .IX Subsection "require" Just require the class, don't instantiate (overrides 'instantiate'); .SS "inner" .IX Subsection "inner" If set to 0 will \fBnot\fR search inner packages. If set to 1 will override \f(CW\*(C`require\*(C'\fR. .SS "only" .IX Subsection "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. .SS "except" .IX Subsection "except" Similar to \f(CW\*(C`only\*(C'\fR it takes a description of plugins to exclude from returning. This is slightly less perverse. .SS "package" .IX Subsection "package" This is for use by extension modules which build on \f(CW\*(C`Module::Pluggable\*(C'\fR: passing a \f(CW\*(C`package\*(C'\fR option allows you to place the plugin method in a different package other than your own. .SS "file_regex" .IX Subsection "file_regex" By default \f(CW\*(C`Module::Pluggable\*(C'\fR only looks for \fI.pm\fR files. .PP By supplying a new \f(CW\*(C`file_regex\*(C'\fR then you can change this behaviour e.g .PP .Vb 1 \& file_regex => qr/\e.plugin$/ .Ve .SS "include_editor_junk" .IX Subsection "include_editor_junk" By default \f(CW\*(C`Module::Pluggable\*(C'\fR ignores files that look like they were left behind by editors. Currently this means files ending in \fI~\fR (~), the extensions \fI.swp\fR or \fI.swo\fR, or files beginning with \fI.#\fR. .PP Setting \f(CW\*(C`include_editor_junk\*(C'\fR changes \f(CW\*(C`Module::Pluggable\*(C'\fR so it does not ignore any files it finds. .SS "follow_symlinks" .IX Subsection "follow_symlinks" Whether, when searching directories, to follow symlinks. .PP Defaults to 1 i.e do follow symlinks. .SS "min_depth, max_depth" .IX Subsection "min_depth, max_depth" This will allow you to set what 'depth' of plugin will be allowed. .PP So, for example, \f(CW\*(C`MyClass::Plugin::Foo\*(C'\fR will have a depth of 3 and \&\f(CW\*(C`MyClass::Plugin::Foo::Bar\*(C'\fR will have a depth of 4 so to only get the former (i.e \f(CW\*(C`MyClass::Plugin::Foo\*(C'\fR) do .PP .Vb 2 \& package MyClass; \& use Module::Pluggable max_depth => 3; .Ve .PP and to only get the latter (i.e \f(CW\*(C`MyClass::Plugin::Foo::Bar\*(C'\fR) .PP .Vb 2 \& package MyClass; \& use Module::Pluggable min_depth => 4; .Ve .SH "TRIGGERS" .IX Header "TRIGGERS" Various triggers can also be passed in to the options. .PP If any of these triggers return 0 then the plugin will not be returned. .SS "before_require " .IX Subsection "before_require " Gets passed the plugin name. .PP If 0 is returned then this plugin will not be required either. .SS "on_require_error " .IX Subsection "on_require_error " Gets called when there's an error on requiring the plugin. .PP Gets passed the plugin name and the error. .PP The default on_require_error handler is to \f(CW\*(C`carp\*(C'\fR the error and return 0. .SS "on_instantiate_error " .IX Subsection "on_instantiate_error " Gets called when there's an error on instantiating the plugin. .PP Gets passed the plugin name and the error. .PP The default on_instantiate_error handler is to \f(CW\*(C`carp\*(C'\fR the error and return 0. .SS "after_require " .IX Subsection "after_require " Gets passed the plugin name. .PP If 0 is returned then this plugin will be required but not returned as a plugin. .SH "METHODs" .IX Header "METHODs" .SS "search_path" .IX Subsection "search_path" The method \f(CW\*(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. .PP .Vb 2 \& $self\->search_path( add => "New::Path" ); # add \& $self\->search_path( new => "New::Path" ); # replace .Ve .SH "BEHAVIOUR UNDER TEST ENVIRONMENT" .IX Header "BEHAVIOUR UNDER TEST ENVIRONMENT" In order to make testing reliable we exclude anything not from blib if blib.pm is in \f(CW%INC\fR. .PP However if the module being tested used another module that itself used \f(CW\*(C`Module::Pluggable\*(C'\fR then the second module would fail. This was fixed by checking to see if the caller had (^|/)blib/ in their filename. .PP 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. .PP You can now (as of version 4.1) force Module::Pluggable to look outside blib in a test environment by doing either .PP .Vb 3 \& require Module::Pluggable; \& $Module::Pluggable::FORCE_SEARCH_ALL_PATHS = 1; \& import Module::Pluggable; .Ve .PP or .PP .Vb 1 \& use Module::Pluggable force_search_all_paths => 1; .Ve .ie n .SH "@INC hooks and App::FatPacker" .el .SH "\f(CW@INC\fP hooks and App::FatPacker" .IX Header "@INC hooks and App::FatPacker" If a module's \f(CW@INC\fR has a hook and that hook is an object which has a \f(CW\*(C`files()\*(C'\fR method then we will try and require those files too. See \f(CW\*(C`t/26inc_hook.t\*(C'\fR for an example. .PP This has allowed App::FatPacker (as of version 0.10.0) to provide support for Module::Pluggable. .PP This should also, theoretically, allow someone to modify \s-1PAR\s0 to do the same thing. .SH "Module::Require recommended" .IX Header "Module::Require recommended" Up until version 5.2 Module::Pluggable used a string \f(CW\*(C`eval\*(C'\fR to require plugins. .PP This has now been changed to optionally use Module::Runtime and it's \f(CW\*(C`require_module\*(C'\fR method when available and fall back to using a path based \f(CW\*(C`require\*(C'\fR when not. .PP It's recommended, but not required, that you install Module::Runtime. .SH "FUTURE PLANS" .IX Header "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). .PP However suggestions (and patches) are always welcome. .SH "DEVELOPMENT" .IX Header "DEVELOPMENT" The master repo for this module is at .PP https://github.com/simonwistow/Module\-Pluggable .SH "AUTHOR" .IX Header "AUTHOR" Simon Wistow .SH "COPYING" .IX Header "COPYING" Copyright, 2006 Simon Wistow .PP Distributed under the same terms as Perl itself. .SH "BUGS" .IX Header "BUGS" None known. .SH "SEE ALSO" .IX Header "SEE ALSO" File::Spec, File::Find, File::Basename, Class::Factory::Util, Module::Pluggable::Ordered