blib/lib/Class/Monkey.pm

filter: Statementstmt 22.9% 65 / 283 Branchbran 5.7% 4 / 70 Conditioncond 0.0% 0 / 11 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 39.1% 18 / 46 Podpod 93.3% 14 / 15 totaltotal 23.4% 101 / 431 CC 48 SCAR 23.6
CC
48
Cov
19%
__ANON__
30.0
has
25.2
_extend_class
20.7
CRAP 10.6
↑ Summary
1 package Class::Monkey;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 our $VERSION = '0.007';
8 1 $Class::Monkey::Subs = {};
9 1 $Class::Monkey::CanPatch = [];
10 1 $Class::Monkey::Classes = [];
11 1 $Class::Monkey::Iter = 0;
12  
13 =head1 NAME
14  
15 Class::Monkey - Monkey Patch a class/instance with modifiers and other sweet stuff
16  
17 =head1 DESCRIPTION
18  
19 Say we have a module installed on the system. It does some handy things, but you find a bug or a strange feature. We can easily fix it without subclassing by the following...
20  
21 # StupidClass.pm
22 package SupidClass;
23
24 sub new {
25 my $class = shift;
26 return bless {}, $class;
27 }
28  
29 sub name {
30 my ($self, $name) = @_;
31 print "Hello, ${name}\n";
32 }
33  
34 sub no_args {
35 print "No arguments were specified!\n";
36 }
37  
38 1;
39  
40 Above is our class. A stupid one at that. The C<name> method doesn't validate the arguments.. it just tries to print them in a 'hello' string.
41 We can use an C<around> method to call the C<name> method if arguments are passed, or to call C<no_args> if not. We can happily do this from the program.
42  
43 # our_program.pl
44 use Class::Monkey qw<StupidClass>;
45  
46 # The patch
47 around 'name' => sub {
48 my $method = shift;
49 my $self = shift;
50
51 if (@_) {
52 $self->$method(@_);
53 }
54 else {
55 $self->no_args();
56 }
57 },
58 qw<StupidClass>;
59 # /The Patch
60
61 $s->name(); # actually executes no_args
62 $s->name("World"): # runs name
63  
64 =head1 SYNOPSIS
65  
66 Simply import the classes you want to patch as an array when you C<use Class::Monkey>. Doing this means you won't even need to C<use> the module you want to patch - Class::Monkey takes care of that for you.
67  
68 use Class::Monkey qw<Some::Package Another::Module>;
69  
70 method 'needThisMethod' => sub {
71 ...
72 },
73 qw<Some::Package>;
74  
75 my $p = Some::Package->new;
76 $p->needThisMethod;
77  
78 =head1 METHODS
79  
80 =cut
81  
82 1 ▶sub import {
Subroutine import: called CC=6 SCAR=26.9
83 1 my ($class, @args) = @_;
84 1 my $pkg = scalar caller;
85 1 my $tweak = 0;
86 1 ▶ if (scalar @args > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @args > 0) 0 1 1
87 0 for my $m (@args) {
88 0 ▶ if ($m eq '-tweak') {
Branch: 1 branch
BranchTrueFalseTotal
if ($m eq "-tweak") 0 0 0
89 0 $tweak = 1;
90 0 my ($index) = grep { $args[$_] eq '-tweak' } 0..$#args;
91 0 splice @args, $index, 1;
92 0 next;
93 }
94 0 push @{$Class::Monkey::CanPatch}, $m;
95 }
96 0 _extend_class(\@args, $pkg);
97 }
98  
99 _import_def(
100 1 ▶ $pkg,
Branch: 1 branch
BranchTrueFalseTotal
unless $tweak 1 0 1
101 undef,
102 qw/
103 override
104 method
105 before
106 after
107 around
108 unpatch
109 instance
110 original
111 has
112 extends
113 exports
114 canpatch
115 /
116 ) unless $tweak;
117  
118 1 ▶ _import_def($pkg, undef, qw<tweak haz>) if $tweak;
Branch: 1 branch
BranchTrueFalseTotal
if $tweak 0 1 1
119 }
120  
121 0 ▶sub _extend_class {
Subroutine _extend_class: not called CC=5 SCAR=30.3
122 0 my ($mothers, $class) = @_;
123  
124 0 ▶ return if $class eq __PACKAGE__;
Branch: 1 branch
BranchTrueFalseTotal
if $class eq __PACKAGE__ 0 0 0
125 0 foreach my $mother (@$mothers) {
126 # if class is unknown to us, import it (FIXME)
127 0 ▶ unless (grep { $_ eq $mother } @$Class::Monkey::Classes) {
Branch: 1 branch
BranchTrueFalseTotal
unless (grep {$_ eq $mother;} @$Class::Monkey::Classes) 0 0 0
128 0 eval "use $mother";
129 0 ▶ warn "Could not load $mother: $@"
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
130 if $@;
131  
132 0 $mother->import;
133 }
134 0 push @$Class::Monkey::Classes, $mother;
135 }
136  
137 {
138 0 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.2
139 0 @{"${class}::ISA"} = @$mothers;
140 }
141 }
142  
143 =head2 haz
144  
145 Please see C<tweak> for more information on how to get this method. C<haz> behaves the exact same way as C<extends>.
146  
147 use Class::Monkey '-tweak';
148  
149 haz 'FooClass';
150 haz qw<FooClass Another::FooClass More::FooClass>;
151  
152 =cut
153  
154 0 ▶sub haz {
Subroutine haz: not called CC=3 SCAR=24.8
Pod documented
155 0 my (@args) = @_;
156 0 my $pkg = getscope();
157 0 ▶ if (scalar @args > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @args > 0) 0 0 0
158 0 for my $m (@args) {
159 0 push @{$Class::Monkey::CanPatch}, $m;
160 }
161 0 _extend_class(\@args, $pkg);
162 }
163 }
164  
165 =head2 tweak
166  
167 This method is only available when you C<use Class::Monkey '-tweak'>. This option may be preferred over the default modifier methods when you need to patch a class from a script using Moose/Mouse/Moo/Mo, etc. When you add -tweak, it will export only the C<tweak> and C<haz> methods.
168  
169 use Class::Monkey '-tweak';
170 haz 'Foo';
171  
172 tweak 'mymethod' => (
173 class => 'Foo',
174 override => sub {
175 print "mymethod has been overridden\n";
176 },
177 );
178  
179 You can replace 'override' in the above example with any of the available Class::Monkey modifiers (ie: before, method, after, around). Also C<class> can be the full name of the class as above, or an instance.
180  
181 =cut
182
183 0 ▶sub tweak {
Subroutine tweak: not called CC=2 SCAR=10.9
Pod documented
184 0 my ($sub, %args) = @_;
185
186 0 my $class = delete $args{class};
187 {
188 0 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.2
189 0 foreach my $action (keys %args) {
190 0 $action->($sub, $args{$action}, $class);
191 }
192 }
193 }
194  
195 1 ▶sub _import_def {
Subroutine _import_def: called CC=4 SCAR=15.5
196 1 my ($pkg, $from, @subs) = @_;
197 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
198 1 ▶ if ($from) {
Branch: 1 branch
BranchTrueFalseTotal
if ($from) { } 0 1 1
199 0 for (@subs) {
200 0 *{$pkg . "::$_"} = \&{"$from\::$_"};
201 }
202 }
203 else {
204 1 for (@subs) {
205 12 *{$pkg . "::$_"} = \&$_;
206 }
207 }
208 }
209  
210 0 ▶sub _doh {
Subroutine _doh: not called CC=1 SCAR=6.9
211 0 my $err = shift;
212 0 die $err . "\n";
213 }
214  
215 0 ▶sub _check_init {
Subroutine _check_init: not called CC=4 SCAR=30.0
216 0 my $class = shift;
217  
218 0 ▶ $class = ref($class) if ref($class);
Branch: 1 branch
BranchTrueFalseTotal
if ref $class 0 0 0
219 0 ▶ _doh "No class was specified" if ! $class;
Branch: 1 branch
BranchTrueFalseTotal
unless $class 0 0 0
220  
221 _doh "Not allowed to patch $class"
222 0 ▶ if ! grep { $_ eq $class } @{$Class::Monkey::CanPatch};
Branch: 1 branch
BranchTrueFalseTotal
if not grep({$_ eq $class;} @{$Class::Monkey::CanPatch;}) 0 0 0
223 }
224  
225 =head2 canpatch
226  
227 Tells Class::Monkey you want to be able to patch the specified modules, but not to 'use' them.
228  
229 use Class::Monkey;
230 use MyFoo;
231  
232 canpatch qw<MyFoo AndThis AndThat>;
233  
234 # then do stuff with MyFoo as normal
235  
236 =cut
237  
238 0 ▶sub canpatch {
Subroutine canpatch: not called CC=1 SCAR=6.9
Pod documented
239 0 my (@modules) = @_;
240
241 0 push @{$Class::Monkey::CanPatch}, @modules;
242 }
243  
244 0 ▶sub _add_to_subs {
Subroutine _add_to_subs: not called CC=2 SCAR=13.4
245 0 my $sub = shift;
246 0 ▶ if (! exists $Class::Monkey::Subs->{$sub}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $Class::Monkey::Subs->{$sub}) 0 0 0
247 0 $Class::Monkey::Subs->{$sub} = {};
248 0 $Class::Monkey::Subs->{$sub} = \&{$sub};
249 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
250 0 *{__PACKAGE__ . "::$sub"} = \&{$sub};
251 }
252 }
253  
254 0 ▶sub getscope {
Subroutine getscope: not called CC=2 SCAR=17.9
Pod undocumented
255 0 my $self = shift;
256 0 ▶ my $pkg = $self||scalar caller(1);
Condition0%0/2
$self || scalar caller 1
l!l
00
MC/DC0%0/1
$self || scalar caller 1
$self
Truth table0%0/2
A
A= $self
Aresult
11
00
257 0 return $pkg;
258 }
259  
260 =head2 exports
261  
262 Have a subroutine in your file you want to explort to your patched class? Use C<exports> to do so.
263  
264 package Foo;
265  
266 sub new { return bless {}, __PACKAGE__ }
267  
268 1;
269  
270 # test.pl
271 package MyPatcher;
272  
273 use Class::Monkey qw<Foo>;
274
275 sub foo { print "Hiya\n"; }
276  
277 exports 'foo', qw<Foo>;
278 my $foo = Foo->new;
279 $foo->foo(); # prints Hiya
280
281 exports 'foo', $foo; # works with instances too
282  
283 =cut
284  
285 0 ▶sub exports {
Subroutine exports: not called CC=2 SCAR=14.8
Pod documented
286 0 my ($method, $class) = @_;
287 0 my $pkg = caller;
288 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
289 0 ▶ if (ref($class)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $class) { } 0 0 0
290 0 $Class::Monkey::Iter++;
291 0 my $package = ref($class) . '::Class::Monkey::' . $Class::Monkey::Iter;
292 0 @{$package . '::ISA'} = (ref($class));
293 0 *{"${package}::${method}"} = *{"${pkg}::${method}"};
294 0 bless $_[1], $package;
295 }
296 else {
297 0 *{"${class}::${method}"} = *{"${pkg}::${method}"};
298 }
299 }
300  
301
302 =head2 extends
303  
304 Sometimes you might not want to include the module you want to patch when you C<use Class::Monkey>. No problem. You can use C<extends> to do it later on.
305  
306 use Class::Monkey;
307 extends 'SomeClass';
308 extends qw<SomeClass FooClass>;
309  
310 =cut
311  
312 0 ▶sub extends {
Subroutine extends: not called CC=3 SCAR=24.8
Pod documented
313 0 my (@args) = @_;
314 0 my $pkg = getscope;
315 0 ▶ if (scalar @args > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @args > 0) 0 0 0
316 0 for my $m (@args) {
317 0 push @{$Class::Monkey::CanPatch}, $m;
318 }
319 0 _extend_class(\@args, $pkg);
320 }
321 }
322  
323 =head2 has
324  
325 Gives the wanted class an accessor. You can assign it a read-only or read-writable type (Similar to Moose).
326 Because it works on remote packages you need to give it the full name of the method including the class.
327  
328 use Class::Monkey qw<Foo::Class>;
329
330 has 'Foo::Class::greet' => ( is => 'ro', default => 'Hello' ); # read-only
331 has 'Foo::Class::name' => ( is => 'rw', default => 'World' ); # read-writable
332
333 my $foo = Foo::Class->new;
334 say "Hello, " . $foo->name;
335
336 $foo->name('Monkey); # updates the name accessor to return a new value
337  
338 If you leave out the C<is> parameter when you define an accessor it will always default to read-writable.
339  
340 =cut
341  
342 0 ▶sub has {
Subroutine has: not called CC=5 SCAR=32.3
Pod documented
343 0 my ($name, %args) = @_;
344 0 ▶ my $rtype = delete $args{is}||"";
Condition0%0/2
delete $args{'is'} || ''
l!l
00
MC/DC0%0/1
delete $args{'is'} || ''
delete $args{'is'}
Truth table0%0/2
A
A= delete $args{'is'}
Aresult
11
00
345 0 ▶ my $default = delete $args{default}||"";
Condition0%0/2
delete $args{'default'} || ''
l!l
00
MC/DC0%0/1
delete $args{'default'} || ''
delete $args{'default'}
Truth table0%0/2
A
A= delete $args{'default'}
Aresult
11
00
346 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
347 0 ▶ if ($rtype eq 'ro') {
Branch: 1 branch
BranchTrueFalseTotal
if ($rtype eq 'ro') { } 0 0 0
348 0 ▶ if (! $default) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($default) 0 0 0
349 0 warn "Redundant null static accessor '$name'";
350 }
351 0 ▶ *{$name} = sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
352 0 my ($self, $val) = @_;
353 0 ▶ if (@_ == 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 2) 0 0 0
354 0 warn "Cannot alter a Read-Only accessor";
355 0 return ;
356 }
357 0 return $default;
358 0 };
359 }
360 else {
361 0 ▶ *{$name} = sub {
Subroutine __ANON__: not called CC=5 SCAR=34.0
362 0 my ($self, $val) = @_;
363 0 ▶ if ($default && ! $self->{_used}->{$name}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($default and not $self->{"_used"}{$name}) 0 0 0
Condition0%0/3
$default and not $self->{"_used"}{$name}
!ll&&!rl&&r
000
MC/DC0%0/2
$default and not $self->{"_used"}{$name}
$default not $self->{"_used"}{$name}
Truth table0%0/3
A and not(B)
A= $defaultB= not $self->{"_used"}{$name}
ABresult
0X0
100
111
364 0 $self->{$name} = $default;
365 0 $self->{_used}->{$name} = 1;
366 }
367 0 ▶ if (@_ == 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 2) { } 0 0 0
368 0 $self->{$name} = $val;
369 }
370 else {
371 0 ▶ return $self->{$name}||"";
Condition0%0/2
$self->{$name} || ''
l!l
00
MC/DC0%0/1
$self->{$name} || ''
$self->{$name}
Truth table0%0/2
A
A= $self->{$name}
Aresult
11
00
372 }
373 0 };
374 }
375 }
376 # modifiers
377  
378 =head2 instance
379  
380 B<Note> This method should be deprecated as all modifiers now support constants OR an instance. Class::Monkey will determine which method should be used, so calling C<instance> is no longer required.
381  
382 Patch an instance method instead of an entire class
383  
384 # Pig.pm
385 package Pig;
386 sub new { return bless {}, shift; }
387 sub says { print "Oink!\n"; }
388  
389 # test.pl
390 package main;
391 use Class::Monkey qw<Pig>;
392  
393 my $pig = Pig->new;
394 my $pig2 = Pig->new;
395 instance 'says' => sub {
396 print "Meow\n";
397 },
398 $pig2;
399  
400 # only $pig2 will have its says method overridden
401  
402 As of 0.002 you can now do it like this
403  
404 override 'says' => sub {
405 print "Meow\n";
406 }, $pig2;
407  
408 before 'says' => sub {
409 print "Going to speak\n";
410 }, $pig;
411  
412 etc..
413  
414 =cut
415  
416 0 ▶sub instance {
Subroutine instance: not called CC=1 SCAR=3.3
Pod documented
417 0 my($method, $code, $instance) = @_;
418 0 $Class::Monkey::Iter++;
419 0 my $package = ref($instance) . '::Class::Monkey::' . $Class::Monkey::Iter;
420 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
421 0 @{$package . '::ISA'} = (ref($instance));
422 0 *{$package . '::' . $method} = $code;
423 0 bless $_[2], $package;
424 }
425  
426 =head2 original
427  
428 If you want to run the original version of a patched method, but not unpatch it right away
429 you can use C<original> to do so. It will run the old method before it was patched with any arguments you specify, but the actual method will still remain patched.
430  
431 after 'someMethod' => sub {
432 print "Blah\n"
433 },
434 qw<Foo>;
435  
436 original('Foo', 'someMethod', qw<these are my args>);
437  
438 OR if you prefer, you can just call C<Class::Monkey::PatchedClassName::method->(@args)>
439  
440 Class::Monkey::Foo->someMethod('these', 'are', 'my', 'args);
441  
442 =cut
443  
444 0 ▶sub original {
Subroutine original: not called CC=2 SCAR=17.9
Pod documented
445 0 my ($class, $method, @args) = @_;
446 0 ▶ if (exists $Class::Monkey::Subs->{"$class\::$method"}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $$Class::Monkey::Subs{"${class}::$method"}) { } 0 0 0
447 0 $Class::Monkey::Subs->{"$class\::$method"}->(@args);
448 }
449 else {
450 0 warn "Could not run original method '$method' in class $class. Not found";
451 0 return 0;
452 }
453 }
454  
455 =head2 override
456  
457 Overrides an already existing method. If the target method doesn't exist then Class::Monkey will throw an error.
458  
459 override 'foo' => sub {
460 return "foo bar";
461 },
462 qw<Some::Module>;
463  
464 =cut
465  
466 0 ▶sub override {
Subroutine override: not called CC=4 SCAR=27.1
Pod documented
467 0 my ($method, $code, $class) = @_;
468  
469 0 _check_init($class);
470  
471 0 ▶ _doh "You need to specify a class to which your overridden method exists"
Branch: 1 branch
BranchTrueFalseTotal
unless $class 0 0 0
472 if ! $class;
473  
474 0 ▶ _doh "Method $method does not exist in $class. Perhaps you meant 'method' instead of 'override'?"
Branch: 1 branch
BranchTrueFalseTotal
unless $class->can($method) 0 0 0
475 if ! $class->can($method);
476  
477 0 _add_to_subs("$class\::$method");
478 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
479 0 ▶ *$method = sub { $code->(@_) };
Subroutine __ANON__: not called CC=1 SCAR=6.9
480 0 ▶ if (ref($class)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $class) { } 0 0 0
481 0 $Class::Monkey::Iter++;
482 0 my $package = ref($class) . '::Class::Monkey::' . $Class::Monkey::Iter;
483 0 @{$package . '::ISA'} = (ref($class));
484 0 *{"${package}::${method}"} = \*$method;
485 0 bless $_[2], $package;
486 }
487 else {
488 0 *{$class . "::$method"} = \*$method;
489 }
490 }
491  
492 =head2 method
493  
494 Creates a brand new method in the target module. It will NOT allow you to override an existing one using this, and will throw an error.
495  
496 method 'active_customers' => sub {
497 my $self = shift;
498 return $self->search({ status => 'active' });
499 },
500 qw<Schema::ResultSet::Customer>;
501  
502 =cut
503  
504 0 ▶sub method {
Subroutine method: not called CC=3 SCAR=20.6
Pod documented
505 0 my ($method, $code, $class) = @_;
506
507 0 _check_init($class);
508 0 ▶ _doh "You need to specify a class to which your created method will be initialised"
Branch: 1 branch
BranchTrueFalseTotal
unless $class 0 0 0
509 if ! $class;
510
511 0 ▶ _doh "The method '$method' already exists in $class. Did you want to 'override' it instead?"
Branch: 1 branch
BranchTrueFalseTotal
if $class->can($method) 0 0 0
512 if $class->can($method);
513  
514 0 _add_to_subs("$class\::$method");
515 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
516 0 ▶ *$method = sub { $code->(@_); };
Subroutine __ANON__: not called CC=1 SCAR=6.9
517  
518 0 *{$class . "::$method"} = \*$method;
519 }
520  
521 =head2 before
522  
523 Simply adds code to the target method before the original code is ran
524  
525 # Foo.pm
526 package Foo;
527
528 sub new { return bless {}, __PACKAGE__; }
529 sub hello { print "Hello, $self->{name}; }
530 1;
531  
532 # test.pl
533 use Class::Monkey qw<Foo>;
534
535 my $foo = Foo->new;
536 before 'hello' => {
537 my $self = shift;
538 $self->{name} = 'World';
539 },
540 qw<Foo>;
541  
542 print $foo->hello . "\n";
543  
544 =cut
545  
546 0 ▶sub before {
Subroutine before: not called CC=4 SCAR=27.9
Pod documented
547 0 my ($method, $code, $class) = @_;
548 0 my $full;
549 0 _check_init($class);
550 0 ▶ $full = ref($class) ? ref($class) . "::${method}" : "${class}::${method}";
Branch: 1 branch
BranchTrueFalseTotal
ref $class ? : 0 0 0
551 0 my $new_code;
552 my $old_code;
553 0 ▶ die "Could not find $method in the hierarchy for $class\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $class->can($method) 0 0 0
554 if ! $class->can($method);
555
556 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
557  
558 0 _add_to_subs($full);
559 0 $old_code = \&{$full};
560 0 ▶ if (ref($class)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $class) { } 0 0 0
561 0 $Class::Monkey::Iter++;
562 0 my $package = ref($class) . '::Class::Monkey::' . $Class::Monkey::Iter;
563 0 @{$package . '::ISA'} = (ref($class));
564 0 $full = "${package}::${method}";
565  
566 0 ▶ *$method = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
567 0 $code->(@_);
568 0 $old_code->(@_);
569 0 };
570
571 0 *{$full} = \*$method;
572 0 bless $_[2], $package;
573 }
574 else {
575 0 ▶ *$method = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
576 0 $code->(@_);
577 0 $old_code->(@_);
578 0 };
579 0 *{$full} = \*$method;
580 }
581
582
583 }
584  
585 =head2 after
586  
587 Basically the same as C<before>, but appends the code specified to the END of the original
588  
589 =cut
590  
591 0 ▶sub after {
Subroutine after: not called CC=4 SCAR=27.8
Pod documented
592 0 my ($method, $code, $class) = @_;
593  
594 0 _check_init($class);
595 0 ▶ my $full = ref($class) ? ref($class) . "::${method}" : "${class}::${method}";
Branch: 1 branch
BranchTrueFalseTotal
ref $class ? : 0 0 0
596 0 my $new_code;
597 my $old_code;
598 0 ▶ die "Could not find $method in the hierarchy for $class\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $class->can($method) 0 0 0
599 if ! $class->can($method);
600  
601 0 $old_code = \&{$full};
602 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
603 0 _add_to_subs($full);
604 0 ▶ if (ref($class)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $class) { } 0 0 0
605 0 $Class::Monkey::Iter++;
606 0 my $package = ref($class) . '::Class::Monkey::' . $Class::Monkey::Iter;
607 0 @{$package . '::ISA'} = (ref($class));
608 0 $full = "${package}::${method}";
609  
610 0 ▶ *$method = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
611 0 $old_code->(@_);
612 0 $code->(@_);
613 0 };
614  
615 0 *{$full} = \*$method;
616 0 bless $_[2], $package;
617 }
618 else {
619 0 ▶ *$method = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
620 0 $old_code->(@_);
621 0 $code->(@_);
622 0 };
623  
624 0 *{$full} = \*$method;
625 }
626 }
627  
628 =head2 around
629  
630 Around gives the user a bit more control over the subroutine. When you create an around method the first argument will be the original method, the second is C<$self> and the third is any arguments passed to the original subroutine. In a away this allows you to control the flow of the entire subroutine.
631  
632 package MyFoo;
633  
634 sub greet {
635 my ($self, $name) = @_;
636  
637 print "Hello, $name!\n";
638 }
639  
640 1;
641  
642 # test.pl
643  
644 use Class::Monkey qw<MyFoo>;
645  
646 # only call greet if any arguments were passed to MyFoo->greet()
647 around 'greet' => sub {
648 my $method = shift;
649 my $self = shift;
650  
651 $self->$method(@_)
652 if @_;
653 },
654 qw<MyFoo>;
655  
656 =cut
657  
658 0 ▶sub around {
Subroutine around: not called CC=3 SCAR=22.1
Pod documented
659 0 my ($method, $code, $class) = @_;
660  
661 0 my $full = "$class\::$method";
662 0 ▶ die "Could not find $method in the hierarchy for $class\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $class->can($method) 0 0 0
663 if ! $class->can($method);
664  
665 0 my $old_code = \&{$full};
666 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
667 0 ▶ *$method = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
668 0 $code->($old_code, @_);
669 0 };
670  
671 0 _add_to_subs($full);
672 0 ▶ if (ref($class)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $class) { } 0 0 0
673 0 $Class::Monkey::Iter++;
674 0 my $package = ref($class) . '::Class::Monkey::' . $Class::Monkey::Iter;
675 0 @{$package . '::ISA'} = (ref($class));
676 0 *{"${package}::${method}"} = \*$method;
677 0 bless $_[2], $package;
678 }
679 else {
680 0 *{$full} = \*$method;
681 }
682 }
683  
684 =head2 unpatch
685  
686 Undoes any modifications made to patched methods, restoring it to its original state.
687  
688 override 'this' => sub {
689 print "Blah\n";
690 }, qw<FooClass>;
691
692 unpatch 'this', 'FooClass';
693  
694 =cut
695  
696 0 ▶sub unpatch {
Subroutine unpatch: not called CC=2 SCAR=13.0
Pod documented
697 0 my ($method, $class) = @_;
698  
699 0 my $sub = "$class\::$method";
700  
701 0 ▶ if (! exists $Class::Monkey::Subs->{$sub}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $Class::Monkey::Subs->{$sub}) 0 0 0
702 0 warn "Could not restore $method in $class because I have no recollection of it";
703 0 return 0;
704 }
705  
706 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
707 0 *{$sub} = $Class::Monkey::Subs->{$sub};
708 }
709  
710 =head1 AUTHOR
711  
712 Brad Haywood <brad@geeksware.net>
713  
714 =head1 LICENSE
715  
716 You may distribute this code under the same terms as Perl itself.
717  
718 =cut
719  
720 1 1;
↑ Summary