File Coverage

blib/lib/Monkey/Patch/Action.pm
Criterion Covered Total %
statement 34 34 100.0
branch 22 24 91.6
condition n/a
subroutine 6 6 100.0
pod 1 1 100.0
total 63 65 96.9


line stmt bran cond sub pod time code
1             package Monkey::Patch::Action;
2              
3 1     1   27559 use 5.010;
  1         4  
  1         45  
4 1     1   6 use warnings;
  1         1  
  1         31  
5 1     1   5 use strict;
  1         2  
  1         78  
6              
7             our $VERSION = '0.04'; # VERSION
8              
9 1     1   1271 use Monkey::Patch::Action::Handle;
  1         3  
  1         1811  
10              
11 1     1   8 use Exporter qw(import);
  1         2  
  1         382  
12             our @EXPORT_OK = qw(patch_package);
13             our %EXPORT_TAGS = (all => \@EXPORT_OK);
14              
15             sub patch_package {
16 22     22 1 108620 my ($package, $subname, $action, $code, @extra) = @_;
17              
18 22 50       95 die "Please specify action" unless $action;
19 22 100       58 if ($action eq 'delete') {
20 5 100       26 die "code not needed for 'delete' action" if $code;
21             } else {
22 17 50       46 die "Please specify code" unless $code;
23             }
24              
25 21         50 my $name = "$package\::$subname";
26 21         31 my $type;
27 21 100       101 if ($action eq 'add') {
    100          
    100          
    100          
    100          
28 4 100       37 die "Adding $name: must not already exist" if defined(&$name);
29 3         6 $type = 'sub';
30             } elsif ($action eq 'replace') {
31 2 100       23 die "Replacing $name: must already exist" unless defined(&$name);
32 1         3 $type = 'sub';
33             } elsif ($action eq 'add_or_replace') {
34 2         3 $type = 'sub';
35             } elsif ($action eq 'wrap') {
36 8 100       41 die "Wrapping $name: must already exist" unless defined(&$name);
37 7         13 $type = 'wrap';
38             } elsif ($action eq 'delete') {
39 4         8 $type = 'delete';
40             } else {
41 1         15 die "Unknown action '$action', please use either ".
42             "wrap/add/replace/add_or_replace/delete";
43             }
44              
45 17         167 my @caller = caller(0);
46              
47 17         127 Monkey::Patch::Action::Handle->new(
48             package => $package,
49             subname => $subname,
50             extra => \@extra,
51             patcher => \@caller,
52             code => $code,
53              
54             -type => $type,
55             );
56             }
57              
58             1;
59             # ABSTRACT: Wrap/add/replace/delete subs from other package (with restore)
60              
61              
62             __END__
63             =pod
64              
65             =head1 NAME
66              
67             Monkey::Patch::Action - Wrap/add/replace/delete subs from other package (with restore)
68              
69             =head1 VERSION
70              
71             version 0.04
72              
73             =head1 SYNOPSIS
74              
75             use Monkey::Patch::Action qw(patch_package);
76              
77             package Foo;
78             sub sub1 { say "Foo's sub1" }
79             sub sub2 { say "Foo's sub2, args=", join(",", @_) }
80             sub meth1 { my $self = shift; say "Foo's meth1" }
81              
82             package Bar;
83             our @ISA = qw(Foo);
84              
85             package main;
86             my $h; # handle object
87             my $foo = Foo->new;
88             my $bar = Bar->new;
89              
90             # replacing a subroutine
91             $h = patch_package('Foo', 'sub1', 'replace', sub { "qux" });
92             Foo::sub1(); # says "qux"
93             undef $h;
94             Foo::sub1(); # says "Foo's sub1"
95              
96             # adding a subroutine
97             $h = patch_package('Foo', 'sub3', 'add', sub { "qux" });
98             Foo::sub3(); # says "qux"
99             undef $h;
100             Foo::sub3(); # dies
101              
102             # deleting a subroutine
103             $h = patch_package('Foo', 'sub2', 'delete');
104             Foo::sub2(); # dies
105             undef $h;
106             Foo::sub2(); # says "Foo's sub2, args="
107              
108             # wrapping a subroutine
109             $h = patch_package('Foo', 'sub2', 'wrap',
110             sub {
111             my $ctx = shift;
112             say "wrapping $ctx->{package}::$ctx->{subname}";
113             $ctx->{orig}->(@_);
114             }
115             );
116             Foo::sub2(1,2,3); # says "wrapping Foo::sub2" then "Foo's sub2, args=1,2,3"
117             undef $h;
118             Foo::sub2(1,2,3); # says "Foo's sub2, args=1,2,3"
119              
120             # stacking patches (note: can actually be unapplied in random order)
121             my ($h2, $h3);
122             $h = patch_package('Foo', 'sub1', 'replace', sub { "qux" });
123             Foo::sub1(); # says "qux"
124             $h2 = patch_package('Foo', 'sub1', 'delete');
125             Foo::sub1(); # dies
126             $h3 = patch_package('Foo', 'sub1', 'replace', sub { "quux" });
127             Foo::sub1(); # says "quux"
128             undef $h3;
129             Foo::sub1(); # dies
130             undef $h2;
131             Foo::sub1(); # says "qux"
132             undef $h;
133             Foo::sub1(); # says "Foo's sub1"
134              
135             =head1 DESCRIPTION
136              
137             Monkey-patching is the act of modifying a package at runtime: adding a
138             subroutine/method, replacing/deleting/wrapping another, etc. Perl makes it easy
139             to do that, for example:
140              
141             # add a subroutine
142             *{"Target::sub1"} = sub { ... };
143              
144             # another way, can be done from any file
145             package Target;
146             sub sub2 { ... }
147              
148             # delete a subroutine
149             undef *{"Target::sub3"};
150              
151             This module makes things even easier by helping you apply a stack of patches and
152             unapply them later in flexible order.
153              
154             =head1 FUNCTIONS
155              
156             =head2 patch_package($package, $subname, $action, $code, @extra) => HANDLE
157              
158             Patch C<$package>'s subroutine named C<$subname>. C<$action> is either:
159              
160             =over 4
161              
162             =item * C<wrap>
163              
164             C<$subname> must already exist. C<code> is required.
165              
166             Your code receives a context hash as its first argument, followed by any
167             arguments the subroutine would have normally gotten. Context hash contains:
168             C<orig> (the original subroutine that is being wrapped), C<subname>, C<package>,
169             C<extra>.
170              
171             =item * C<add>
172              
173             C<subname> must not already exist. C<code> is required.
174              
175             =item * C<replace>
176              
177             C<subname> must already exist. C<code> is required.
178              
179             =item * C<add_or_replace>
180              
181             C<code> is required.
182              
183             =item * C<delete>
184              
185             C<code> is not needed.
186              
187             =back
188              
189             Die on error.
190              
191             Function returns a handle object. As soon as you lose the value of the handle
192             (by calling in void context, assigning over the variable, undeffing the
193             variable, letting it go out of scope, etc), the patch is unapplied.
194              
195             Patches can be unapplied in random order, but unapplying a patch where the next
196             patch is a wrapper can lead to an error. Example: first patch (P1) adds a
197             subroutine and second patch (P2) wraps it. If P1 is unapplied before P2, the
198             subroutine is now no longer there, and P2 no longer works. Unapplying P1 after
199             P2 works, of course.
200              
201             =head1 FAQ
202              
203             =head2 Differences with Monkey::Patch?
204              
205             This module is based on the wonderful L<Monkey::Patch> by Paul Driver. The
206             differences are:
207              
208             =over 4
209              
210             =item *
211              
212             This module adds the ability to add/replace/delete subroutines instead of just
213             wrapping them.
214              
215             =item *
216              
217             Interface to patch_package() is slightly different (see previous item for the
218             cause).
219              
220             =item *
221              
222             Using this module, the wrapper receives a context hash instead of just the
223             original subroutine.
224              
225             =item *
226              
227             Monkey::Patch adds convenience for patching classes and objects. To keep things
228             simple, no such convenience is currently provided by this module.
229             C<patch_package()> *can* patch classes and objects as well (see the next FAQ
230             entry).
231              
232             =back
233              
234             =head2 How to patch classes and objects?
235              
236             Patching a class is basically the same as patching any other package, since Perl
237             implements a class with a package. One thing to note is that to call a parent's
238             method inside your wrapper code, instead of:
239              
240             $self->SUPER::methname(...)
241              
242             you need to do something like:
243              
244             use SUPER;
245             SUPER::find_parent(ref($self), 'methname')->methname(...)
246              
247             Patching an object is also basically patching a class/package, because Perl does
248             not have per-object method like Ruby. But if you just want to provide a modified
249             behavior for a certain object only, you can do something like:
250              
251             patch_package($package, $methname, 'wrap',
252             sub {
253             my $ctx = shift;
254             my $self = shift;
255              
256             my $obj = $ctx->{extra}[0];
257             no warnings 'numeric';
258             if ($obj == $self) {
259             # do stuff
260             }
261             $ctx->{orig}->(@_);
262             }, $obj);
263              
264             =head1 SEE ALSO
265              
266             L<Monkey::Patch>
267              
268             =head1 AUTHOR
269              
270             Steven Haryanto <stevenharyanto@gmail.com>
271              
272             =head1 COPYRIGHT AND LICENSE
273              
274             This software is copyright (c) 2012 by Steven Haryanto.
275              
276             This is free software; you can redistribute it and/or modify it under
277             the same terms as the Perl 5 programming language system itself.
278              
279             =cut
280