I've written a module currently called Sub::Prepend that I'm considering for CPAN release.
Update: Uploaded to CPAN, Sub::Prepend.
The synopsis from the POD
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).
Description / Motivation
Occasionally I want to prepend some code to subroutines, for instance to aid debugging. So I've many times repeated the code
That's OK as long as the subroutine name is written explicitly (and foo doesn't have a prototype I'd have to remember). Otherwise I'd need something likemy $old = \&foo; *foo = sub { ...; goto &$old };
and now it's pretty ugly. Putting it in a subroutine makes it a lot nicer: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; }
The prototype is kept, unless explicitly overridden:prepend(foo => sub { ... });
Prototype mismatch warnings are propagated. An explicit prototype of undef "removes" any previous prototype.prepend(foo => sub ($) { ... }); prepend(foo => sub { ... }, { prototype => '$' });
Other modules
Of course, this has been done before. There's at least three modules available at CPAN that does this: Hook::WrapSub, Hook::PrePostCall, and Hook::LexWrap. Their common issue is that they try to also append code to subroutines. This requires them to hack around caller as they can't use goto &NAME and Perl doesn't have an uplevel function. Unfortunately, that hack is fundamentally broken as it won't work if the subroutine is compiled before the wrapper module is loaded. Even if you don't use the append functionality provided by those modules you get an overloaded caller that may introduce subtle bugs, like when another module tries to get extra information via the DB interface, or when another module also wants to override caller. Also, none of those modules take care of prototypes, althought that would be easy to add. Some also mess with caller context noticable by users of Want, although this too could be avoided for pure prepending operations.
Appending code is fundamentally flawed, and the caller overloading in particular is what I think motivates this module. To my knowledge, Sub::Prepend is fully transparent to the subroutine it wraps. Of course, it's noticable to code that references the subroutine, but that's a different story altogether.
Considerations
- Have I missed any existing module that renders Sub::Prepend unnecessary?
- The name. The other modules are called Hook::*, but personally I think there's too many types of modules in Hook::*.
- The interface. Currently it's only an exported subroutine called prepend. Is that a good name? Does the argument structure feel natural?
- Any issues I've overlooked?
- Last but not least: would you like to see this on CPAN?
Thanks in advance,
lodin
|
---|
Back to
Meditations