.\" Automatically generated by Pod::Man 2.25 (Pod::Simple 3.16) .\" .\" 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" '' '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. .ie \nF \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . nr % 0 . rr F .\} .el \{\ . de IX .. .\} .\" .\" 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 3perl" .TH Module::Pluggable 3perl "2010-12-30" "perl v5.14.2" "Perl Programmers Reference Guide" .\" 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 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 \f(CW\*(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 \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 "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 directorys 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. .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 "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 .PP Recently tried fixed to find inner packages and to make it \&'just work' with \s-1PAR\s0 but there are still some issues. .PP However suggestions (and patches) are welcome. .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