Monkey::Patch::Action

Wrap/add/replace/delete subs from other package (with restore)
Download

Monkey::Patch::Action Ranking & Summary

Advertisement

  • Rating:
  • License:
  • Perl Artistic License
  • Price:
  • FREE
  • Publisher Name:
  • Steven Haryanto
  • Publisher web site:
  • http://search.cpan.org/~sharyanto/

Monkey::Patch::Action Tags


Monkey::Patch::Action Description

Monkey-patching is the act of modifying a package at runtime: adding a subroutine/method, replacing/deleting/wrapping another, etc. Perl makes it easy to do that, for example: # add a subroutine *{"Target::sub1"} = sub { ... }; # another way, can be done from any file package Target; sub sub2 { ... } # delete a subroutine undef *{"Target::sub3"};Monkey::Patch::Action is a Perl module that makes things even easier by helping you apply a stack of patches and unapply them later in flexible order.SYNOPSIS use Monkey::Patch::Action qw(patch_package); package Foo; sub sub1 { say "Foo's sub1" } sub sub2 { say "Foo's sub2, args=", join(",", @_) } sub meth1 { my $self = shift; say "Foo's meth1" } package Bar; our @ISA = qw(Foo); package main; my $h; # handle object my $foo = Foo->new; my $bar = Bar->new; # replacing a subroutine $h = patch_package('Foo', 'sub1', 'replace', sub { "qux" }); Foo::sub1(); # says "qux" undef $h; Foo::sub1(); # says "Foo's sub1" # adding a subroutine $h = patch_package('Foo', 'sub3', 'add', sub { "qux" }); Foo::sub3(); # says "qux" undef $h; Foo::sub3(); # dies # deleting a subroutine $h = patch_package('Foo', 'sub2', 'delete'); Foo::sub2(); # dies undef $h; Foo::sub2(); # says "Foo's sub2, args=" # wrapping a subroutine $h = patch_package('Foo', 'sub2', 'wrap', sub { my $ctx = shift; say "wrapping $ctx->{package}::$ctx->{subname}"; $ctx->{orig}->(@_); } ); Foo::sub2(1,2,3); # says "wrapping Foo::sub2" then "Foo's sub2, args=1,2,3" undef $h; Foo::sub2(1,2,3); # says "Foo's sub2, args=1,2,3" # stacking patches (note: can actually be unapplied in random order) my ($h2, $h3); $h = patch_package('Foo', 'sub1', 'replace', sub { "qux" }); Foo::sub1(); # says "qux" $h2 = patch_package('Foo', 'sub1', 'delete'); Foo::sub1(); # dies $h3 = patch_package('Foo', 'sub1', 'replace', sub { "quux" }); Foo::sub1(); # says "quux" undef $h3; Foo::sub1(); # dies undef $h2; Foo::sub1(); # says "qux" undef $h; Foo::sub1(); # says "Foo's sub1"Product's homepage


Monkey::Patch::Action Related Software