blib/lib/Devel/Trace/More.pm

filter: Statementstmt 67.3% 35 / 52 Branchbran 31.2% 5 / 16 Conditioncond 36.3% 4 / 11 MC/DCmcdc 28.5% 2 / 7 Subroutinesub 76.9% 10 / 13 Podpod 75.0% 3 / 4 totaltotal 57.2% 59 / 103 CC 13 SCAR 18.1
CC
13
Cov
56%
DB
14.6
output_to
12.0
trace
6.0
CRAP 6.1
↑ Summary
1 package Devel::Trace::More;
2  
3  
4 =head1 NAME
5  
6 Devel::Trace::More - Like Devel::Trace but with more control
7  
8 =head1 VERSION
9  
10 Version 0.05
11  
12 =head1 SYNOPSIS
13  
14 #!/usr/bin/perl -d:Trace::More
15  
16 use Devel::Trace::More qw{ filter_on };
17
18 filter_on('blah');
19  
20 filter_on(qr/blah/);
21  
22 filter_on(sub { my ($p, $file, $line, $code) = @_; ... });
23  
24 # or
25  
26 $IS_INTERESTING = sub { my ($p, $file, $line, $code) = @_; ... };
27  
28 =head1 DESCRIPTION
29  
30 This module will print out every line of code as it executes when used under
31 the perl debugger. By default all executed lines will print to STDERR. By
32 calling filter_on with a code ref, regex ref, a scalar, or by setting
33 $Devel::Trace::More::IS_INTERESTING directly then only those lines that are
34 'interesting' will be returned.
35  
36 If filter_on is given a scalar or a regular expression reference then the
37 file name of the code being executed or the line of code itself that matches
38 the given patter will be printed. Passing in a code ref is the same as setting
39 $IS_INTERESTING itself. Setting the filter this way will allow you to do
40 more complicated things like filtering on just the module name or the line number
41 of the code. $IS_INTERESTING can be changed in different places in the code if
42 needed.
43  
44 Caveat: Using regular expressions to filter what gets printed can cause unexected
45 issues if the code being debugged relies on the regular expression global variables.
46 Use with caution!
47  
48 =cut
49  
50 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
51 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
52  
53 1 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
54  
55 1 ▶use base 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
56 1 our @EXPORT_OK = qw{ trace filter_on output_to };
57  
58 1 our $VERSION = '0.05';
59  
60 1 ▶our $IS_INTERESTING = sub { return 1; };
Subroutine __ANON__: called CC=1 SCAR=0.0
61 1 our $TRACE = 1;
62 1 our $OUT = *STDERR;
63  
64 # This is the important part. The rest is just fluff.
65 0 ▶sub DB::DB {
Subroutine DB: not called CC=5 SCAR=26.8
Pod undocumented
66 0 ▶ return unless $TRACE;
Branch: 1 branch
BranchTrueFalseTotal
unless $Devel::Trace::More::TRACE 0 0 0
67 0 my ($p, $f, $l) = caller;
68
69 # have no idea how to do this with strict on
70 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
71 0 my $code = \@{"::_<$f"};
72 1 ▶ use strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
73 0 ▶ my $code_line = defined($code->[$l]) ? $code->[$l] : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $code->[$l] ? : 0 0 0
74 0 chomp($code_line);
75
76 0 ▶ print $OUT ">> $f:$l: $code_line\n" if $OUT && $IS_INTERESTING->($p, $f, $l, $code_line);
Branch: 1 branch
BranchTrueFalseTotal
if $Devel::Trace::More::OUT and &$Devel::Trace::More::IS_INTERESTING($p, $f, $l, $code_line) 0 0 0
Condition0%0/3
$Devel::Trace::More::OUT and &$Devel::Trace::More::IS_INTERESTING($p, $f, $l, $code_line)
!ll&&!rl&&r
000
MC/DC0%0/2
$Devel::Trace::More::OUT and &$Devel::Trace::More::IS_INTERESTING($p, $f, $l, $code_line)
$Devel::Trace::More::OUT &$Devel::Trace::More::IS_INTERESTING($p, $f, $l, $code_line)
Truth table0%0/3
A and B
A= $Devel::Trace::More::OUTB= &$Devel::Trace::More::IS_INTERESTING($p, $f, $l, $code_line)
ABresult
0X0
100
111
77 }
78  
79 =head1 FUNCTIONS
80  
81 =head2 filter_on(...)
82  
83 Takes a string, code ref, or regular expression ref and sets the IS_INTERESTING code ref appropriately.
84  
85 =over 1
86  
87 =item String
88  
89 A string will cause the line of code to be printed if either the filename or the code line has the
90 string in it.
91  
92 =item Code Ref
93  
94 A code ref passed will just set $IS_INTERESTING to it, saves a few characters of typing.
95  
96 =item RegEx Ref
97  
98 The line of code will be printed if the regular expression matches either the file name or the line of code
99  
100 =back
101  
102 =cut
103  
104 3 ▶sub filter_on {
Subroutine filter_on: called CC=4 SCAR=14.1
Pod documented
105 3 my $filter = shift;
106  
107 3 ▶ if ( uc( ref($filter) ) eq 'REGEXP') {
Branch: 3 branches
BranchTrueFalseTotal
if (uc ref $filter eq 'REGEXP') { } 1 2 3
elsif (uc ref $filter eq 'CODE') { } 1 1 2
elsif (not ref $filter) { } 1 0 1
108 1 ▶ $IS_INTERESTING = sub { my ($p, $file, $line_num, $code_line) = @_; return $file =~ $filter || $code_line =~ $filter; };
Subroutine __ANON__: called CC=2 SCAR=7.0
Condition66%2/3
$file =~ /$filter/ || $code_line =~ /$filter/
l!l&&r!l&&!r
101
MC/DC50%1/2
$file =~ /$filter/ || $code_line =~ /$filter/
$file =~ /$filter/ $code_line =~ /$filter/
Truth table66%2/3
A || B
A= $file =~ /$filter/B= $code_line =~ /$filter/
ABresult
1X1
011
000
109 }
110 elsif ( uc( ref($filter) ) eq 'CODE') {
111 1 $IS_INTERESTING = $filter;
112 }
113 elsif (! ref($filter) ) {
114 1 ▶ $IS_INTERESTING = sub { my ($p, $file, $line_num, $code_line) = @_; return ( index($file, $filter) > -1) || ( index($code_line, $filter) > -1); };
Subroutine __ANON__: called CC=2 SCAR=7.0
Condition66%2/3
(index($file, $filter) != -1) || (index($code_line, $filter) != -1)
l!l&&r!l&&!r
101
MC/DC50%1/2
(index($file, $filter) != -1) || (index($code_line, $filter) != -1)
(index($file, $filter) != -1) (index($code_line, $filter) != -1)
Truth table66%2/3
A || B
A= (index($file, $filter) != -1)B= (index($code_line, $filter) != -1)
ABresult
1X1
011
000
115 }
116 else {
117 0 die "I don't know how to handle that filter!";
118 }
119 }
120  
121 =head2 trace('on') or trace('off')
122  
123 Turns the printing of code on or off
124  
125 =cut
126  
127 1 my %tracearg = ('on' => 1, 'off' => 0);
128 0 ▶sub trace {
Subroutine trace: not called CC=2 SCAR=17.9
Pod documented
129 0 my $arg = shift;
130 0 ▶ $arg = $tracearg{$arg} while exists $tracearg{$arg};
Branch: 1 branch
BranchTrueFalseTotal
exists $tracearg{$arg} and do { $arg = $tracearg{$arg} } 0 0 0
131 0 $TRACE = $arg;
132 }
133  
134 =head2 output_to($filename)
135  
136 Given a filename the code lines will get printed to the file instead of STDERR.
137 Can be called with different filenames at different points in the script if need be.
138 By default the file will be open for reading and will be either created or cleared.
139 You can input '>>' as a param to have the trace keep appending.
140  
141 =cut
142  
143 0 ▶sub output_to {
Subroutine output_to: not called CC=3 SCAR=24.8
Pod documented
144 # have to turn trace off because messing with filehandles while
145 # it's tracing itself might cause it to die
146 0 trace('off');
147 0 my $filename = shift;
148 0 ▶ my $mode = shift || '>';
Condition0%0/2
shift() || '>'
l!l
00
MC/DC0%0/1
shift() || '>'
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
149  
150 # There can be cases where STDOUT/STDERR messed with in the code
151 # which will cause problems if $OUT isn't cleared first
152 0 $OUT = undef;
153  
154 0 ▶ open $OUT, $mode, $filename or die "Can't open file $filename : $!";
Branch: 1 branch
BranchTrueFalseTotal
open $Devel::Trace::More::OUT, $mode, $filename or die "Can't open file $filename : $!" 0 0 0
155 0 trace('on');
156 }
157  
158 1 1;
159 __END__
↑ Summary