Why not adopt me?
NAME
ex::monkeypatched - Experimental API for safe monkey-patching
SYNOPSIS
use ex::monkeypatched 'Third::Party::Class' => (
clunk => sub { ... },
eth => sub { ... },
);
BACKGROUND
The term "monkey patching" describes injecting additional methods into a class whose implementation you don't control. If done without care, this is dangerous; the problematic case arises when:
You add a method to a class;
A newer version of the monkey-patched class adds another method of the same name
And uses that new method in some other part of its own implementation.
ex::monkeypatched
lets you do this sort of monkey-patching safely: before it injects a method into the target class, it checks whether the class already has a method of the same name. If it finds such a method, it throws an exception (at compile-time with respect to the code that does the injection).
See http://aaroncrane.co.uk/talks/monkey_patching_subclassing/ for more details.
DESCRIPTION
ex::monkeypatched
injects methods when you use
it. Your use
call should supply the name of a class to patch, and a hash from method names to code references implementing those methods. The class to be patched will be loaded automatically before any patching is done (thus ensuring that all its base classes are also loaded).
Alternatively, you can inject methods after a class has already been loaded, using the inject
method:
use ex::monkeypatched;
ex::monkeypatched->inject('Third::Party::Class' => (
clunk => sub { ... },
eth => sub { ... },
);
Calling inject
like this does not load the class in question, so ex::monkeypatched
is unable to guarantee that all the target class's methods have been loaded at the point the new methods are injected.
The ex::
prefix on the name of this module indicates that its API is still considered experimental. However, the underlying code has been in use in production for an extended period, and seems to be reliable.
CAVEATS
If the class you're monkeying around in uses AUTOLOAD
to implement some of its methods, and doesn't also implement its own can
method to accurately report which method names are autoloaded, ex::monkeypatched
will incorrectly assume that an autoloaded method does not exist. The solution is to fix the broken class; implementing AUTOLOAD
but not can
is always an error.
AUTHOR
Aaron Crane <arc@cpan.org>
LICENCE
This library is free software; you can redistribute it and/or modify it under the terms of either the GNU General Public License version 2 or, at your option, the Artistic License.