1Monkey::Patch(3)      User Contributed Perl Documentation     Monkey::Patch(3)
2
3
4

NAME

6       Monkey::Patch - Scoped monkeypatching (you can at least play nice)
7

VERSION

9       version 0.03
10

SYNOPSIS

12           use Monkey::Patch qw(:all);
13
14           sub some_subroutine {
15               my $pkg = patch_class 'Some::Class' => 'something' => sub {
16                   my $original = shift;
17                   say "Whee!";
18                   $original->(@_);
19               };
20               Some::Class->something(); # says Whee! and does whatever
21               undef $pkg;
22               Some::Class->something(); # no longer says Whee!
23
24               my $obj = Some::Class->new;
25               my $obj2 = Some::Class->new;
26
27               my $whoah = patch_object $obj, 'twiddle' => sub {
28                   my $original = shift;
29                   my $self     = shift;
30                   say "Whoah!";
31                   $self->$original(@_);
32               };
33
34               $obj->twiddle();  # says Whoah!
35               $obj2->twiddle(); # doesn't
36               $obj->twiddle()   # still does
37               undef $whoah;
38               $obj->twiddle();  # but not any more
39

SUBROUTINES

41       The following subroutines are available (either individually or via
42       :all)
43
44   patch_package (package, subname, code)
45       Wraps "package"'s subroutine named <subname> with your <code>.  Your
46       code receives the original subroutine as its first argument, followed
47       by any arguments the subroutine would have normally gotten.  You can
48       always call the subroutine ref your received; if there was no
49       subroutine by that name, the coderef will simply do nothing.
50
51   patch_class (class, methodname, code)
52       Just like "patch_package", except that the @ISA chain is walked when
53       you try to call the original subroutine if there wasn't any subroutine
54       by that name in the package.
55
56   patch_object (object, methodname, code)
57       Just like "patch_class", except that your code will only get called on
58       the object you pass, not the entire class.
59

HANDLES

61       All the "patch" functions return a handle object.  As soon as you lose
62       the value of the handle (by calling in void context, assigning over the
63       variable, undeffing the variable, letting it go out of scope, etc), the
64       monkey patch is unwrapped.  You can stack monkeypatches and let go of
65       the handles in any order; they obey a stack discipline, and the most
66       recent valid monkeypatch will always be called.  Calling the "original"
67       argument to your wrapper routine will always call the next-most-recent
68       monkeypatched version (or, the original subroutine, of course).
69

BUGS

71       This magic is only faintly black, but mucking around with the symbol
72       table is not for the faint of heart.  Help make this module better by
73       reporting any strange behavior that you see!
74
75
76
77perl v5.32.0                      2020-07-28                  Monkey::Patch(3)
Impressum