blib/lib/Subs/Trace.pm

filter: Statementstmt 100% 25 / 25 Branchbran 100% 2 / 2 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 7 / 7 Podpod n/a totaltotal 100% 34 / 34 CC 3 SCAR 5.1
CC
3
Cov
100%
import
3.0
BEGIN
1.0
BEGIN
1.0
CRAP 1.7
↑ Summary
1 package Subs::Trace;
2  
3 2 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 =head1 NAME
8  
9 Subs::Trace - Trace all calls in a package.
10  
11 =cut
12  
13 2 our $VERSION = '0.10';
14  
15 =head1 SYNOPSIS
16  
17 Similar to
18  
19 around 'my_function' => sub {
20 my $original = shift;
21 print "--> my_function\n";
22 $original->(@_);
23 };
24  
25 But for ALL functions in a class.
26  
27 package MyClass;
28  
29 sub Func1 { ... }
30 sub Func2 { ... }
31 sub Func3 { ... }
32  
33 use Subs::Trace;
34  
35 Func1();
36 # Prints:
37 # --> MyClass::Func1
38  
39 =head1 DESCRIPTION
40  
41 This module updates all methods/functions in a class to
42 also print a message when invoked.
43  
44 (This is a more of a proof-of-concept than useful!)
45  
46 =head1 SUBROUTINES/METHODS
47  
48 =head2 import
49  
50 NOTE: This must be put at the very bottom of a class.
51  
52 Also, some reason C<INIT{ ... }> is not being called with Moose.
53  
54 Will attach hooks to all functions defined BEFORE this import call.
55  
56 =cut
57  
58 2 ▶sub import {
Subroutine import: called CC=3 SCAR=11.0
59 2 my $pkg = caller();
60  
61 # print "pkg=$pkg\n";
62  
63 2 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
64 2 ▶ no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
65  
66 2 for my $func ( sort keys %{"${pkg}::"} ) {
67  
68 # print "func=$func\n";
69  
70 8 my $stash = "$pkg\::$func";
71 8 my $code = *$stash{CODE};
72 8 ▶ next if not $code;
Branch: 1 branch
BranchTrueFalseTotal
unless $code 2 6 8
73  
74 # print " Updated $stash\n";
75  
76 5 ▶ *$stash = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
77 5 print "--> $pkg\::$func\n";
78 5 &$code;
79 }
80 6 }
81 }
82  
83 =head1 AUTHOR
84  
85 Tim Potapov, C<< <tim.potapov at gmail.com> >>
86  
87 =head1 BUGS
88  
89 Please report any bugs or feature requests to L<https://github.com/poti1/subs-trace/issues>.
90  
91 =head1 SUPPORT
92  
93 You can find documentation for this module with the perldoc command.
94  
95 perldoc Subs::Trace
96  
97 =head1 ACKNOWLEDGEMENTS
98  
99 TBD
100  
101 =head1 LICENSE AND COPYRIGHT
102  
103 This software is Copyright (c) 2022 by Tim Potapov.
104  
105 This is free software, licensed under:
106  
107 The Artistic License 2.0 (GPL Compatible)
108  
109  
110 =cut
111  
112 2 1; # End of Subs::Trace
↑ Summary