blib/lib/BFD.pm

filter: Statementstmt 71.1% 32 / 45 Branchbran 35.7% 5 / 14 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 75.0% 9 / 12 Podpod 0.0% 0 / 5 totaltotal 60.5% 46 / 76 CC 8 SCAR 19.6
CC
8
Cov
63%
format_msg
15.1
dump_ref
2.0
d_to
2.0
CRAP 7.1
↑ Summary foo.in »
1 package BFD;
2  
3 1 $VERSION = 0.31;
4  
5 =head1 NAME
6  
7 BFD - Impromptu dumping of data structures for debugging purposes
8  
9 =head1 SYNOPSIS
10  
11 my $scary_structure1 = foo();
12 my $scary_structure2 = bar();
13 use BFD; d $scary_structure1, " hmmm ", $scary_structure2, ...;
14 ....
15  
16 =head1 DESCRIPTION
17  
18 Allows for impromptu dumping of output to STDERR. Useful when you want
19 to take a peek at a nest Perl data structure by emitting (relatively)
20 nicely formatted output with filename and line number prefixed to each line.
21  
22 Basically,
23  
24 use BFD;d $foo;
25  
26 is shorthand for
27  
28 use Data::Dumper;
29 local $Data::Dumper::Indent = 1;
30 local $Data::Dumper::Quotekeys = 0;
31 local $Data::Dumper::Terse = 1;
32 local $Data::Dumper::Sortkeys = 1;
33 my $msg = Dumper( $foo );
34 $msg =~ s/^/$where: /mg;
35 warn $msg;
36  
37 I use this incantation soooo often that a TLA version is warranted.
38 YMMV.
39  
40 =cut
41  
42 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
43 1 ▶use Cwd qw( cwd );
Subroutine BEGIN: called CC=1 SCAR=0.0
44 1 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
45  
46 1 ▶sub import {
Subroutine import: called CC=1 SCAR=0.0
47 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
48 1 *{caller() . "::d"} = \&d;
49 }
50  
51  
52 0 ▶sub dump_ref {
Subroutine dump_ref: not called CC=1 SCAR=6.9
Pod undocumented
53 0 require Data::Dumper;
54 0 local $Data::Dumper::Indent = 1;
55 0 local $Data::Dumper::Quotekeys = 0;
56 0 local $Data::Dumper::Terse = 1;
57 0 local $Data::Dumper::Sortkeys = 1;
58 0 Data::Dumper::Dumper( @_ )
59 }
60  
61  
62 1 my $start_dir; ## Captured at compile time to use for shortening prefixes
63 1 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
64 1 $start_dir = cwd;
65 };
66  
67 1 ▶use vars qw( $LineNumberWidth );
Subroutine BEGIN: called CC=1 SCAR=0.0
68  
69 1 $LineNumberWidth = 4;
70  
71 1 ▶sub format_msg {
Subroutine format_msg: called CC=8 SCAR=27.2
Pod undocumented
72 1 my ( $fn, $ln ) = ( shift, shift );
73  
74 ## Line number fields never get narrower so that you don't
75 ## get output that's all jaggy.
76 1 ▶ $LineNumberWidth = length $ln if length $ln > $LineNumberWidth;
Branch: 1 branch
BranchTrueFalseTotal
if length $ln > $BFD::LineNumberWidth 0 1 1
77  
78 1 ▶ if ( File::Spec->file_name_is_absolute( $fn ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ("File::Spec"->file_name_is_absolute($fn)) 0 1 1
79 0 ▶ if ( $fn =~ s/.*\b(site_perl)\b/$1/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fn =~ s/.*\b(site_perl)\b/$1/) { } 0 0 0
80 ## Should use Config.pm's list of perl dirs, but hack for now
81 }
82 else {
83 0 my $rel_fn = File::Spec->abs2rel( $fn, $start_dir );
84 0 ▶ if ( 0 == index $rel_fn, File::Spec->updir ) {
Branch: 1 branch
BranchTrueFalseTotal
if (0 == index($rel_fn, "File::Spec"->updir)) 0 0 0
85 0 $fn = $rel_fn;
86 }
87 }
88 }
89  
90  
91 1 my $where = sprintf "%s, %${LineNumberWidth}d:", $fn, $ln;
92  
93 my $msg = join "", map {
94 1 ( my $out = $_ ) =~ s/^/$where/gm;
95 1 ▶ $out;
Branch: 2 branches
BranchTrueFalseTotal
!defined($_) ? : 0 1 1
ref $_ ? : 0 1 1
96 } join "", map
97 ! defined $_ ? "undef"
98 : ref $_ ? dump_ref $_
99 : $_,
100 @_;
101  
102 1 ▶ 1 while chomp $msg;
Branch: 1 branch
BranchTrueFalseTotal
chomp $msg and do { '???' } 0 1 1
103 1 return $msg;
104 }
105  
106  
107 1 ▶sub d {
Subroutine d: called CC=1 SCAR=0.0
Pod undocumented
108 1 warn format_msg( (caller)[1,2], @_ );
109 }
110  
111  
112 0 ▶sub d_to {
Subroutine d_to: not called CC=1 SCAR=6.9
Pod undocumented
113 0 my $fh = shift;
114 0 print $fh format_msg( (caller)[1,2], @_ );
115 }
116  
117  
118 0 ▶sub d_to_string {
Subroutine d_to_string: not called CC=1 SCAR=6.9
Pod undocumented
119 0 format_msg( (caller)[1,2], @_ );
120 }
121  
122  
123 =head1 LIMITATIONS
124  
125 Uses Data::Dumper, which has varying degrees of stability and usefulness
126 on different versions of perl.
127  
128 =head1 AUTHOR
129  
130 Barrie Slaymaker <barries@slaysys.com>
131  
132 =head1 COPYRIGHT
133  
134 Copyright (c) 2003, Barrie Slaymaker. All Rights Reserved.
135  
136 =head1 LICENSE
137  
138 You may use this software under the terms of the GNU Public License, the
139 Artistic License, the BSD license, or the MIT license.
140  
141 Good luck and God Speed.
142  
143 =cut
144  
145 1 1 ;
↑ Summary foo.in »