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:

  1. Checks the PERL_TRACE_CALLS environment variable is true. If it's false, it doesn't do the next two things.

  2. 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.

  3. 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 } }
use strict; use warnings; package Devel::TraceCalls; our $AUTHORITY = 'cpan:TOBYINK'; our $VERSION = '0.001'; use constant ACTIVE => $ENV{'PERL_TRACE_CALLS'}; BEGIN { eval q{ use match::simple (); use Carp (); use File::Spec (); use FindBin (); use Hook::AfterRuntime (); use JSON::PP (); use Sub::Util (); 1; } || die($@) if ACTIVE; }; our $JSON; our %CALL; $JSON = 'JSON::PP'->new->pretty(1)->canonical(1) if ACTIVE; sub import { my $me = shift; my $caller = caller; my (%opts) = @_; &Hook::AfterRuntime::after_runtime( sub { $me->setup_for($caller, %opts) }, ) if ACTIVE; } sub setup_for { my $me = shift; my ($caller, %opts) = @_; $opts{match} = sub { local $_ = shift; !/^_/ and /\p{Ll}/; } unless exists $opts{match}; no strict 'refs'; my @names = grep match::simple::match($_, $opts{match}), grep !/::$/, sort keys %{"$caller\::"}; $me->wrap_sub($caller, $_) for @names; } sub wrap_sub { my $me = shift; no strict 'refs'; no warnings 'redefine'; my ($package, $sub) = @_; ($package, $sub) = (/^(.+)::([^:]+)$/ =~ $package) if !defined $sub; my $code = \&{"$package\::$sub"}; my $subname = Sub::Util::subname($code); my $newcode = Sub::Util::set_prototype prototype($code), Sub::Util::set_subname $subname, sub { ++$CALL{$package}{$sub}; goto $code }; *{"$package\::$sub"} = $newcode; } END { if (ACTIVE) { my $map = $JSON->encode(\%CALL); my $outfile = 'File::Spec'->catfile( $FindBin::RealBin, $FindBin::RealScript . ".map", ); my $already = 0; if (-f $outfile) { my $slurped = do { local $/; my $fh; open($fh, '<', $outfile) ? <$fh> : undef; }; $already++ if $slurped eq $map; } if (!$already) { open my $outfh, '>', $outfile or Carp::croak("Cannot open $outfile for output: $!"); print {$outfh} $map or Carp::croak("Cannot write to $outfile: $!"); close $outfh or Carp::croak("Cannot close $outfile: $!"); } }; } 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.


In reply to Re^5: Fast coverage module or hack by tobyink
in thread Fast coverage module or hack by vsespb

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



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.