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
my $old = \&foo;
*foo = sub { ...; goto &$old };
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 like
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;
}
and now it's pretty ugly. Putting it in a subroutine makes it a lot nicer:
prepend(foo => sub { ... });
The prototype is kept, unless explicitly overridden:
prepend(foo => sub ($) { ... });
prepend(foo => sub { ... }, { prototype => '$' });
Prototype mismatch warnings are propagated. An explicit prototype of
undef "removes" any previous 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
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.