I will probably put this on CPAN soonish, but here's the gist of it. Below is the contents of "Devel::TraceCalls". Most of its dependencies are in core, except for Hook::AfterRuntime and match::simple which are on CPAN.
You need to use Devel::TraceCalls in each module.
Devel::TraceCalls does three things:
Checks the PERL_TRACE_CALLS environment variable is true. If it's false, it doesn't do the next two things.
Wraps every sub in the caller module with tracking code to count how many times the sub has been called. This wrapper uses goto to call the original sub, so it should be invisible to caller. It does wrap imported subs, so if you don't want them wrapped, clean then with namespace::autoclean. It does wrap generated subs, like Moose attributes. It doesn't wrap inherited subs. (Though of course the class you're inheriting from can use Devel::TraceCalls). Note that Moose constructors are inherited if your class is mutable and generated if your class is immutable.
In an END { ... } block, it uses FindBin to find the name of the test script, and creates a JSON file with the same name but a ".t.map" file extension. In this JSON file, it dumps a hashref where the keys are the modules which have been called.
The end result is that if you run:
PERL_TRACE_CALLS=1 prove -l t/sometest.t
You'll get a file called "t/sometest.t.map" containing something like this:
{
"Local::Example" : {
"quux" : 1
},
"Local::Example::Module1" : {
"bar" : 1
},
"Local::Example::Module2" : {
"bar" : 1,
"foo" : 1
}
}
There are some deliberate optimizations like loading dependencies in a stringy eval, so even if you do use Devel::TraceCalls in production code, it shouldn't waste time or memory.
|