1Module::Pluggable(3pm) Perl Programmers Reference Guide Module::Pluggable(3pm)
2
3
4
6 Module::Pluggable - automatically give your module the ability to have
7 plugins
8
10 Simple use Module::Pluggable -
11
12 package MyClass;
13 use Module::Pluggable;
14
15 and then later ...
16
17 use MyClass;
18 my $mc = MyClass->new();
19 # returns the names of all plugins installed under MyClass::Plugin::*
20 my @plugins = $mc->plugins();
21
23 Why would you want to do this? Say you have something that wants to
24 pass an object to a number of different plugins in turn. For example
25 you may want to extract meta-data from every email you get sent and do
26 something with it. Plugins make sense here because then you can keep
27 adding new meta data parsers and all the logic and docs for each one
28 will be self contained and new handlers are easy to add without
29 changing the core code. For that, you might do something like ...
30
31 package Email::Examiner;
32
33 use strict;
34 use Email::Simple;
35 use Module::Pluggable require => 1;
36
37 sub handle_email {
38 my $self = shift;
39 my $email = shift;
40
41 foreach my $plugin ($self->plugins) {
42 $plugin->examine($email);
43 }
44
45 return 1;
46 }
47
48 .. and all the plugins will get a chance in turn to look at it.
49
50 This can be trivally extended so that plugins could save the email
51 somewhere and then no other plugin should try and do that. Simply have
52 it so that the "examine" method returns 1 if it has saved the email
53 somewhere. You might also wnat to be paranoid and check to see if the
54 plugin has an "examine" method.
55
56 foreach my $plugin ($self->plugins) {
57 next unless $plugin->can('examine');
58 last if $plugin->examine($email);
59 }
60
61 And so on. The sky's the limit.
62
64 Provides a simple but, hopefully, extensible way of having 'plugins'
65 for your module. Obviously this isn't going to be the be all and end
66 all of solutions but it works for me.
67
68 Essentially all it does is export a method into your namespace that
69 looks through a search path for .pm files and turn those into class
70 names.
71
72 Optionally it instantiates those classes for you.
73
75 Alternatively, if you don't want to use 'plugins' as the method ...
76
77 package MyClass;
78 use Module::Pluggable sub_name => 'foo';
79
80 and then later ...
81
82 my @plugins = $mc->foo();
83
84 Or if you want to look in another namespace
85
86 package MyClass;
87 use Module::Pluggable search_path => ['Acme::MyClass::Plugin', 'MyClass::Extend'];
88
89 or directory
90
91 use Module::Pluggable search_dirs => ['mylibs/Foo'];
92
93 Or if you want to instantiate each plugin rather than just return the
94 name
95
96 package MyClass;
97 use Module::Pluggable instantiate => 'new';
98
99 and then
100
101 # whatever is passed to 'plugins' will be passed
102 # to 'new' for each plugin
103 my @plugins = $mc->plugins(@options);
104
105 alternatively you can just require the module without instantiating it
106
107 package MyClass;
108 use Module::Pluggable require => 1;
109
110 since requiring automatically searches inner packages, which may not be
111 desirable, you can turn this off
112
113 package MyClass;
114 use Module::Pluggable require => 1, inner => 0;
115
116 You can limit the plugins loaded using the except option, either as a
117 string, array ref or regex
118
119 package MyClass;
120 use Module::Pluggable except => 'MyClass::Plugin::Foo';
121
122 or
123
124 package MyClass;
125 use Module::Pluggable except => ['MyClass::Plugin::Foo', 'MyClass::Plugin::Bar'];
126
127 or
128
129 package MyClass;
130 use Module::Pluggable except => qr/^MyClass::Plugin::(Foo|Bar)$/;
131
132 and similarly for only which will only load plugins which match.
133
134 Remember you can use the module more than once
135
136 package MyClass;
137 use Module::Pluggable search_path => 'MyClass::Filters' sub_name => 'filters';
138 use Module::Pluggable search_path => 'MyClass::Plugins' sub_name => 'plugins';
139
140 and then later ...
141
142 my @filters = $self->filters;
143 my @plugins = $self->plugins;
144
146 If you have, for example, a file lib/Something/Plugin/Foo.pm that
147 contains package definitions for both "Something::Plugin::Foo" and
148 "Something::Plugin::Bar" then as long as you either have either the
149 require or instantiate option set then we'll also find
150 "Something::Plugin::Bar". Nifty!
151
153 You can pass a hash of options when importing this module.
154
155 The options can be ...
156
157 sub_name
158 The name of the subroutine to create in your namespace.
159
160 By default this is 'plugins'
161
162 search_path
163 An array ref of namespaces to look in.
164
165 search_dirs
166 An array ref of directorys to look in before @INC.
167
168 instantiate
169 Call this method on the class. In general this will probably be 'new'
170 but it can be whatever you want. Whatever arguments are passed to
171 'plugins' will be passed to the method.
172
173 The default is 'undef' i.e just return the class name.
174
175 require
176 Just require the class, don't instantiate (overrides 'instantiate');
177
178 inner
179 If set to 0 will not search inner packages. If set to 1 will override
180 "require".
181
182 only
183 Takes a string, array ref or regex describing the names of the only
184 plugins to return. Whilst this may seem perverse ... well, it is. But
185 it also makes sense. Trust me.
186
187 except
188 Similar to "only" it takes a description of plugins to exclude from
189 returning. This is slightly less perverse.
190
191 package
192 This is for use by extension modules which build on
193 "Module::Pluggable": passing a "package" option allows you to place the
194 plugin method in a different package other than your own.
195
196 file_regex
197 By default "Module::Pluggable" only looks for .pm files.
198
199 By supplying a new "file_regex" then you can change this behaviour e.g
200
201 file_regex => qr/\.plugin$/
202
203 include_editor_junk
204 By default "Module::Pluggable" ignores files that look like they were
205 left behind by editors. Currently this means files ending in ~ (~), the
206 extensions .swp or .swo, or files beginning with .#.
207
208 Setting "include_editor_junk" changes "Module::Pluggable" so it does
209 not ignore any files it finds.
210
212 search_path
213 The method "search_path" is exported into you namespace as well. You
214 can call that at any time to change or replace the search_path.
215
216 $self->search_path( add => "New::Path" ); # add
217 $self->search_path( new => "New::Path" ); # replace
218
220 This does everything I need and I can't really think of any other
221 features I want to add. Famous last words of course
222
223 Recently tried fixed to find inner packages and to make it 'just work'
224 with PAR but there are still some issues.
225
226 However suggestions (and patches) are welcome.
227
229 Simon Wistow <simon@thegestalt.org>
230
232 Copyright, 2006 Simon Wistow
233
234 Distributed under the same terms as Perl itself.
235
237 None known.
238
240 File::Spec, File::Find, File::Basename, Class::Factory::Util,
241 Module::Pluggable::Ordered
242
243
244
245perl v5.12.4 2011-06-01 Module::Pluggable(3pm)