blib/lib/DBG.pm

filter: Statementstmt 95.2% 242 / 254 Branchbran 64.0% 73 / 114 Conditioncond 50.0% 19 / 38 MC/DCmcdc 23.8% 5 / 21 Subroutinesub 100% 43 / 43 Podpod 100% 15 / 15 totaltotal 81.8% 397 / 485 CC 80 SCAR 16.9
CC
80
Cov
82%
rt
13.1
_cycles
9.8
inh
8.3
CRAP 5.4
↑ Summary
1 package DBG;
2 2 $DBG::VERSION = '0.004';
3 # ABSTRACT: A collection of debugging functions
4  
5  
6 2 ▶use v5.10;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 2 ▶use parent 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use FileHandle;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use Perl::Tidy;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use DateTime;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 2 ▶use Scalar::Util qw(refaddr reftype blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
17 2 ▶use List::MoreUtils qw(natatime);
Subroutine BEGIN: called CC=1 SCAR=0.0
18 2 ▶use B qw(svref_2object);
Subroutine BEGIN: called CC=1 SCAR=0.0
19 2 ▶use B::Deparse;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 2 ▶use Devel::Size qw(total_size);
Subroutine BEGIN: called CC=1 SCAR=0.0
21 2 ▶use Class::MOP;
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 2 our @EXPORT = qw(dmp png trc dbg ts rt cyc prp cnm pkg sz mtd inh dpr flt);
24  
25 2 ▶our $ON = $ENV{DBG_ON} // 1;
Condition50%1/2
$ENV{"DBG_ON"} // 1
l!l
02
MC/DC0%0/1
$ENV{"DBG_ON"} // 1
$ENV{"DBG_ON"}
Truth table50%1/2
A
A= $ENV{"DBG_ON"}
Aresult
11
00
26 2 ▶our $HEADER = $ENV{DBG_HEADER} // 1;
Condition50%1/2
$ENV{"DBG_HEADER"} // 1
l!l
02
MC/DC0%0/1
$ENV{"DBG_HEADER"} // 1
$ENV{"DBG_HEADER"}
Truth table50%1/2
A
A= $ENV{"DBG_HEADER"}
Aresult
11
00
27  
28 2 our ( $fh, $fn, $stamped );
29  
30 BEGIN {
31 $fn =
32 defined $ENV{DBG_LOG}
33 ? $ENV{DBG_LOG} eq '0'
34 ? ''
35 : $ENV{DBG_LOG} . ''
36 2 ▶ : File::Spec->catfile( $ENV{HOME}, 'DBG.log' );
Subroutine BEGIN: called CC=3 SCAR=11.2
Branch: 2 branches
BranchTrueFalseTotal
defined $ENV{'DBG_LOG'} ? : 1 1 2
$ENV{'DBG_LOG'} eq '0' ? : 0 1 1
37 }
38  
39 sub _tee($) {
40 44 ▶ return unless $ON;
Subroutine _tee: called CC=7 SCAR=19.7
Branch: 1 branch
BranchTrueFalseTotal
unless $DBG::ON 1 43 44
41 43 my $data = shift;
42 43 ▶ return unless defined $data;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $data 0 43 43
43 43 ▶ if ( $HEADER && !$stamped ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($DBG::HEADER and not $DBG::stamped) 1 42 43
Condition66%2/3
$DBG::HEADER and not $DBG::stamped
!ll&&!rl&&r
0421
MC/DC50%1/2
$DBG::HEADER and not $DBG::stamped
$DBG::HEADER not $DBG::stamped
Truth table66%2/3
A and not(B)
A= $DBG::HEADERB= not $DBG::stamped
ABresult
0X0
100
111
44 1 my @msg = (
45 '>> DEBUGGING SESSION START: ',
46 DateTime->now, ' ; PID: ', $$, ' <<', "\n\n"
47 );
48 1 ▶ print $fh @msg if $fh;
Branch: 1 branch
BranchTrueFalseTotal
if $DBG::fh 1 0 1
49 1 print STDERR @msg;
50 1 $stamped = 1;
51 }
52 43 $data =~ s/\s++$//;
53 43 $data .= "\n";
54 43 ▶ print $fh $data if $fh;
Branch: 1 branch
BranchTrueFalseTotal
if $DBG::fh 43 0 43
55 43 print STDERR $data;
56 }
57  
58 BEGIN {
59 2 ▶ if ( length $fn ) {
Subroutine BEGIN: called CC=3 SCAR=11.3
Branch: 1 branch
BranchTrueFalseTotal
if (length $DBG::fn) 2 0 2
60 2 ▶ $fh = FileHandle->new(">> $fn") or die $!;
Branch: 1 branch
BranchTrueFalseTotal
$DBG::fh = "FileHandle"->new(">> $DBG::fn") or die $! 0 2 2
61 2 binmode $fh, ':utf8';
62 2 binmode *STDERR, ':utf8';
63 2 $fh->autoflush(1);
64 }
65 }
66  
67 END {
68 2 ▶ if ( $HEADER && $stamped ) {
Subroutine END: called CC=4 SCAR=14.1
Branch: 1 branch
BranchTrueFalseTotal
if ($DBG::HEADER and $DBG::stamped) 1 1 2
Condition66%2/3
$DBG::HEADER and $DBG::stamped
!ll&&!rl&&r
011
MC/DC50%1/2
$DBG::HEADER and $DBG::stamped
$DBG::HEADER $DBG::stamped
Truth table66%2/3
A and B
A= $DBG::HEADERB= $DBG::stamped
ABresult
0X0
100
111
69 1 my $msg = join '', "\n", '** DEBUGGING SESSION END: ', DateTime->now,
70 ' ; PID: ', $$, ' **';
71 1 _tee($msg);
72 }
73 2 ▶ $fh->close if $fh;
Branch: 1 branch
BranchTrueFalseTotal
if $DBG::fh 2 0 2
74 }
75  
76 { # DateTime with optional label payload
77  
78 package DBG::ts;
79 2 $DBG::ts::VERSION = '0.004';
80 2 ▶use parent 'DateTime';
Subroutine BEGIN: called CC=1 SCAR=0.0
81 2 ▶ use Scalar::Util qw(refaddr);
Subroutine BEGIN: called CC=1 SCAR=0.0
82  
83 2 our %messages;
84  
85 sub text {
86 6 ▶ my ( $self, $text ) = @_;
Subroutine text: called CC=2 SCAR=6.9
87 6 my $addr = refaddr $self;
88 6 my $old = $messages{$addr};
89 6 ▶ $messages{$addr} = $text if defined $text;
Branch: 1 branch
BranchTrueFalseTotal
if defined $text 1 5 6
90 6 return $old;
91 }
92  
93 sub DESTROY {
94 3 ▶ my $self = shift;
Subroutine DESTROY: called CC=2 SCAR=7.1
95 3 delete $messages{ refaddr $self };
96 3 ▶ $self->SUPER::DESTROY if $self->can("SUPER::DESTROY");
Branch: 1 branch
BranchTrueFalseTotal
if $self->can("SUPER::DESTROY") 0 3 3
97 }
98 }
99  
100  
101 2 sub ts(;$) {
102 3 ▶ my $ts = DBG::ts->now;
Subroutine ts: called CC=1 SCAR=0.0
Pod documented
103 3 $ts->text(shift);
104 3 return $ts;
105 }
106  
107  
108 sub rt($$) {
109 2 ▶ return unless $ON;
Subroutine rt: called CC=10 SCAR=25.7
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
unless $DBG::ON 0 2 2
110 2 for (@_) {
111 4 ▶ die 'DBG::ts expected'
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($_) and $_->isa("DBG::ts") 0 4 4
Condition33%1/3
builtin::blessed($_) and $_->isa("DBG::ts")
!ll&&!rl&&r
004
MC/DC0%0/2
builtin::blessed($_) and $_->isa("DBG::ts")
builtin::blessed($_) $_->isa("DBG::ts")
Truth table33%1/3
A and B
A= builtin::blessed($_)B= $_->isa("DBG::ts")
ABresult
0X0
100
111
112 unless blessed($_) && $_->isa('DBG::ts');
113 }
114 2 my ( $t1, $t2 ) = @_;
115 2 my $i = natatime 2, ( $t2 - $t1 )->deltas;
116 2 my $reported;
117 2 my $text = $t1->text;
118 2 my $prefix = '';
119 2 ▶ if ( defined $text ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $text) 1 1 2
120 1 _tee("timestamp $text");
121 1 $prefix = "\t";
122 }
123 2 ▶ while ( my ( $unit, $amt ) = $i->() ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = &$i()) 10 2 12
124 10 ▶ next unless $amt;
Branch: 1 branch
BranchTrueFalseTotal
unless $amt 10 0 10
125 0 $reported = 1;
126 0 ▶ $unit =~ s/s$// if $amt == 1;
Branch: 1 branch
BranchTrueFalseTotal
if $amt == 1 0 0 0
127 0 _tee("$prefix$amt $unit");
128 }
129 2 ▶ _tee("${prefix}negligible time elapsed") unless $reported;
Branch: 1 branch
BranchTrueFalseTotal
unless $reported 2 0 2
130 2 return $t2;
131 }
132  
133  
134 sub trc() {
135 1 ▶ return unless $ON;
Subroutine trc: called CC=4 SCAR=13.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
unless $DBG::ON 0 1 1
136 1 _tee 'TRACE';
137 1 my $i = 0;
138 1 my @stack;
139 1 ▶ while ( my @frame = caller($i) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@frame) = caller $i) 6 1 7
140 6 push @stack, [ $i++, $frame[3], $frame[1], $frame[2] ];
141 }
142 1 my $fmt = '%' . length( $stack[-1][0] ) . 'd) %s (%s:%d)';
143 1 for $i ( 1 .. $#stack ) {
144 5 _tee sprintf $fmt, ( @{ $stack[$i] } )[ 0 .. 1 ],
145 5 ( @{ $stack[ $i - 1 ] } )[ 2 .. 3 ];
146 }
147 1 _tee 'END TRACE';
148 }
149  
150  
151 sub dmp($) {
152 3 ▶ return unless $ON;
Subroutine dmp: called CC=2 SCAR=7.0
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
unless $DBG::ON 0 3 3
153 3 my $ref = shift;
154 3 local $Data::Dumper::Indent = 1;
155 3 local $Data::Dumper::Sortkeys = 1;
156 3 local $Data::Dumper::Quotekeys = 0;
157 3 local $Data::Dumper::Terse = 1;
158 3 my $code = Dumper $ref;
159 3 _dmp($code);
160 }
161  
162 sub _dmp {
163 4 ▶ my $code = shift;
Subroutine _dmp: called CC=2 SCAR=7.5
164 4 my ( $ds, $stderr_string );
165 4 local @ARGV; # prevents Perl::Tidy craziness
166 4 my $error = Perl::Tidy::perltidy(
167 source => \$code,
168 destination => \$ds,
169 stderr => \$stderr_string
170 );
171 4 ▶ if ($error) {
Branch: 1 branch
BranchTrueFalseTotal
if ($error) { } 0 4 4
172 0 _tee "TIDY ERROR: $stderr_string";
173 0 _tee $code;
174 }
175 else {
176 4 _tee $ds;
177 }
178 }
179  
180  
181 sub dbg($) {
182 1 ▶ return unless $ON;
Subroutine dbg: called CC=2 SCAR=7.1
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
unless $DBG::ON 0 1 1
183 1 my $data = shift;
184 1 _tee $data;
185 }
186  
187  
188 sub png(;$) {
189 3 ▶ return unless $ON;
Subroutine png: called CC=8 SCAR=21.1
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
unless $DBG::ON 0 3 3
190 3 my $msg = shift;
191 3 my @frame = caller(1);
192 3 my $data;
193 3 ▶ if ( @frame && $msg ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@frame and $msg) { } 2 1 3
Condition66%2/3
@frame and $msg
!ll&&!rl&&r
012
MC/DC50%1/2
@frame and $msg
@frame $msg
Truth table66%2/3
A and B
A= @frameB= $msg
ABresult
0X0
100
111
194 2 ( $data = $frame[3] ) =~ s/.*::(.*)/in code $1/;
195 }
196 else {
197 1 ▶ $data = @frame ? sprintf( 'PING %4$s (%2$s:%3$d)', @frame ) : 'PING';
Branch: 1 branch
BranchTrueFalseTotal
@frame ? : 1 0 1
198 }
199 3 ▶ $data .= " -- $msg" if $msg && ( ref $msg || $msg ne '1' );
Branch: 1 branch
BranchTrueFalseTotal
if $msg and ref $msg || $msg ne 1 1 2 3
Condition83%5/6
$msg and ref $msg || $msg ne 1
!ll&&!rl&&r
111
ref $msg or $msg ne 1
l!l&&r!l&&!r
011
MC/DC66%2/3
$msg and ref $msg || $msg ne 1
$msg ref $msg $msg ne 1
Truth table75%3/4
A and B or C
A= $msgB= ref $msgC= $msg ne 1
ABCresult
0XX0
1000
11X1
1011
200 3 _tee $data;
201 }
202  
203  
204 sub cyc($) {
205 1 ▶ return unless $ON;
Subroutine cyc: called CC=2 SCAR=7.1
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
unless $DBG::ON 0 1 1
206 1 _tee '===== OBJECT GRAPH =====';
207 1 _cycles( shift, {}, 0, 'base' );
208 }
209  
210 sub _cycles {
211 3 ▶ my ( $ref, $hash, $indent, $parent ) = @_;
Subroutine _cycles: called CC=8 SCAR=22.8
212 3 my $type = reftype $ref;
213 3 ▶ return unless $type;
Branch: 1 branch
BranchTrueFalseTotal
unless $type 0 3 3
214 ## Please see file perltidy.ERR
215 3 my $addr = refaddr $ref;
216 3 ▶ my $name = blessed $ref // $type;
Condition33%1/3
builtin::blessed($ref) // $type
l!l&&r!l&&!r
030
MC/DC0%0/2
builtin::blessed($ref) // $type
builtin::blessed($ref) $type
Truth table33%1/3
A // B
A= builtin::blessed($ref)B= $type
ABresult
1X1
011
000
217 3 my $left = ' ' x ( $indent * 3 );
218 3 ▶ if ( $hash->{$addr}++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hash->{$addr}++) { } 1 2 3
219 _tee sprintf '%s%s (%s <- %s) -- ref count: %d', $left, $name, $addr,
220 $parent,
221 1 $hash->{$addr};
222 }
223 else {
224 2 _tee sprintf '%s%s (%s <- %s)', $left, $name, $addr, $parent;
225 2 ▶ if ( $type eq 'HASH' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($type eq 'HASH') { } 2 0 2
elsif ($type eq 'ARRAY') { } 0 0 0
226 2 _cycles( $_, $hash, $indent + 1, $addr ) for values %$ref;
227 }
228 elsif ( $type eq 'ARRAY' ) {
229 0 _cycles( $_, $hash, $indent + 1, $addr ) for @$ref;
230 }
231 }
232 }
233  
234  
235 sub prp($$) {
236 2 ▶ my ( $msg, $var ) = @_;
Subroutine prp: called CC=2 SCAR=6.9
Pod documented
237 2 $msg =~ s/\??\s*$/? /;
238 2 ▶ _tee( $msg . ( $var ? 'yes' : 'no' ) );
Branch: 1 branch
BranchTrueFalseTotal
$var ? : 1 1 2
239 }
240  
241  
242 sub cnm($;$) {
243 1 ▶ my ( $code, $quiet ) = @_;
Subroutine cnm: called CC=6 SCAR=18.5
Pod documented
244 1 ▶ return unless ref $code;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $code 0 1 1
245 1 my $gv = _code_name($code);
246 1 my $name = '';
247 1 ▶ if ( my $st = $gv->STASH ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $st = $gv->STASH) 1 0 1
248 1 $name = $st->NAME . '::';
249 }
250 1 my $n = $gv->NAME;
251 1 ▶ if ($n) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n) 1 0 1
252 1 $name .= $n;
253 1 ▶ if ( $n eq '__ANON__' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n eq "__ANON__") 1 0 1
254 1 $name .= ' defined at ' . $gv->FILE . ':' . $gv->LINE;
255 }
256 }
257 1 ▶ _tee($name) unless $quiet;
Branch: 1 branch
BranchTrueFalseTotal
unless $quiet 1 0 1
258 1 return $name;
259 }
260  
261 sub _code_name {
262 12 ▶ my $code = shift;
Subroutine _code_name: called CC=4 SCAR=15.6
263 12 ▶ return unless my $cv = svref_2object($code);
Branch: 1 branch
BranchTrueFalseTotal
unless my $cv = svref_2object($code) 0 12 12
264 return
265 12 ▶ unless $cv->isa('B::CV')
Branch: 1 branch
BranchTrueFalseTotal
unless $cv->isa("B::CV") and my $gv = $cv->GV 0 12 12
Condition33%1/3
$cv->isa("B::CV") and my $gv = $cv->GV
!ll&&!rl&&r
0012
MC/DC0%0/2
$cv->isa("B::CV") and my $gv = $cv->GV
$cv->isa("B::CV") my $gv = $cv->GV
Truth table33%1/3
A and B
A= $cv->isa("B::CV")B= my $gv = $cv->GV
ABresult
0X0
100
111
266 and my $gv = $cv->GV;
267 12 return $gv;
268 }
269  
270  
271 sub pkg($$;$) {
272 2 ▶ my ( $obj, $method, $file ) = @_;
Subroutine pkg: called CC=6 SCAR=18.4
Pod documented
273 2 ▶ return _tee('first parameter must be an object') unless blessed $obj;
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($obj) 0 2 2
274 2 ▶ return _tee('method not defined') unless defined $method;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $method 0 2 2
275 2 my $m = $obj->can($method);
276 2 ▶ return _tee( "did not find method $method in " . ref $obj ) unless $m;
Branch: 1 branch
BranchTrueFalseTotal
unless $m 0 2 2
277 2 my $gv = _code_name($m);
278 2 ▶ return _tee("could not find $method") unless $gv;
Branch: 1 branch
BranchTrueFalseTotal
unless $gv 0 2 2
279 2 ▶ if ( !$file ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $file) { } 1 1 2
280 1 _tee( sprintf 'package: %s; file: %s; line: %s',
281 $gv->STASH->NAME, $gv->FILE, $gv->LINE );
282 }
283 else {
284 1 _tee( $gv->STASH->NAME );
285 }
286 }
287  
288  
289 sub sz($;$) {
290 2 ▶ state $ts = eval { require Devel::Size };
Subroutine sz: called CC=3 SCAR=11.2
Pod documented
291 2 ▶ if ($ts) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ts) { } 2 0 2
292 2 my $msg = Devel::Size::total_size( pop @_ );
293 2 ▶ $msg = pop(@_) . ' ' . $msg if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 1 1 2
294 2 _tee($msg);
295 }
296 else {
297 0 _tee('sz requires Devel::Size');
298 }
299 }
300  
301  
302 sub mtd($;$) {
303 2 ▶ my ( $obj, $verbose ) = @_;
Subroutine mtd: called CC=7 SCAR=19.5
Pod documented
304 2 ▶ if ( my $class = ref $obj ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $class = ref $obj) { } 2 0 2
305 2 my $meta = Class::MOP::Class->initialize($class);
306 2 _tee("Class: $class");
307 2 ▶ if ($verbose) {
Branch: 1 branch
BranchTrueFalseTotal
if ($verbose) { } 1 1 2
308 1 my $longest = 0;
309 1 for ( $meta->get_all_methods ) {
310 9 my $l = length $_->name;
311 9 ▶ $longest = $l if $l > $longest;
Branch: 1 branch
BranchTrueFalseTotal
if $l > $longest 3 6 9
312 }
313 1 my $format = '%-' . $longest . 's : %s %s';
314 1 for my $method ( sort { $a->name cmp $b->name }
315 $meta->get_all_methods )
316 {
317 9 my $code = $obj->can( $method->name );
318 9 my $gv = _code_name($code);
319 9 ▶ if ( $gv->LINE ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($gv->LINE) { } 3 6 9
320 3 _tee( sprintf $format, $method->name, $gv->FILE,
321 $gv->LINE );
322 }
323 else {
324 6 _tee( $method->fully_qualified_name );
325 }
326 }
327 }
328 else {
329 dmp(
330 [
331 1 sort map { $_->fully_qualified_name }
332 $meta->get_all_methods
333 ]
334 );
335 }
336 }
337 else {
338 0 _tee "NOT AN OBJECT: $obj";
339 }
340 }
341  
342  
343 sub inh($) {
344 1 ▶ my $class = shift;
Subroutine inh: called CC=6 SCAR=21.2
Pod documented
345 1 ▶ _tee('inh needs a class') && return unless length( $class // '' );
Branch: 1 branch
BranchTrueFalseTotal
unless length($class // "") 0 1 1
Condition28%2/7
length($class // '') || &_tee('inh needs a class') && (return)
l!l&&r!l&&!r
100
$class // ""
l!l
10
&_tee("inh needs a class") and return
!ll
00
MC/DC0%0/2
length($class // '') || &_tee('inh needs a class') && (return)
$class &_tee("inh needs a class")
Truth table33%1/3
A || B
A= $classB= &_tee("inh needs a class")
ABresult
1X1
011
000
346 1 ▶ $class = ref($class) || $class;
Condition33%1/3
ref $class || $class
l!l&&r!l&&!r
010
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table33%1/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
347 1 my $hash = { $class => 1 };
348 1 _fetch_classes( $class, $hash );
349 1 my @classes = sort keys %$hash;
350 1 _tee("Classes in the inheritance hierarchy of $class:");
351 1 _tee(" $_") for @classes;
352 }
353  
354 sub _fetch_classes {
355 2 ▶ my ( $class, $hash ) = @_;
Subroutine _fetch_classes: called CC=3 SCAR=11.0
356 2 my @ar = eval '@' . $class . '::ISA';
357 2 my @new = grep { !$hash->{$_} } @ar;
358 2 $hash->{$_} = 1 for @ar;
359 2 _fetch_classes( $_, $hash ) for @new;
360 }
361  
362  
363 sub dpr {
364 1 ▶ my $ref = shift;
Subroutine dpr: called CC=2 SCAR=7.0
Pod documented
365 1 ▶ die 'code reference expected' unless ref $ref eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $ref eq "CODE" 0 1 1
366 1 my $d = B::Deparse->new(@_);
367 1 _dmp( $d->coderef2text($ref) );
368 }
369  
370  
371 sub flt($;$) {
372 1 ▶ my $v = _flt(shift);
Subroutine flt: called CC=2 SCAR=7.1
Pod documented
373 1 ▶ dmp($v) unless shift;
Branch: 1 branch
BranchTrueFalseTotal
unless shift() 1 0 1
374 1 return $v;
375 }
376  
377 sub _flt {
378 2 ▶ my $i = shift;
Subroutine _flt: called CC=4 SCAR=15.0
379 2 ▶ return "$i" if blessed $i;
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($i) 1 1 2
380 1 for ( ref $i ) {
381 1 when ('HASH') {
382 1 my %h = %$i;
383 1 $_ = _flt($_) for values %h;
384 1 return \%h;
385 }
386 0 when ('ARRAY') {
387 0 return [ map { _flt($_) } @$i ];
388 }
389 }
390 0 return $i;
391 }
392  
393 2 1;
394  
395 __END__
396  
397 =pod
398  
399 =encoding UTF-8
400  
401 =head1 NAME
402  
403 DBG - A collection of debugging functions
404  
405 =head1 VERSION
406  
407 version 0.004
408  
409 =head1 SYNOPSIS
410  
411 package Foo::Bar::Baz;
412 use DBG;
413  
414 ...
415 dbg "log this $message";
416 ...
417 png; # do I ever get here?
418 ...
419 trc; # how did I get here?
420 ...
421 dmp $obj; # what is this?
422 ...
423 cyc $obj; # does this have reference cycles?
424 ...
425 my $ts = ts; # get me the current time
426 ...
427 rt $ts, ts; # how long did that take?
428 ...
429 prp "is it so", $val; # prints message plus "yes" or "no"
430 ...
431 pkg $obj, 'doit'; # prints package providing obj's doit method
432 ...
433  
434 =head1 DESCRIPTION
435  
436 This is just a collection of functions useful for debugging. Instead of adding
437  
438 use Data::Dumper;
439 use B::Deparse;
440 use Devel::Size qw(total_size);
441  
442 and so forth you can just type
443  
444 use DBG;
445  
446 at the top of the script. If you're using git, or another version control system
447 with similar functionality, you can write a simple pre-commit hook to prevent
448 yourself from committing debugging lines to the repository. Once you've deleted
449 the C<use DBG;> line you can find all the other stuff you may have left in by
450 trying to compile the code and looking at the errors.
451  
452 All functions have short names to make debugging quick(er).
453  
454 All debugging messages are printed both to the screen and to a log. The log will
455 be C<~/DBG.log> unless otherwise specified. See C<$ENV{DBG_LOG}>. This facilitates
456 examining debugging output at one's leisure without having to visually cull away
457 any other output produced by the program.
458  
459 All debugging functions are exported by default.
460  
461 A timestamp will be printed before any debugging output to facilitate
462 distinguising one debugging session from another.
463  
464 =head1 FUNCTIONS
465  
466 =head2 ts(;$) -- "get timestamp"
467  
468 Returns a L<DateTime>-based timestamp. The optional argument is a label for the
469 timestamp. The label will be accessible via the timestamp's C<text> method.
470  
471 my $t = ts 'foo';
472 say $t->text; # foo
473 say $t; # 2014-05-31T22:31:52
474  
475 =head2 rt($$) -- "report timestamp"
476  
477 Report time difference. This function expects two objects generated by the
478 C<ts> function, the earlier first. It returns the second timestamp to facilitate
479 the
480  
481 my $ts = ts;
482 # some code
483 $ts = rt $ts, ts
484  
485 pattern.
486  
487 The report will vary according to whether the first timestamp holds a label.
488  
489 my $t1 = ts 'foo';
490 my $t2 = rt $t1, ts 'bar';
491 sleep 1;
492 my $t3 = rt $t2, ts;
493 sleep 61;
494 rt $t3, ts;
495  
496 # timestamp foo
497 # negligible time elapsed
498 # timestamp bar
499 # 1 second
500 # 1 minute
501 # 1 second
502  
503 =head2 trc() -- "trace"
504  
505 Prints a stack trace, skipping its own frame. Each line of the trace is
506 formatted as
507  
508 frame number) code name (file:line)
509  
510 This involves munging the frames as returned by C<caller> so instead of saying
511 "you got here when called from here" it says simply "you are here". The next
512 line says how you got here. That is, the code name is the name of the code
513 you're in, not the code that just called you. I simply find this easier to
514 follow.
515  
516 sub foo { bar() }
517 sub bar { baz() }
518 sub baz { plugh() }
519 sub plugh { trc }
520 foo();
521  
522 # TRACE
523 # 1) main::plugh (test.pl:11)
524 # 2) main::baz (test.pl:10)
525 # 3) main::bar (test.pl:9)
526 # 4) main::foo (test.pl:8)
527 # END TRACE
528  
529 =head2 dmp($) -- "dump"
530  
531 Prints a pretty data dump. This uses a combination of L<Perl::Tidy> and
532 L<Data::Dumper>.
533  
534 my $r = { a => [qw(1 2 3)], c => { d => undef, egg => [ {}, {} ] } };
535 dmp $r;
536  
537 # {
538 # a => [ '1', '2', '3' ],
539 # c => {
540 # d => undef,
541 # egg => [ {}, {} ]
542 # }
543 # }
544  
545 =head2 dbg($) -- "debug"
546  
547 Prints a message to the debugging log.
548  
549 dbg 'foo'; # foo
550  
551 =head2 png(;$) -- "ping"
552  
553 Prints a ping message to the debugging log. If optional argument is true, just
554 prints "in code <code name> -- <optional arg>", where C<code name> is the name
555 of the function or method minus the package. If the optional argument is just
556 "1", it is not suffixed to the ping message.
557  
558 sub foo { png }
559 sub bar { png 1 }
560 sub baz { png 'la la la la la' }
561 foo();
562 bar();
563 baz();
564  
565 # PING main::foo (test.pl:11)
566 # in code bar
567 # in code baz -- la la la la la
568  
569 =head2 cyc($) -- "cycles"
570  
571 Checks for cycles in a reference, teeing out the entire object graph.
572 This is like a condensed dump concerning itself only with references.
573  
574 my $a = {};
575 my $b = { b => $a };
576 my $c = { c => $b };
577 $a->{a} = $c;
578 cyc $a;
579  
580 # HASH (140416464744656 <- base)
581 # HASH (140416464745304 <- 140416464744656)
582 # HASH (140416464744992 <- 140416464745304)
583 # HASH (140416464744656 <- 140416464744992) -- ref count: 2
584  
585 =head2 prp($$) -- "property"
586  
587 Takes a message and a scalar to be evaluated as a boolean and submits this to
588 C<dbg> as C<"$message? yes/no">.
589  
590 prp 'true', 1;
591 prp 'true', 0;
592  
593 # true? yes
594 # true? no
595  
596 =head2 cnm($;$) -- "code name"
597  
598 cnm $ua->can('request'); # LWP::UserAgent::request
599  
600 Converts a code reference to the place in the source code it comes from. This
601 uses B::svref_2object to do its magic. Sometimes it will provide the file and
602 line number, sometimes not.
603  
604 If the optional second parameter is provided, the information is only returned,
605 not teed out.
606  
607 =head2 pkg($$;$) -- "package"
608  
609 Determines the package providing a method to an object. The first parameter is
610 the object and the second the method name. Unless the optional third parameter
611 is true, the file and line are also provided.
612  
613 my $d = DateTime->now;
614 pkg $d, 'ymd';
615  
616 # package: DateTime; file: /Users/houghton/perl5/lib/perl5/darwin-thread-multi-2level/DateTime.pm; line: 820
617  
618 pkg $d, 'ymd', 1;
619  
620 # DateTime
621  
622 =head2 sz($;$) -- "size"
623  
624 Tees out the size of a scalar. If two arguments are given, the first is taken
625 as a label and the second the scalar.
626  
627 sz {}; # 128
628 sz 'foo', {}; # foo 128
629  
630 This delegates to the C<total_size> function in L<Devel::Size>. If you do not
631 have L<Devel::Size>, the C<sz> will only emit a warning that it requires
632 L<Devel::Size>.
633  
634 =head2 mtd($;$) -- "method"
635  
636 Dumps out a sorted list of the object's method names, fully qualified. If the
637 optional parameter is provided, it also lists where the code for each method
638 can be found.
639  
640 my $d = DateTime->now;
641 mtd $d;
642  
643 # Class: DateTime
644 # [
645 # 'DateTime::DefaultLanguage',
646 # 'DateTime::DefaultLocale',
647 # 'DateTime::INFINITY',
648 # 'DateTime::MAX_NANOSECONDS',
649 # ... # many lines omitted
650 # ]
651  
652 mtd $d, 1;
653  
654 # Class: DateTime
655 # UNIVERSAL::DOES
656 # DefaultLanguage : /Users/houghton/perl5/lib/perl5/darwin-thread-multi-2level/DateTime.pm 106
657 # DefaultLocale : /Users/houghton/perl5/lib/perl5/darwin-thread-multi-2level/DateTime.pm 106
658 # INFINITY : /Users/houghton/perl5/lib/perl5/constant.pm 30
659 # MAX_NANOSECONDS : /Users/houghton/perl5/lib/perl5/darwin-thread-multi-2level/Class/MOP/Mixin/HasMethods.pm 131
660 # ...
661  
662 =head2 inh($) -- "inheritance"
663  
664 Takes an object or class and prints out a sorted list of all the classes in
665 that object or class's inheritance tree.
666  
667 package Plugh;
668 package Foo;
669 our @ISA = qw(Plugh);
670 package Bar;
671 package Baz;
672 our @ISA = qw(Foo Bar);
673 package main;
674  
675 inh 'Baz';
676  
677 # Classes in the inheritance hierarchy of Baz:
678 # Bar
679 # Baz
680 # Foo
681 # Plugh
682  
683 =head2 dpr -- "deparse"
684  
685 Takes a code reference and any optional parameters to pass to L<B::Deparse>.
686 Tees out the result of deparsing this reference.
687  
688 my $foo = sub { print "foo\n" };
689 ...
690 dpr $foo; # what is this mystery code ref?
691  
692 # {
693 # use warnings;
694 # use strict 'refs';
695 # print "foo\n";
696 # }
697  
698 =head2 flt($;$) -- "flatten"
699  
700 Takes a parameter and flattens it. For an ordinary scalar this just
701 means it returns it. For containers -- hash or array references -- it returns
702 copies with flattened values. Anything blessed it stringifies.
703  
704 flt { bar => 1, baz => DateTime->now };
705  
706 # {
707 # 'bar' => 1,
708 # 'baz' => '2014-05-31T21:04:07'
709 # };
710  
711 This is useful for dumping hashes containing huge objects whose innards you
712 don't need to see.
713  
714 If the optional second parameter is provided, the information is only returned,
715 not also dumped out via C<dmp>.
716  
717 =head1 VARIABLES
718  
719 =head3 $ENV{DBG_LOG}
720  
721 If the C<DBG_LOG> environment variable is set and is not equal to 0, this will
722 be understood as the file into which debugging output should be dumped. If it
723 is set to 0, the debugging output will only be sent to STDERR. If it is
724 undefined, the log will be C<~/DBG.log>.
725  
726 =head3 $ENV{DBG_ON}
727  
728 If the C<DBG_ON> environment variable is set, its boolean value will be used to
729 determine the value of C<$DBG::ON>.
730  
731 =head3 $ENV{DBG_HEADER}
732  
733 If the C<DBG_HEADER> environment variable is set, its boolean value will be used to
734 determine the value of C<$DBG::HEADER>.
735  
736 =head3 $DBG::ON
737  
738 If C<$DBG::ON> is true, which it is by default, all debugging code is executed.
739 If it is false, debugging code is ignored (aside from the initial timestamp).
740 The state of C<$ON> can be manipulated programmatically or set by the
741 C<$ENV{DBG_ON}> environment variable. This can be used to constrain debugging
742 output to a particular section of a program. For instance, one may set debugging
743 to off and then locally set it to one within a particular method.
744  
745 sub foo {
746 local $DBG::ON = 1;
747 my self = shift;
748 ...
749 }
750  
751 =head3 $DBG::HEADER
752  
753 Unless C<$DBG::HEADER> is false, a timestamp and process ID will be logged for
754 a debugging process. The header is not printed until the first debugging line
755 is logged, so this need not be set in a BEGIN block.
756  
757 =head1 PRE-COMMIT HOOK
758  
759 You probably don't want debugging code, at least not that associated with
760 DBG, getting into your repository. Here's a sample git pre-commit hook script
761 for screening it out:
762  
763 my $rx = qr/
764 ( (?&line){0,3} (?&dbg) (?&line){0,3} )
765 (?(DEFINE)
766 (?<line> ^.*?(?:\R|\z) )
767 (?<dbg> ^\+\s*use\s+DBG\b.*?(?:\R|\z) )
768 )
769 /mx;
770 my $text = `git diff --staged`;
771 if ( my @matches = $text =~ /$rx/g ) {
772 @matches = grep defined, @matches;
773 exit 0 unless @matches;
774 print STDERR "DBG lines: \n\n" . join "\n", @matches;
775 print STDERR "\nRun with --no-verify if you want to skip the DBG check.\n";
776 print STDERR "Aborting commit.\n";
777 exit 1;
778 }
779 exit 0;
780  
781 =head1 AUTHORS
782  
783 =over 4
784  
785 =item *
786  
787 Grant Street Group <developers@grantstreet.com>
788  
789 =item *
790  
791 David F. Houghton <dfhoughton@gmail.com>
792  
793 =back
794  
795 =head1 COPYRIGHT AND LICENSE
796  
797 This software is copyright (c) 2014 by Grant Street Group.
798  
799 This is free software; you can redistribute it and/or modify it under
800 the same terms as the Perl 5 programming language system itself.
801  
802 =cut
↑ Summary