| 1 | =head1 NAME | ||
| 2 | |||
| 3 | WWW::Mechanize::Pluggable::Design - the architecture of WWW::Mechanize::Pluggable | ||
| 4 | |||
| 5 | =head1 DESCRIPTION | ||
| 6 | |||
| 7 | This document describes C<WWW::Mechanize::Pluggable>'s design | ||
| 8 | and explains how it works, including plugins and how they | ||
| 9 | interact with the base module. | ||
| 10 | |||
| 11 | =head1 BASICS | ||
| 12 | |||
| 13 | =head2 Why write this module? | ||
| 14 | |||
| 15 | Previous to the creation of C<WWW::Mechanize::Pluggable>, anyone who wanted | ||
| 16 | an extended version of C<WWW::Mechanize> had to subclass it to add new | ||
| 17 | features. | ||
| 18 | |||
| 19 | This in itself is not a bad thing: many modules have been created to | ||
| 20 | address specific behaviors that C<WWW::Mechanize> doesn't support | ||
| 21 | itself: | ||
| 22 | |||
| 23 | =over 4 | ||
| 24 | |||
| 25 | =item C<WWW::Mechanize::Sleepy> - pauses between requests | ||
| 26 | |||
| 27 | =item C<WWW::Mechanize::Cached> - caches requests | ||
| 28 | |||
| 29 | =item C<WWW::Mechanize::Timed> - times requests | ||
| 30 | |||
| 31 | =back | ||
| 32 | |||
| 33 | And so on. The problem is, what if you want both the C<Sleepy> behavior | ||
| 34 | and the C<Cached> behavior simultaneously? The answer is you can't do that | ||
| 35 | unless you write a module which inherits from both. | ||
| 36 | |||
| 37 | This approach isn't viable in the long term because the number of possible | ||
| 38 | combinations of behavior grows too fast. So how can we address this problem? | ||
| 39 | |||
| 40 | =head2 Enter C<Module::Pluggable> | ||
| 41 | |||
| 42 | A partial solution comes from C<Module::Pluggable>. This module allows | ||
| 43 | you to create I<plugins> - specially-named packages, installed just like | ||
| 44 | regular modules. A base package, which uses C<Module::Pluggable>, can | ||
| 45 | then automatically search for and load extra functions via these | ||
| 46 | plugin classes. | ||
| 47 | |||
| 48 | This solves the problem of extending a base class - if you're the one | ||
| 49 | who controls the base class's source code. | ||
| 50 | |||
| 51 | =head2 C<WWW::Mechanize::Pluggable> - plugins with a twist | ||
| 52 | |||
| 53 | The simplest way to solve the problem is just to create a subclass of | ||
| 54 | the class you want to add plugin functionality to - in our case, | ||
| 55 | C<WWW::Mechanize> - and then write plugins. And as long as all you're | ||
| 56 | doing is just adding new functions, you're in good shape. | ||
| 57 | |||
| 58 | But what if you want to I<change> the way something functions, rather than | ||
| 59 | just add something new? You have a problem, because a plugin can't do | ||
| 60 | that - or rather, two different plugins that want to alter the same base-class | ||
| 61 | method can't do so without knowing about each other. This might seem like | ||
| 62 | a good-enough solution, but it has the same problem as the "subclass the | ||
| 63 | subclasses" approach: you have a combinatorial explosion of checks that | ||
| 64 | have to be made every time a new module that wants to alter the same | ||
| 65 | base-class method gets into the act. | ||
| 66 | |||
| 67 | =head2 Proxy and Decorator | ||
| 68 | |||
| 69 | The approach used in C<WWW::Mechanize::Pluggable> is to combine the | ||
| 70 | proxy pattern (one class intercepts all the calls for another and | ||
| 71 | then passes them on) and the decorator pattern (one class | ||
| 72 | instantiates another, then exposes methods matching all of the | ||
| 73 | second classes methods, with its own code inserted before | ||
| 74 | and/or after the calls to the contained class). | ||
| 75 | |||
| 76 | Perl provides us with very flexible ways to deal with this | ||
| 77 | process. | ||
| 78 | |||
| 79 | =head3 AUTOLOAD | ||
| 80 | |||
| 81 | We use C<AUTOLOAD> to "catch" all the calls to the class. | ||
| 82 | We actually implement very little in C<WWW::Mechanize::Pluggable>; | ||
| 83 | only enough to be able to create the base object and its contained | ||
| 84 | C<WWW::Mechanize> object. | ||
| 85 | |||
| 86 | To decorate, we add a hash of pre-hook and post-hook lists. | ||
| 87 | These are subroutines to be called before (pre-hook) and after | ||
| 88 | (post-hook) the base-class methods. Now it's possible to alter | ||
| 89 | either the behavior of a given method, or to feed it different | ||
| 90 | parameters "behind the back" of the main program. | ||
| 91 | |||
| 92 | =head1 PLUGIN STRUCTURE | ||
| 93 | |||
| 94 | We'll speak specifically about C<WWW::Mechanize::Pluggable> | ||
| 95 | here; wo hope to extract the "pluggability" into a completely | ||
| 96 | separate module in the very near future, allowing the creation | ||
| 97 | of C<::Pluggable> versions of any module you please. | ||
| 98 | |||
| 99 | =head2 The Pluggable interface | ||
| 100 | |||
| 101 | |||
| 102 | |||
| 103 | =cut | ||
| 104 | |||
| 105 | 1 | ▶ | use strict; # or get dinged by CPANTS |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 106 | 1 | 1; | |
| 107 | |||