my $package_method = 'Foo::Bar::baz';
my $orig = \&($package_method};
no warnings;
*{$package_method} = sub { pre(); $orig->(@_); post() };
You can also do this:
package Foo::Bar;
sub baz { print "I am the original!\n" }
package main;
sub Foo::Bar::baz { print "I am the new baz!\n" }
Foo::Bar::baz();
Or you can toss in your own definition between a couple package lines anywhere you want. You can also build up a string like the above at run time and wrap it in an eval whenever you're ready.
Note that these various methods can have subtle differences when it comes to caller, and maybe some other stuff. |