develooper Front page | perl.perl5.porters | Postings from February 2014

Hooking require before and after

Thread Next
February 26, 2014 19:32
Hooking require before and after
Message ID:
Recently I have been looking into some things which would be made MUCH
easier if I could "hook" require such that a sub of my definition was
called before a require executes, and a sub of my definition was
called after a require executes.

So for instance image we had a variable $^REQUIRE_HOOK_BEFORE and a

I could then do something like  this:

use Time::HiRes qw(time);

$^REQUIRE_HOOK_BEFORE= sub { printf "before require %s at %.2f", $_[0], time };
$^REQUIRE_HOOK_AFTER= sub {print "after require %s at %.2f", $_[0], time };

Which I could then use to time how long each require statement
(including use statements) took. My actual use case is to measure both
time and memory footprint to identify modules which consume a lot of
memory during startup. (Because for instance they build a reference
data structure from a query at compile time.)

I looked into using the source filter hooks, which beside being both
awful and powerful ways to make Perl segfault, does not seem to do
what I want. There is no way to get a callback when the code inside of
the module has finished executing.

So I have two questions, a) is this possible somehow already? b) if
not, then does it seem sufficiently generally useful that it would be
welcomed in core?


perl -Mre=debug -e "/just|another|perl|hacker/"

Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About