File Coverage

blib/lib/Sub/WrapPackages/CallTree.pm
Criterion Covered Total %
statement 15 15 100.0
branch n/a
condition n/a
subroutine 6 6 100.0
pod n/a
total 21 21 100.0


line stmt bran cond sub pod time code
1             package Sub::WrapPackages::CallTree;
2              
3 1     1   77602 use strict;
  1         9  
  1         24  
4 1     1   4 use warnings;
  1         1  
  1         20  
5              
6 1     1   360 use Sub::WrapPackages ();
  1         2  
  1         171  
7              
8             our $VERSION = '2.02';
9              
10             sub import {
11 1     1   8 my $indent = '';
12             Sub::WrapPackages->import(
13             packages => [@_[1 .. $#_]],
14             wrap_inherited => 1,
15             pre => sub {
16 3     3   81 print STDERR "${indent}Called $_[0] with: [".join(', ', @_[1..$#_])."]\n";
17 3         19 $indent .= ' ';
18             },
19             post => sub {
20 3     3   6 $indent = substr($indent, 2);
21 3         44 print STDERR "${indent}Return from $_[0] with: [".join(', ', @_[1 .. $#_])."]\n";
22             }
23 1         10 );
24             }
25              
26             1;
27              
28             =head1 NAME
29              
30             Sub::WrapPackages::CallTree
31              
32             =head1 DESCRIPTION
33              
34             Tool that uses Sub::WrapPackages to show on STDERR a tree of function calls as
35             your code runs, including arguments and a list of return values
36              
37             =head1 SYNOPSIS
38              
39             In your code - in a test file, perhaps:
40              
41             use Sub::WrapPackages::CallTree qw(My::App::* And::Another::Namespace)
42              
43             Or in your environment:
44              
45             PERL5OPT=-MSub::WrapPackages::CallTree=My::App::*,And::Another::Namespace
46              
47             The results will look something like this:
48              
49             Called Sub::WrapPackages::Tests::Victim::foo with: [Sub::WrapPackages::Tests::Victim]
50             Called Sub::WrapPackages::Tests::Victim::bar with: [1]
51             Called Sub::WrapPackages::Tests::Victim::baz with: [OMG, ROBOTS, 5]
52             Return from Sub::WrapPackages::Tests::Victim::baz with: [OMG, ROBOTS, 5]
53             Return from Sub::WrapPackages::Tests::Victim::bar with: [OMG, ROBOTS, 5]
54             Return from Sub::WrapPackages::Tests::Victim::foo with: [2, OMG, ROBOTS, 5]
55              
56             NB that all arguments and return values are stringified for display, as there
57             is no way of nicely displaying a tree of function calls as well as complex data
58             structures, and the tree of function calls is more important.
59              
60             =head1 PARAMETERS
61              
62             The arguments are the same as those you would pass as the C option
63             to L. The other options have sensible defaults and can not
64             (currently) be set. If you would like to be able to over-ride the defaults
65             please submit a pull request with tests.
66              
67             =head1 BUGS and IMPROVEMENTS
68              
69             Please report bugs and submit improvements via Github.
70              
71             =head1 THANKS TO
72              
73             Thanks to Will Shepherd, who berated me for cut n pasting the code that
74             implements this all over the place while debugging, and prompting me to wrap it
75             up into a much smaller piece of code that I can splatter all over the place
76             while debugging.
77              
78             =head1 COPYRIGHT and LICENCE
79              
80             Copyright 2022 David Cantrell EFE
81              
82             This software is free-as-in-speech software, and may be used, distributed, and
83             modified under the terms of either the GNU General Public Licence version 2 or
84             the Artistic Licence. It's up to you which one you use. The full text of the
85             licences can be found in the files GPL2.txt and ARTISTIC.txt, respectively.
86              
87             =head1 CONSPIRACY
88              
89             This code is also free-as-in-mason.
90              
91             =cut
92