gitpan / sub-monkey Goto Github PK
View Code? Open in Web Editor NEWRead-only release history for Sub-Monkey
Home Page: http://metacpan.org/release/Sub-Monkey
Read-only release history for Sub-Monkey
Home Page: http://metacpan.org/release/Sub-Monkey
NAME Sub::Monkey - Dynamically and neatly monkey patch a module DESCRIPTION In some cases, rare cases, you may need to temporarily patch a module on-the-go. Sub::Monkey can help you achieve this by providing a set of methods to create, override and add hook modifiers, similar to Moose, but can apply them to remote modules (Not the current one). This type of monkey patching is reasonably safe because you can plainly see what changes are being made to what modules. Obviously monkey patching isn't always the best alternative, but sometimes you may have no other choice. Sub::Monkey also provides the ability to undo any patching you made with "unpatch". SYNOPSIS use Sub::Monkey qw<Some::Package>; method 'needThisMethod' => sub { ... }, qw<Some::Package>; We just created a brand new method in the Some::Package class. If you attempt to override an existing method using "method", then Sub::Monkey will raise an error, because really you should be using "override" instead. Remember, to patch a module with Sub::Monkey, you need to explicitly tell it you want to modify a class by importing it when you "use Sub::Monkey". To do this for multiple modules just add them all into an array. use Sub::Monkey qw<Some::Package Foo::Bar Another::One>; METHODS instance Patch an instance method instead of an entire class # Pig.pm package Pig; sub new { return bless {}, shift; } sub says { print "Oink!\n"; } # test.pl package main; use Sub::Monkey qw<Pig>; my $pig = Pig->new; my $pig2 = Pig->new; instance 'says' => sub { print "Meow\n"; }, $pig2; # only $pig2 will have its says method overridden original If you want to run the original version of a patched method, but not unpatch it right away you can use "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. after 'someMethod' => sub { print "Blah\n" }, qw<Foo>; original('Foo', 'someMethod', qw<these are my args>); OR if you prefer, you can just call Sub::Monkey::PatchedClassName::method->(@args) Sub::Monkey::Foo->someMethod('these', 'are', 'my', 'args); override Overrides an already existing method. If the target method doesn't exist then Sub::Monkey will throw an error. override 'foo' => sub { return "foo bar"; }, qw<Some::Module>; method 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. method 'active_customers' => sub { my $self = shift; return $self->search({ status => 'active' }); }, qw<Schema::ResultSet::Customer>; before Simply adds code to the target method before the original code is ran # Foo.pm package Foo; sub new { return bless {}, __PACKAGE__; } sub hello { print "Hello, $self->{name}; } 1; # test.pl use Sub::Monkey qw<Foo>; my $foo = Foo->new; before 'hello' => { my $self = shift; $self->{name} = 'World'; }, qw<Foo>; print $foo->hello . "\n"; after Basically the same as "before", but appends the code specified to the END of the original around 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 $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. package MyFoo; sub greet { my ($self, $name) = @_; print "Hello, $name!\n"; } 1; # test.pl use Sub::Monkey qw<MyFoo>; # only call greet if any arguments were passed to MyFoo->greet() around 'greet' => sub { my $method = shift; my $self = shift; $self->$method(@_) if @_; }, qw<MyFoo>; unpatch Undoes any modifications made to patched methods, restoring it to its original state. override 'this' => sub { print "Blah\n"; }, qw<FooClass>; unpatch 'this' => 'FooClass'; AUTHOR Brad Haywood <[email protected]> LICENSE You may distribute this code under the same terms as Perl itself. POD ERRORS Hey! The above document had some coding errors, which are explained below: Around line 15: Deleting unknown formatting code M<>
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.