Pluggable.pm

filter: Statementstmt 90.4% 113 / 125 Branchbran 65.7% 25 / 38 Conditioncond 25.0% 3 / 12 MC/DCmcdc 16.6% 1 / 6 Subroutinesub 96.3% 26 / 27 Podpod 100% 7 / 7 totaltotal 81.4% 175 / 215 CC 29 SCAR 26.5
CC
29
Cov
81%
AUTOLOAD
39.2
import
4.6
init
3.0
CRAP 14.1
↑ Summary Plugin/HelloWorld.pm »
1 package WWW::Mechanize::Pluggable;
2 12 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 12 ▶use WWW::Mechanize;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 12 ▶use Data::Dump::Streamer;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 12 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 12 ▶use Module::Pluggable search_path => [ qw(WWW::Mechanize::Plugin) ],
Subroutine BEGIN: called CC=1 SCAR=0.0
8 12 'require' => 1;
9  
10 12 our $AUTOLOAD;
11  
12 12 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
13 12 ▶ use vars qw ($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 12 $VERSION = "1.14";
15 }
16  
17 =head1 NAME
18  
19 WWW::Mechanize::Pluggable - A WWW::Mechanize that's custmomizable via plugins
20  
21 =head1 SYNOPSIS
22  
23 use WWW::Mechanize::Pluggable;
24 # plugins now automatically loaded
25  
26 =head1 DESCRIPTION
27  
28 This module provides all of the same functionality of C<WWW::Mechanize>, but
29 adds support for I<plugins> using C<Module::Pluggable>; this means that
30 any module named C<WWW::Mechanize::Plugin::I<whatever...>> will
31 be found and loaded when C<WWW::Mechanize::Pluggable> is loaded.
32  
33 Big deal, you say. Well, it I<becomes> a big deal in conjunction with
34 C<WWW::Mechanize::Pluggable>'s other feature: I<plugin hooks>. When plugins
35 are loaded, their C<import()> methods can call C<WWW::Mechanize::Pluggable>'s
36 C<prehook> and C<posthook> methods. These methods add callbacks to the
37 plugin code in C<WWW::Mechanize::Pluggable>'s methods. These callbacks can
38 act before a method or after it, and have to option of short-circuiting the
39 call to the C<WWW::Mechanize::Pluggable> method altogether.
40  
41 These methods receive whatever parameters the C<WWW::Mechanize::Pluggable>
42 methods received, plus a reference to the actvive C<Mech> object.
43  
44 All other extensions to C<WWW::Mechanize::Pluggable> are handled by the
45 plugins.
46  
47 =head1 SUBCLASSING
48  
49 Subclassing this class is not recommended; partly because the method
50 redispatch we need to do internally doesn't play well with the standard
51 Perl OO model, and partly because you should be using plugins and hooks
52 instead.
53  
54 In C<WWW::Mechanize>, it is recommended that you extend functionality by
55 subclassing C<WWW::Mechanize>, because there's no other way to extend the
56 class. With C<Module::Pluggable> support, it is easy to load another method
57 directly into C<WWW::Mechanize::Pluggable>'s namespace; it then appears as
58 if it had always been there. In addition, the C<pre_hook()> and C<post_hook()>
59 methods provide a way to intercept a call and replace it with your output, or
60 to tack on further processing at the end of a standard method (or even a
61 plugin!).
62  
63 The advantage of this is in not having a large number of subclasses, all of
64 which add or alter C<WWW::Mechanize>'s function, and all of which have to be
65 loaded if you want them available in your code. With
66 C<WWW::Mechanize::Pluggable>, one simply installs the desired plugins and they
67 are all automatically available when you C<use WWW::Mechanize::Pluggable>.
68  
69 Configuration is a possible problem area; if three different plugins all
70 attempt to replace C<get()>, only one will win. It's better to create more
71 sophisticated methods that call on lower-level ones than to alter existing
72 known behavior.
73  
74 =head1 USAGE
75  
76 See the synopsis for an example use of the base module; extended behavior is
77 documented in the plugin classes.
78  
79 =head1 BUGS
80  
81 None known.
82  
83 =head1 SUPPORT
84  
85 Contact the author at C<mcmahon@yahoo-inc.com>.
86  
87 =head1 AUTHOR
88  
89 Joe McMahon
90 mcmahon@yahoo-inc.com
91  
92 =head1 COPYRIGHT
93  
94 This program is free software; you can redistribute
95 it and/or modify it under the same terms as Perl itself.
96  
97 The full text of the license can be found in the
98 LICENSE file included with this module.
99  
100  
101 =head1 SEE ALSO
102  
103 L<WWW::Mechanize>
104  
105 =head1 CLASS METHODS
106  
107 =head2 import
108  
109 Handles the delegation of import options to the appropriate plugins.
110  
111 C<import> loads the plugins (found via a call to C<__PACKAGE__->plugins>) using
112 C<erquire>; it then calls each plugin's C<import> method with the parameters
113 specific to it, if there are any.
114  
115 =head3 What your plugin sees
116  
117 Let's take the example
118  
119 use WWW::Mechanize::Pluggable Zonk => [foo => 1, bar => [qw(a b c)]],
120 Thud => [baz => 'quux'];
121  
122 C<WWW::Mechanize::Plugin::Zonk>'s import() would get called like this:
123  
124 WWW::Mechanize::Plugin::Zonk->import(foo => 1, bar => [qw(a b c)]);
125  
126 And C<WWW::Mechanize::Plugin::Thud>'s import() would get
127  
128 WWW::Mechanize::Plugin::Thud->import(baz => 'quux');
129  
130 So each plugin only sees what it's supposed to.
131  
132 =cut
133  
134 11 ▶sub import {
Subroutine import: called CC=4 SCAR=15.2
135 11 my ($class, %plugin_args) = @_;
136 11 foreach my $plugin (__PACKAGE__->plugins) {
137 22 my ($plugin_name) = ($plugin =~ /.*::(.*)$/);
138 22 ▶ if ($plugin->can('import')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($plugin->can("import")) 22 0 22
139 22 ▶ if (exists $plugin_args{$plugin_name}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $plugin_args{$plugin_name}) { } 0 22 22
140 0 $plugin->import( @{ $plugin_args{$plugin_name} } );
141 }
142 else {
143 22 $plugin->import();
144 }
145 }
146 }
147 }
148  
149 =head2 init
150  
151 C<init> runs through all of the plugins for this class and calls
152 their C<init> methods (if they exist). Not meant to be called by your
153 code; it's internal-use-only.
154  
155 C<init> gets all of the arguments supplied to C<new>; it can
156 process them or not as it pleases.
157  
158 =head3 What your plugin sees
159  
160 Your plugin's C<init> gets a reference to the C<Pluggable> object
161 plus the list of parameters supplied to the C<new()> call. This is
162 assumewd to be a set of zero or more key/value pairs.
163  
164 C<init> can return a list of keys to be deleted from the parameter
165 hash; this allows plugins to process parameters themselves without
166 the internal C<WWW::Mechanize> object ever seeing them. If you
167 return a null list, nothing gets deleted.
168  
169 As an example:
170  
171 my $mech = new WWW::Mechanize::Pluggable foo=>'bar';
172  
173 A plugin's C<init> could process the C<foo> argument and return C<foo>;
174 this parameter would then be deleted from the arguments.
175  
176 =cut
177  
178 12 ▶sub init {
Subroutine init: called CC=3 SCAR=11.0
Pod documented
179 12 my ($self, %args) = @_;
180 # call all the inits (if defined) in all our
181 # plugins so they can all set up their defaults
182 12 my @deletes;
183 12 foreach my $plugin (__PACKAGE__->plugins) {
184 24 ▶ if ($plugin->can('init')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($plugin->can("init")) 12 12 24
185 12 push @deletes, $plugin->init($self, %args);
186 }
187 }
188 12 @deletes;
189 }
190  
191 =head2 new
192  
193 C<new> constructs a C<WWW::Mechanize::Pluggable> object and initializes
194 its pre and port hook queues. You can add parameters to be passed to
195 plugins' C<init> methods by adding them to this C<new> call.
196  
197 =cut
198  
199 12 ▶sub new {
Subroutine new: called CC=2 SCAR=6.9
Pod documented
200 12 my ($class, %args) = @_;
201 12 my $self = {};
202 12 bless $self, $class;
203  
204  
205 12 $self->{PreHooks} = {};
206 12 $self->{PostHooks} = {};
207 12 my @deletes = $self->init(%args);
208  
209 12 local $_;
210 12 delete $args{$_} foreach @deletes;
211  
212  
213 12 $self->mech($self->_create_mech_object(\%args));
214  
215 12 $self;
216 }
217  
218 =head2 _create_mech_object
219  
220 Create the WWW::Mechanize object. Optional parameter '_Pluggable_mech_class'
221 specifies a different class, e.g. Test::WWW::Mechanize.
222  
223 =cut
224  
225 12 ▶sub _create_mech_object {
Subroutine _create_mech_object: called CC=2 SCAR=7.0
226 12 my ($self, $args) = @_;
227  
228 12 my $mech_class = delete $args->{_Pluggable_mech_class};
229 12 ▶ $mech_class = 'WWW::Mechanize' if !defined($mech_class);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $mech_class 12 0 12
230 12 $mech_class->new(%$args);
231 }
232  
233 =head2 mech
234  
235 Returns the component C<WWW::Mechanize> object.
236  
237 This is a simple set/get accessor; normally we'd just use L<Class::Accessor>
238 to create it and forget about the details. We don't use C<Class::Accessor>,
239 though, because we want the C<WWW::Mechanize::Pluggable> class to have no
240 superclass (other than C<UNIVERSAL>).
241  
242 This is necessary because we use C<AUTOLOAD> (q.v.) to trap all of the calls
243 to this class so they can be pre- and post-processed before being passed on
244 to the underlying C<WWW::Mechanize> object. If we C<use base qw(Class::Accessor)>,
245 as is needed to make it work properly, C<Class::Accessor>'s C<AUTOLOAD> gets control
246 instead of ours, and the hooks don't work.
247  
248 =cut
249  
250 22 ▶sub mech {
Subroutine mech: called CC=2 SCAR=6.9
Pod documented
251 22 my ($self, $mech) = @_;
252 22 ▶ $self->{Mech} = $mech if defined $mech;
Branch: 1 branch
BranchTrueFalseTotal
if defined $mech 12 10 22
253 22 $self->{Mech};
254 }
255  
256 =head2 _insert_hook
257  
258 Adds a hook to a hook queue. This is a utility routine, encapsulating
259 the hook queue manipulation in a single method.
260  
261 Needs the queue name, the method name of the method being hooked, and a
262 reference to the hook sub itself.
263  
264 =cut
265  
266 4 ▶sub _insert_hook {
Subroutine _insert_hook: called CC=1 SCAR=0.0
267 4 my ($self, $which, $method, $hook_sub) = @_;
268 4 push @{$self->{$which}->{$method}}, $hook_sub;
269 }
270  
271 =head2 _remove_hook
272  
273 Deletes a hook from a hook queue.
274  
275 Needs the queue name, the method name of the method being hooked, and a
276 reference to the hook sub itself.
277  
278 =cut
279  
280 1 ▶sub _remove_hook {
Subroutine _remove_hook: called CC=2 SCAR=7.0
281 1 my ($self, $which, $method, $hook_sub) = @_;
282 $self->{$which}->{$method} =
283 4 [grep { "$_" ne "$hook_sub"} @{$self->{$which}->{$method}}]
284 1 ▶ if defined $self->{$which}->{$method};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{$which}{$method} 1 0 1
285 }
286  
287 =head2 pre_hook
288  
289 Shortcut to add a hook to a method's pre queue. Needs a method name
290 and a reference to a subroutine to be called as the hook.
291  
292 =cut
293  
294 4 ▶sub pre_hook {
Subroutine pre_hook: called CC=1 SCAR=0.0
Pod documented
295 4 my $self = shift;
296 4 $self->_insert_hook(PreHooks=>@_);
297 }
298  
299 =head2 post_hook
300  
301 Shortcut to add a hook to a method's post queue. Needs a method
302 name and a reference to the subroutine to be called as the hook.
303  
304 =cut
305  
306 0 ▶sub post_hook {
Subroutine post_hook: not called CC=1 SCAR=6.9
Pod documented
307 0 my $self = shift;
308 0 $self->_insert_hook(PostHooks=>@_);
309 }
310  
311 =head2 last_method
312  
313 Records the last method used to call C<WWW::Mechanize::Pluggable>.
314 This allows plugins to call a method again if necessary without
315 having to know what method was actually called.
316  
317 =cut
318  
319 11 ▶sub last_method {
Subroutine last_method: called CC=2 SCAR=6.9
Pod documented
320 11 my($self, $method) = @_;
321 11 ▶ $self->{LastMethod} = $method if defined $method;
Branch: 1 branch
BranchTrueFalseTotal
if defined $method 10 1 11
322 11 $self->{LastMethod};
323 }
324  
325 =head1 AUTOLOAD
326  
327 This subroutine implements a mix of the "decorator" pattern and
328 the "proxy" pattern. It intercepts all the calls to the underlying class,
329 and also wraps them with pre-hooks (called before the method is called)
330 and post-hooks (called after the method is called). This allows us to
331 provide all of the functionality of C<WWW::Mechanize> in this class
332 without copying any of the code, and to alter the behavior as well
333 without altering the original class.
334  
335 Pre-hooks can cause the actual method call to the underlying class
336 to be skipped altogether by returning a true value.
337  
338 =cut
339  
340 24 ▶sub AUTOLOAD {
Subroutine AUTOLOAD: called CC=18 SCAR=36.7
341 24 ▶ return if $AUTOLOAD =~ /DESTROY/;
Branch: 1 branch
BranchTrueFalseTotal
if $WWW::Mechanize::Pluggable::AUTOLOAD =~ /DESTROY/ 13 11 24
342  
343 # don't shift; this might be a straight sub call!
344 11 my $self = $_[0];
345  
346 # figure out what was supposed to be called.
347 11 (my $super_sub = $AUTOLOAD) =~ s/::Pluggable//;
348 11 my ($class, $plain_sub) = ($AUTOLOAD =~ /\A(.*)::(.*)$/);
349  
350 # Determine if this is a class method call or a subroutine call. Getting here
351 # for either means that they haven't been defined and we don't know how to
352 # find them.
353 11 my $call_type;
354 11 ▶ if (scalar @_ == 0 or !defined $_[0] or !ref $_[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @_ == 0 or not defined $_[0] or not ref $_[0]) 1 10 11
Condition50%3/6
scalar @_ == 0 or not defined $_[0] or not ref $_[0]
l!l&&r!l&&!r
0110
scalar @_ == 0 or not defined $_[0]
l!l&&r!l&&!r
0011
MC/DC33%1/3
scalar @_ == 0 or not defined $_[0] or not ref $_[0]
scalar @_ == 0 not defined $_[0] not ref $_[0]
Truth table50%2/4
A or not(B) or not(C)
A= scalar @_ == 0B= not defined $_[0]C= not ref $_[0]
ABCresult
1XX1
01X1
0011
0000
355 1 ▶ $call_type = ( $_[0] eq $class ? 'class method' : 'subroutine' );
Branch: 1 branch
BranchTrueFalseTotal
$_[0] eq $class ? : 1 0 1
356 }
357  
358 11 ▶ die "Can't resolve $call_type $plain_sub(). Did your plugins define it?"
Branch: 1 branch
BranchTrueFalseTotal
if $call_type 1 10 11
359 if $call_type;
360  
361 # Record the method name so plugins can check it.
362 10 $self->last_method($plain_sub);
363  
364 10 my ($ret, @ret) = "";
365 10 shift @_;
366 10 my $skip;
367 10 ▶ if (my $pre_hook = $self->{PreHooks}->{$plain_sub}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $pre_hook = $self->{"PreHooks"}{$plain_sub}) 0 10 10
368 # skip call to actual method if pre_hook returns false.
369 # pre_hook must muck with Mech object to really return anything.
370 0 foreach my $hook (@$pre_hook) {
371 0 my $result = $hook->($self, $self->mech, @_);
372 0 ▶ $skip ||= (defined $result) && ($result == -1);
Condition0%0/6
$skip ||= defined $result && $result == -1
l!l&&r!l&&!r
000
defined $result && $result == -1
!ll&&!rl&&r
000
MC/DC0%0/3
$skip ||= defined $result && $result == -1
$skip defined $result $result == -1
Truth table0%0/4
A ||= B && C
A= $skipB= defined $resultC= $result == -1
ABCresult
1XX1
0111
00X0
0100
373 }
374 }
375 10 ▶ unless ($skip) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($skip) 10 0 10
376 10 ▶ if (wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 0 10 10
377 0 @ret = eval { $self->mech->$plain_sub(@_) };
378 0 ▶ croak $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
379 }
380 else {
381 10 $ret = eval { $self->mech->$plain_sub(@_) };
382 10 ▶ croak $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 3 7 10
383 }
384 }
385 7 ▶ if (my $post_hook = $self->{PostHooks}->{$plain_sub}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $post_hook = $self->{"PostHooks"}{$plain_sub}) 0 7 7
386 # Same deal here. Anything you want to return has to go in the object.
387 0 foreach my $hook (@$post_hook) {
388 0 $hook->($self, $self->mech, @_);
389 }
390 }
391 7 ▶ wantarray ? @ret : $ret;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 7 7
392 }
393  
394 =head2 clone
395  
396 An ovveride for C<WWW::Mechanize>'s C<clone()> method; uses YAML to make sure
397 that the code references get cloned too. Note that this is important for
398 later code (the cache stuff in particular); general users won't notice
399 any real difference.
400  
401 There's been some discussion as to whether this is totally adequate (for
402 instance, if the code references are closures, they won't be properly cloned).
403 For now, we'll go with this and see how it works.
404  
405 =cut
406  
407 1 ▶sub clone {
Subroutine clone: called CC=2 SCAR=6.9
Pod documented
408 1 my $self = shift;
409 # Name created by eval; works out to a no-op.
410 my $value =
411 12 ▶ eval { no strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
412 1 local $WWW_Mechanize_Pluggable1;
413 1 ▶ eval Dump($self)->Out();
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
414 1 $WWW_Mechanize_Pluggable1;
415 };
416 1 ▶ die "clone failed: $@\n" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 1 1
417 1 return $value;
418 }
419  
420 =head1 TODO
421  
422 The plugin mechanism is ridiculously programmer-intensive. This needs to be
423 replaced with something better.
424  
425 =cut
426  
427 12 1; #this line is important and will help the module return a true value
428 __END__
↑ Summary Plugin/HelloWorld.pm »