1Module::Pluggable(3) User Contributed Perl Documentation Module::Pluggable(3)
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 chang‐
29 ing 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 con‐
147 tains package definitions for both "Something::Plugin::Foo" and "Some‐
148 thing::Plugin::Bar" then as long as you either have either the require
149 or instantiate option set then we'll also find "Something::Plug‐
150 in::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
159 The name of the subroutine to create in your namespace.
160
161 By default this is 'plugins'
162
163 search_path
164
165 An array ref of namespaces to look in.
166
167 search_dirs
168
169 An array ref of directorys to look in before @INC.
170
171 instantiate
172
173 Call this method on the class. In general this will probably be 'new'
174 but it can be whatever you want. Whatever arguments are passed to
175 'plugins' will be passed to the method.
176
177 The default is 'undef' i.e just return the class name.
178
179 require
180
181 Just require the class, don't instantiate (overrides 'instantiate');
182
183 inner
184
185 If set to 0 will not search inner packages. If set to 1 will override
186 "require".
187
188 only
189
190 Takes a string, array ref or regex describing the names of the only
191 plugins to return. Whilst this may seem perverse ... well, it is. But
192 it also makes sense. Trust me.
193
194 except
195
196 Similar to "only" it takes a description of plugins to exclude from
197 returning. This is slightly less perverse.
198
199 package
200
201 This is for use by extension modules which build on "Module::Plug‐
202 gable": passing a "package" option allows you to place the plugin
203 method in a different package other than your own.
204
205 file_regex
206
207 By default "Module::Pluggable" only looks for .pm files.
208
209 By supplying a new "file_regex" then you can change this behaviour e.g
210
211 file_regex => qr/\.plugin$/
212
214 search_path
215
216 The method "search_path" is exported into you namespace as well. You
217 can call that at any time to change or replace the search_path.
218
219 $self->search_path( add => "New::Path" ); # add
220 $self->search_path( new => "New::Path" ); # replace
221
223 This does everything I need and I can't really think of any other fea‐
224 tures I want to add. Famous last words of course
225
226 Recently tried fixed to find inner packages and to make it 'just work'
227 with PAR but there are still some issues.
228
229 However suggestions (and patches) are welcome.
230
232 Simon Wistow <simon@thegestalt.org>
233
235 Copyright, 2006 Simon Wistow
236
237 Distributed under the same terms as Perl itself.
238
240 None known.
241
243 File::Spec, File::Find, File::Basename, Class::Factory::Util, Mod‐
244 ule::Pluggable::Ordered
245
246
247
248perl v5.8.8 2007-04-17 Module::Pluggable(3)