blib/lib/Debug/Show.pm

filter: Statementstmt 100% 80 / 80 Branchbran 81.2% 13 / 16 Conditioncond 37.5% 3 / 8 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 92.3% 12 / 13 Podpod 100% 2 / 2 totaltotal 89.4% 110 / 123 CC 12 SCAR 12.5
CC
12
Cov
92%
__ANON__
6.0
import
5.8
debug_show
3.0
CRAP 3.5
↑ Summary
1 =head1 NAME
2  
3 Debug::Show - display variables helpfully for debugging
4  
5 =head1 SYNOPSIS
6  
7 use Debug::Show qw(debug=hide); # normally
8 use Debug::Show qw(debug=show); # while debugging
9  
10 debug $foo, $bar->{baz};
11  
12 =head1 DESCRIPTION
13  
14 This module provides a facility for displaying variable values for
15 debugging purposes. Statements in the code determine what values are
16 displayed. Whether the statements actually cause debugging output depends
17 on the manner in which C<Debug::Show> was invoked, so the debug statements
18 can remain permanently in the code, normally inactive. When inactive,
19 the debug statements impose no runtime overhead.
20  
21 When the debug statements are active, each value displayed is labelled
22 with the expression used to generate it. This saves the bother of
23 manually applying labels.
24  
25 =cut
26  
27 package Debug::Show;
28  
29 3 ▶{ use 5.006; }
Subroutine BEGIN: called CC=1 SCAR=0.0
30 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
31 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
32  
33 3 ▶use B::CallChecker 0.000 qw(cv_set_call_checker ck_entersub_args_proto);
Subroutine BEGIN: called CC=1 SCAR=0.0
34 3 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
35 # B::Generate provides a broken version of B::COP->warnings, which
36 # makes B::Deparse barf [rt.cpan.org #70396], and of B::SVOP->sv,
37 # which makes B::Concise emit rubbish [rt.cpan.org #70398].
38 # This works around it by restoring the non-broken versions,
39 # provided that B::Generate hasn't already been loaded. If it
40 # was loaded by someone else, better hope they worked around it
41 # the same way.
42 3 require B;
43 3 my $cop_warnings = \&B::COP::warnings;
44 3 my $svop_sv = \&B::SVOP::sv;
45 3 require B::Generate;
46 3 ▶ no warnings "redefine";
Subroutine BEGIN: called CC=1 SCAR=0.0
47 3 *B::COP::warnings = $cop_warnings;
48 3 *B::SVOP::sv = $svop_sv;
49 3 B::Generate->VERSION(1.33);
50 }
51 3 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
52  
53 3 our $VERSION = "0.000";
54  
55 =head1 FUNCTIONS
56  
57 These functions are not exported in the normal way. See below for how
58 to import. The functions may be referenced directly by fully qualified
59 name (e.g., C<Debug::Show::debug_show>).
60  
61 =over
62  
63 =item debug_show(EXPR, ...)
64  
65 Display (via C<warn>) the values of all the argument expressions.
66 There may be any number of argument expressions. Each value is deeply
67 serialised (by means of C<Data::Dumper>), and is labelled with source
68 for the expression that evaluated to it (generated by C<B::Deparse>).
69 All the expresssions are evaluated in scalar context, so say C<\%foo>
70 rather than C<%foo> if you want to display the contents of a hash.
71 The entire display consists of a single line.
72  
73 =cut
74  
75 3 my $dumper_initialised;
76 11 ▶sub debug_show {
Subroutine debug_show: called CC=3 SCAR=11.0
Pod documented
77 11 ▶ unless($dumper_initialised) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($dumper_initialised) 1 10 11
78 1 require Data::Dumper;
79 1 Data::Dumper->VERSION(2.11);
80 1 $dumper_initialised = 1;
81 }
82 11 my @part = ("###");
83 11 ▶ while(@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 14 11 25
84 14 my $label = shift(@_);
85 14 my $value = shift(@_);
86 14 my $dumper = Data::Dumper->new([$value]);
87 14 $dumper->Terse(1);
88 14 $dumper->Indent(0);
89 14 $dumper->Useqq(1);
90 14 $dumper->Quotekeys(0);
91 14 $dumper->Sortkeys(1);
92 14 push @part, " ", $label, " = ", $dumper->Dump, ";";
93 }
94 11 push @part, "\n";
95 11 warn join("", @part);
96 }
97  
98 my $deparser_initialised;
99 7 ▶cv_set_call_checker(\&debug_show, sub ($$$) {
Subroutine __ANON__: called CC=6 SCAR=18.0
100 7 my($entersubop, $namegv, undef) = @_;
101 7 ▶ unless($deparser_initialised) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($deparser_initialised) 1 6 7
102 1 require B::Compiling;
103 1 B::Compiling->VERSION(0.01);
104 1 require B::Deparse;
105 1 B::Deparse->VERSION(0.64);
106 1 $deparser_initialised = 1;
107 }
108 7 my $deparser = B::Deparse->new;
109 # Beware, this knows too much about B::Deparse internals.
110 # Would prefer to have a proper interface to parse ops in
111 # (relative) isolation.
112 7 $deparser->{curcv} = $entersubop->find_cv;
113 7 $deparser->{curcop} = B::Compiling::PL_compiling();
114 7 my $foreop = $entersubop->first;
115 7 ▶ $foreop = $foreop->first if $foreop->sibling->isa("B::NULL");
Branch: 1 branch
BranchTrueFalseTotal
if $foreop->sibling->isa("B::NULL") 7 0 7
116 7 my $n = 0;
117 7 ▶ until((my $argop = $foreop->sibling)->sibling->isa("B::NULL")) {
Branch: 1 branch
BranchTrueFalseTotal
unless ((my $argop = $foreop->sibling)->sibling->isa("B::NULL")) 9 7 16
118 9 ▶ my $expr = eval {
Condition50%1/2
eval { do { local $SIG{'__DIE__'}; $deparser->indent($deparser->deparse($argop, 50)) } } || q['???']
l!l
90
MC/DC0%0/1
eval { do { local $SIG{'__DIE__'}; $deparser->indent($deparser->deparse($argop, 50)) } } || q['???']
eval { do { local $SIG{'__DIE__'}; $deparser->indent($deparser->deparse($argop, 50)) } }
Truth table50%1/2
A
A= eval { do { local $SIG{'__DIE__'}; $deparser->indent($deparser->deparse($argop, 50)) } }
Aresult
11
00
119 9 local $SIG{__DIE__};
120 # The 50 here is a precedence value. This is a
121 # very high precedence, forcing any non-atomic
122 # expression to be parenthesised.
123 9 $deparser->indent($deparser->deparse($argop, 50));
124 } || "'???'";
125 9 ▶ print $@ if $@ ne "";
Branch: 1 branch
BranchTrueFalseTotal
if $@ ne "" 0 9 9
126 9 $expr =~ s/\n[\t ]*/ /g;
127 9 my $exprop = B::SVOP->new("const", 0, $expr);
128 9 $exprop->sibling($argop);
129 9 $foreop->sibling($exprop);
130 9 $foreop = $argop;
131 9 $n++;
132 }
133 7 return ck_entersub_args_proto($entersubop, $namegv, \("\$\$"x$n));
134 3 }, \!1);
135  
136 =item debug_hide(EXPR, ...)
137  
138 Do nothing. Calls to this function are excised at compile time, so
139 there is no overhead from evaluating the argument expressions or calling
140 the subroutine.
141  
142 =cut
143  
144 0 ▶sub debug_hide { }
Subroutine debug_hide: not called CC=1 SCAR=0.0
Pod documented
145  
146 8 ▶cv_set_call_checker(\&debug_hide, sub ($$$) {
Subroutine __ANON__: called CC=1 SCAR=0.0
147 8 my($entersubop, undef, undef) = @_;
148 # B::Generate doesn't offer a way to explicitly free ops.
149 # We ought to be able to implicitly free $entersubop via constant
150 # folding, by something like
151 #
152 # return B::LOGOP->new("and", 0,
153 # B::SVOP->new("const", 0, !1),
154 # $entersubop);
155 #
156 # but empirically that causes memory corruption and it's not
157 # clear why. For the time being, leak $entersubop.
158 8 return B::SVOP->new("const", 0, !1);
159 3 }, \!1);
160  
161 =back
162  
163 =head1 PACKAGE METHOD
164  
165 This method is meant to be invoked on the C<Debug::Show> package.
166 It will normally be accessed through the C<use> facility.
167  
168 =over
169  
170 =item Debug::Show->import("debug=show")
171  
172 Puts the subroutine L</debug_show> into the caller's namespace under
173 the name "C<debug>".
174  
175 =item Debug::Show->import("debug=hide")
176  
177 Puts the subroutine L</debug_hide> into the caller's namespace under
178 the name "C<debug>".
179  
180 =cut
181  
182 2 ▶sub import {
Subroutine import: called CC=5 SCAR=17.5
183 2 ▶ croak "bad importation from $_[0]"
Branch: 1 branch
BranchTrueFalseTotal
unless @_ == 2 and ref $_[1] eq "" and $_[1] =~ /\Adebug=(?:show|hide)\z/ 0 2 2
Condition33%2/6
@_ == 2 and ref $_[1] eq "" and $_[1] =~ /\Adebug=(?:show|hide)\z/
!ll&&!rl&&r
002
@_ == 2 and ref $_[1] eq ""
!ll&&!rl&&r
002
MC/DC0%0/3
@_ == 2 and ref $_[1] eq "" and $_[1] =~ /\Adebug=(?:show|hide)\z/
@_ == 2 ref $_[1] eq "" $_[1] =~ /\Adebug=(?:show|hide)\z/
Truth table25%1/4
A and B and C
A= @_ == 2B= ref $_[1] eq ""C= $_[1] =~ /\Adebug=(?:show|hide)\z/
ABCresult
0XX0
10X0
1100
1111
184 unless @_ == 2 && ref($_[1]) eq "" &&
185 $_[1] =~ /\Adebug=(?:show|hide)\z/;
186 3 ▶ no strict "refs";
Subroutine BEGIN: called CC=1 SCAR=0.0
187 2 ▶ *{caller(0)."::debug"} =
Branch: 1 branch
BranchTrueFalseTotal
$_[1] eq 'debug=show' ? : 1 1 2
188 $_[1] eq "debug=show" ? \&debug_show : \&debug_hide;
189 }
190  
191 =back
192  
193 =head1 BUGS
194  
195 The operation of this module depends on L<B::Generate>. That module has
196 been found to interact badly with other C<B::> modules in some cases.
197 This module includes workarounds for known bugs, but others may lurk.
198  
199 Because the expression source in the output is generated by
200 L<B::Deparse>, it cannot be expected to match the original source
201 character-for-character. It will normally be equivalent source. In some
202 obscure cases the deparser generates incorrect output; that is a bug
203 in L<B::Deparse>. The kinds of expression that confuse the deparser
204 are relatively unlikely to occur with expressions being displayed for
205 debugging.
206  
207 The shenanigans that take place with the debug functions at compile time
208 will make L<B::Deparse> produce incorrect output for the debug statements.
209  
210 When hiding debug output, the ops corresponding to the debug expressions,
211 which ought to be freed, are instead leaked. This is because freeing
212 them has been observed to cause memory corruption. The cause of this
213 is currently unknown. The leakage should have negligible impact, unless
214 debug statements occur in code that is repeatedly generated dynamically.
215  
216 =head1 SEE ALSO
217  
218 L<B::Deparse>,
219 L<Data::Dumper>
220  
221 =head1 AUTHOR
222  
223 Andrew Main (Zefram) <zefram@fysh.org>
224  
225 =head1 COPYRIGHT
226  
227 Copyright (C) 2011 Andrew Main (Zefram) <zefram@fysh.org>
228  
229 =head1 LICENSE
230  
231 This module is free software; you can redistribute it and/or modify it
232 under the same terms as Perl itself.
233  
234 =cut
235  
236 3 1;
↑ Summary