| 1 | package Attribute::RecordCallers; | ||||||||||||
| 2 | |||||||||||||
| 3 | 7 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 4 | 7 | ▶ | use warnings; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 5 | 7 | ▶ | use Attribute::Handlers; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 6 | 7 | ▶ | use Carp qw(carp); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 7 | 7 | ▶ | use Time::HiRes qw(time); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 8 | 7 | ▶ | use Scalar::Util qw(set_prototype); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 9 | |||||||||||||
| 10 | 7 | our $VERSION = '0.02'; | |||||||||||
| 11 | |||||||||||||
| 12 | 7 | our @CARP_NOT = qw(Attribute::Handlers); | |||||||||||
| 13 | # arguably a bug in Carp, but Attribute::Handlers does | ||||||||||||
| 14 | # nasty things with UNIVERSAL | ||||||||||||
| 15 | 7 | @Attribute::Handlers::CARP_NOT = qw(attributes); | |||||||||||
| 16 | |||||||||||||
| 17 | 7 | our %callers; | |||||||||||
| 18 | |||||||||||||
| 19 | 8 | ▶ | sub UNIVERSAL::RecordCallers :ATTR(CODE,BEGIN) { | ||||||||||
|
Subroutine
RecordCallers: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||
| 20 | 8 | my ($pkg, $glob, $referent) = @_; | |||||||||||
| 21 | 7 | ▶ | no strict 'refs'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 22 | 7 | ▶ | no warnings qw(redefine once prototype); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 23 | 8 | my $subname = *{$glob}{NAME}; | |||||||||||
| 24 | 8 | ▶ | if ($subname eq 'ANON') { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 25 | 2 | carp "Ignoring RecordCallers attribute on anonymous subroutine"; | |||||||||||
| 26 | 2 | return; | |||||||||||
| 27 | } | ||||||||||||
| 28 | 6 | $subname = $pkg . '::' . $subname; | |||||||||||
| 29 | 12 | ▶ | *$subname = sub { | ||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||
| 30 | 12 | ▶ | push @{ $callers{$subname} ||= [] }, [ caller, time ]; | ||||||||||
|
Condition100%2/2
$Attribute::RecordCallers::callers{$subname} ||= []
MC/DC100%1/1
$Attribute::RecordCallers::callers{$subname} ||= []
$Attribute::RecordCallers::callers{$subname}
Truth table100%2/2
A
A= $Attribute::RecordCallers::callers{$subname}
| |||||||||||||
| 31 | 12 | goto &$referent; | |||||||||||
| 32 | 6 | }; | |||||||||||
| 33 | 6 | my $proto = prototype $referent; | |||||||||||
| 34 | 6 | ▶ | set_prototype(\&$subname, $proto) if defined $proto; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 35 | 7 | ▶ | } | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 36 | |||||||||||||
| 37 | 3 | ▶ | sub clear { | ||||||||||
|
Subroutine
clear: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 38 | 3 | %callers = (); | |||||||||||
| 39 | } | ||||||||||||
| 40 | |||||||||||||
| 41 | 1 | ▶ | sub walk { | ||||||||||
|
Subroutine
walk: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||
| 42 | 1 | my $coderef = shift; | |||||||||||
| 43 | 1 | $coderef->($_, $callers{$_}) for sort keys %callers; | |||||||||||
| 44 | } | ||||||||||||
| 45 | |||||||||||||
| 46 | 7 | 1; | |||||||||||
| 47 | |||||||||||||
| 48 | =head1 NAME | ||||||||||||
| 49 | |||||||||||||
| 50 | Attribute::RecordCallers - keep a record of who called a subroutine | ||||||||||||
| 51 | |||||||||||||
| 52 | =head1 SYNOPSIS | ||||||||||||
| 53 | |||||||||||||
| 54 | use Attribute::RecordCallers; | ||||||||||||
| 55 | sub call_me_and_i_ll_tell_you : RecordCallers { ... } | ||||||||||||
| 56 | ... | ||||||||||||
| 57 | END { | ||||||||||||
| 58 | use Data::Dumper; | ||||||||||||
| 59 | print Dumper \%Attribute::RecordCallers::callers; | ||||||||||||
| 60 | } | ||||||||||||
| 61 | |||||||||||||
| 62 | =head1 DESCRIPTION | ||||||||||||
| 63 | |||||||||||||
| 64 | This module defines a function attribute that will trigger collection of | ||||||||||||
| 65 | callers for the designated functions. | ||||||||||||
| 66 | |||||||||||||
| 67 | Each time a function with the C<:RecordCallers> attribute is run, a global | ||||||||||||
| 68 | hash C<%Attribute::RecordCallers::caller> is populated with caller information. | ||||||||||||
| 69 | The keys in the hash are the function names, and the elements are arrayrefs | ||||||||||||
| 70 | containing lists of quadruplets: | ||||||||||||
| 71 | |||||||||||||
| 72 | [ $package, $filename, $line, $timestamp ] | ||||||||||||
| 73 | |||||||||||||
| 74 | The timestamp is obtained via C<Time::HiRes>. | ||||||||||||
| 75 | |||||||||||||
| 76 | =head1 FUNCTIONS | ||||||||||||
| 77 | |||||||||||||
| 78 | =over 4 | ||||||||||||
| 79 | |||||||||||||
| 80 | =item clear() | ||||||||||||
| 81 | |||||||||||||
| 82 | (not exported) This function will clear the C<%callers> global hash. | ||||||||||||
| 83 | |||||||||||||
| 84 | =item walk(sub { ... }) | ||||||||||||
| 85 | |||||||||||||
| 86 | (not exported) Invokes the subroutine passed as argument once for each | ||||||||||||
| 87 | item in the C<%callers> hash. The arguments passed to it are the | ||||||||||||
| 88 | recorded subroutine name, and the arrayref of arrayrefs recording | ||||||||||||
| 89 | all the calls. | ||||||||||||
| 90 | |||||||||||||
| 91 | =back | ||||||||||||
| 92 | |||||||||||||
| 93 | =head1 LIMITATIONS | ||||||||||||
| 94 | |||||||||||||
| 95 | You cannot use the C<:RecordCaller> attribute on anonymous or lexical | ||||||||||||
| 96 | subroutines, or or subroutines with any other attribute (such as | ||||||||||||
| 97 | C<:lvalue>). | ||||||||||||
| 98 | |||||||||||||
| 99 | With perls older than version 5.16.0, setting the C<:RecordCallers> | ||||||||||||
| 100 | attribute will remove the prototype of any subroutine. | ||||||||||||
| 101 | |||||||||||||
| 102 | =head1 LICENSE | ||||||||||||
| 103 | |||||||||||||
| 104 | (c) Rafael Garcia-Suarez (rgs at consttype dot org) 2014 | ||||||||||||
| 105 | |||||||||||||
| 106 | This program is free software; you may redistribute it and/or modify it under | ||||||||||||
| 107 | the same terms as Perl itself. | ||||||||||||
| 108 | |||||||||||||
| 109 | A git repository for the sources is at L<https://github.com/rgs/Attribute-RecordCallers>. | ||||||||||||
| 110 | |||||||||||||
| 111 | =cut | ||||||||||||