use Sub::Prepend 'prepend';
sub foo ($) {
print "Foo executed with \@_ = (@_).\n";
}
BEGIN {
prepend foo => sub {
# This is called before foo executes.
print "Foo was called with \@_ = (@_).\n";
};
}
my @bar = qw/ foo bar baz /;
foo(@bar); # The prototype is preserved!
__END__
Foo was called with @_ = (3).
Foo executed with @_ = (3).
####
my $old = \&foo;
*foo = sub { ...; goto &$old };
##
##
use Scalar::Util 'set_prototype';
my $name = ...;
my $old;
my $new = sub { ...; goto &$old };
{
no strict 'refs';
$old = \&$name;
set_prototype(\&$new, prototype $old);
no warnings 'redefine';
*$name = $new;
}
##
##
prepend(foo => sub { ... });
##
##
prepend(foo => sub ($) { ... });
prepend(foo => sub { ... }, { prototype => '$' });