Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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


In reply to RFC: Sub::Prepend - Prepend code to named subroutines by lodin

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (4)
As of 2024-04-24 17:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found