| 1 |
Name |
| 2 |
Class::Delegator - Simple and fast object-oriented delegation |
| 3 |
|
| 4 |
Synopsis |
| 5 |
package Car; |
| 6 |
|
| 7 |
use Class::Delegator |
| 8 |
send => 'start', |
| 9 |
to => '{engine}', |
| 10 |
|
| 11 |
send => 'power', |
| 12 |
to => 'flywheel', |
| 13 |
as => 'brake', |
| 14 |
|
| 15 |
send => [qw(play pause rewind fast_forward shuffle)], |
| 16 |
to => 'ipod', |
| 17 |
|
| 18 |
send => [qw(accelerate decelerate)], |
| 19 |
to => 'brakes', |
| 20 |
as => [qw(start stop)], |
| 21 |
|
| 22 |
send => 'drive', |
| 23 |
to => [qw(right_rear_wheel left_rear_wheel)], |
| 24 |
as => [qw(rotate_clockwise rotate_anticlockwise)] |
| 25 |
; |
| 26 |
|
| 27 |
Description |
| 28 |
This module provides a subset of the functionality of Damian Conway's |
| 29 |
lovely Class::Delegation module. Why a subset? Well, I didn't need all |
| 30 |
of the fancy matching semantics, just string string specifications to |
| 31 |
map delegations. Furthermore, I wanted it to be fast (See Benchmarks). |
| 32 |
And finally, since Class::Delegation uses an "INIT" block to do its |
| 33 |
magic, it doesn't work in persistent environments that don't execute |
| 34 |
"INIT" blocks, such as in mod_perl. |
| 35 |
|
| 36 |
However the specification semantics of Class::Delegator differ slightly |
| 37 |
from those of Class::Delegation, so this module isn't a drop-in |
| 38 |
replacement for Class::Delegation. Read on for details. |
| 39 |
|
| 40 |
Specifying methods to be delegated |
| 41 |
The names of methods to be redispatched can be specified using the |
| 42 |
"send" parameter. This parameter may be specified as a single string or |
| 43 |
as an array of strings. A single string specifies a single method to be |
| 44 |
delegated, while an array reference is a list of methods to be |
| 45 |
delegated. |
| 46 |
|
| 47 |
Specifying attributes to be delegated to |
| 48 |
Use the "to" parameter to specify the attribute(s) or accessor method(s) |
| 49 |
to which the method(s) specified by the "send" parameter are to be |
| 50 |
delegated. The semantics of the "to" parameter are a bit different from |
| 51 |
Class::Delegation. In order to ensure the fastest performance possible, |
| 52 |
this module simply installs methods into the calling class to handle the |
| 53 |
delegation. There is no use of $AUTOLOAD or other such trickery. But |
| 54 |
since the new methods are installed by "eval"ing a string, the "to" |
| 55 |
parameter for each delegation statement must be specified in the manner |
| 56 |
appropriate to accessing the underlying attribute. For example, to |
| 57 |
delegate a method call to an attribute stored in a hash key, simply wrap |
| 58 |
the key in braces: |
| 59 |
|
| 60 |
use Class::Delegator |
| 61 |
send => 'start', |
| 62 |
to => '{engine}', |
| 63 |
; |
| 64 |
|
| 65 |
To delegate to a method, simply name the method: |
| 66 |
|
| 67 |
use Class::Delegator |
| 68 |
send => 'power', |
| 69 |
to => 'flywheel', |
| 70 |
; |
| 71 |
|
| 72 |
If your objects are array-based, wrap the appropriate array index number |
| 73 |
in brackets: |
| 74 |
|
| 75 |
use Class::Delegator |
| 76 |
send => 'idle', |
| 77 |
to => '[3]', |
| 78 |
; |
| 79 |
|
| 80 |
And so on. |
| 81 |
|
| 82 |
Specifying the name of a delegated method |
| 83 |
Sometimes it's necessary for the name of the method that's being |
| 84 |
delegated to be different from the name of the method to which you're |
| 85 |
delegating execution. For example, your class might already have a |
| 86 |
method with the same name as the method to which you're delegating. The |
| 87 |
"as" parameter allows you translate the method name or names in a |
| 88 |
delegation statement. The value associated with an "as" parameter |
| 89 |
specifies the name of the method to be invoked, and may be a string or |
| 90 |
an array (with the number of elements in the array matching the number |
| 91 |
of elements in a corresponding "send" array). |
| 92 |
|
| 93 |
If the attribute is specified via a single string, that string is taken |
| 94 |
as the name of the attribute to which the associated method (or methods) |
| 95 |
should be delegated. For example, to delegate invocations of |
| 96 |
"$self->power(...)" to "$self->{flywheel}->brake(...)": |
| 97 |
|
| 98 |
use Class::Delegator |
| 99 |
send => 'power', |
| 100 |
to => '{flywheel}', |
| 101 |
as => 'brake', |
| 102 |
; |
| 103 |
|
| 104 |
If both the "send" and the "as" parameters specify array references, |
| 105 |
each local method name and deleted method name form a pair, which is |
| 106 |
invoked. For example: |
| 107 |
|
| 108 |
use Class::Delegator |
| 109 |
send => [qw(accelerate decelerate)], |
| 110 |
to => 'brakes', |
| 111 |
as => [qw(start stop)], |
| 112 |
; |
| 113 |
|
| 114 |
In this example, the "accelerate" method will be delegated to the |
| 115 |
"start" method of the "brakes" attribute and the "decelerate" method |
| 116 |
will be delegated to the "stop" method of the "brakes" attribute. |
| 117 |
|
| 118 |
Delegation to multiple attributes in parallel |
| 119 |
An array reference can be used as the value of the "to" parameter to |
| 120 |
specify the a list of attributes, *all of which* are delegated to--in |
| 121 |
the same order as they appear in the array. In this case, the "send" |
| 122 |
parameter must be a scalar value, not an array of methods to delegate. |
| 123 |
|
| 124 |
For example, to distribute invocations of "$self->drive(...)" to both |
| 125 |
"$self->{left_rear_wheel}->drive(...)" and |
| 126 |
"$self->{right_rear_wheel}->drive(...)": |
| 127 |
|
| 128 |
use Class::Delegator |
| 129 |
send => 'drive', |
| 130 |
to => ["{left_rear_wheel}", "{right_rear_wheel}"] |
| 131 |
; |
| 132 |
|
| 133 |
Note that using an array to specify parallel delegation has an effect on |
| 134 |
the return value of the delegation method specified by the "send" |
| 135 |
parameter. In a scalar context, the original call returns a reference to |
| 136 |
an array containing the (scalar context) return values of each of the |
| 137 |
calls. In a list context, the original call returns a list of array |
| 138 |
references containing references to the individual (list context) return |
| 139 |
lists of the calls. So, for example, if the "cost" method of a class |
| 140 |
were delegated like so: |
| 141 |
|
| 142 |
use Class::Delegator |
| 143 |
send => 'cost', |
| 144 |
to => ['supplier', 'manufacturer', 'distributor'] |
| 145 |
; |
| 146 |
|
| 147 |
then the total cost could be calculated like this: |
| 148 |
|
| 149 |
use List::Util 'sum'; |
| 150 |
my $total = sum @{$obj->cost()}; |
| 151 |
|
| 152 |
If both the "to" key and the "as" parameters specify multiple values, |
| 153 |
then each attribute and method name form a pair, which is invoked. For |
| 154 |
example: |
| 155 |
|
| 156 |
use Class::Delegator |
| 157 |
send => 'escape', |
| 158 |
to => ['{flywheel}', '{smokescreen}'], |
| 159 |
as => ['engage', 'release'], |
| 160 |
; |
| 161 |
|
| 162 |
would sequentially call, within the "escape()" delegation method: |
| 163 |
|
| 164 |
$self->{flywheel}->engage(...); |
| 165 |
$self->{smokescreen}->release(...); |
| 166 |
|
| 167 |
Benchmarks |
| 168 |
I whipped up a quick script to compare the performance of |
| 169 |
Class::Delegator to Class::Delegation and a manually-installed |
| 170 |
delegation method (the control). I'll let the numbers speak for |
| 171 |
themselves: |
| 172 |
|
| 173 |
Benchmark: timing 1000000 iterations of Class::Delegation, Class::Delegator, Manually... |
| 174 |
Class::Delegation: 106 wallclock secs (89.03 usr + 2.09 sys = 91.12 CPU) @ 10974.54/s (n=1000000) |
| 175 |
Class::Delegator: 3 wallclock secs ( 3.44 usr + 0.02 sys = 3.46 CPU) @ 289017.34/s (n=1000000) |
| 176 |
Control: 3 wallclock secs ( 3.01 usr + 0.02 sys = 3.03 CPU) @ 330033.00/s (n=1000000) |
| 177 |
|
| 178 |
Bugs |
| 179 |
Please send bug reports to <[email protected]> or report |
| 180 |
them via the CPAN Request Tracker at |
| 181 |
<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Class-Delegator>. |
| 182 |
|
| 183 |
Author |
| 184 |
David Wheeler <[email protected]> |
| 185 |
|
| 186 |
See Also |
| 187 |
Class::Delegation |
| 188 |
Damian Conway's brilliant module does ten times what this one |
| 189 |
does--and does it ten times slower. |
| 190 |
|
| 191 |
Class::Delegate |
| 192 |
Kurt Starsinic's module uses inheritance to manage delegation, and |
| 193 |
has a somewhat more complex interface. |
| 194 |
|
| 195 |
Class::HasA |
| 196 |
Simon Cozen's delegation module takes the same approach as this |
| 197 |
module, but provides no method for resolving method name clashes the |
| 198 |
way this module's "as" parameter does. |
| 199 |
|
| 200 |
Copyright and License |
| 201 |
Copyright (c) 2005-2008 David Wheeler. Some Rights Reserved. |
| 202 |
|
| 203 |
This module is free software; you can redistribute it and/or modify it |
| 204 |
under the same terms as Perl itself. |
| 205 |
|