blib/lib/Devel/StackTrace/AsHTML.pm

filter: Statementstmt 84.0% 84 / 100 Branchbran 58.8% 20 / 34 Conditioncond 60.0% 9 / 15 MC/DCmcdc 33.3% 3 / 9 Subroutinesub 92.3% 12 / 13 Podpod 0.0% 0 / 3 totaltotal 73.5% 128 / 174 CC 26 SCAR 21.8
CC
26
Cov
76%
_build_lexicals
30.0
render
9.6
_build_context
8.1
CRAP 8.9
↑ Summary
1 package Devel::StackTrace::AsHTML;
2  
3 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use 5.008_001;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 our $VERSION = '0.15';
6  
7 3 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use Devel::StackTrace;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use Scalar::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 3 ▶no warnings 'qw';
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 my %enc = qw( & &amp; > &gt; < &lt; " &quot; ' &#39; );
13  
14 # NOTE: because we don't know which encoding $str is in, or even if
15 # $str is a wide character (decoded strings), we just leave the low
16 # bits, including latin-1 range and encode everything higher as HTML
17 # entities. I know this is NOT always correct, but should mostly work
18 # in case $str is encoded in utf-8 bytes or wide chars. This is a
19 # necessary workaround since we're rendering someone else's code which
20 # we can't enforce string encodings.
21  
22 38 ▶sub encode_html {
Subroutine encode_html: called CC=2 SCAR=6.9
Pod undocumented
23 38 my $str = shift;
24 38 ▶ $str =~ s/([^\x00-\x21\x23-\x25\x28-\x3b\x3d\x3f-\xff])/$enc{$1} || '&#' . ord($1) . ';' /ge;
Condition100%2/2
$enc{$1} || '&#' . ord($1) . ';'
l!l
151
MC/DC100%1/1
$enc{$1} || '&#' . ord($1) . ';'
$enc{$1}
Truth table100%2/2
A
A= $enc{$1}
Aresult
11
00
25 38 utf8::downgrade($str);
26 38 $str;
27 }
28  
29 2 ▶sub Devel::StackTrace::as_html {
Subroutine as_html: called CC=1 SCAR=0.0
Pod undocumented
30 2 __PACKAGE__->render(@_);
31 }
32  
33 2 ▶sub render {
Subroutine render: called CC=9 SCAR=22.6
Pod undocumented
34 2 my $class = shift;
35 2 my $trace = shift;
36 2 my %opt = @_;
37  
38 2 my $msg = encode_html($trace->frame(0)->as_string(1));
39 2 my $out = qq{<!doctype html><head><title>Error: ${msg}</title>};
40  
41 2 ▶ $opt{style} ||= \<<STYLE;
Condition50%1/2
$opt{"style"} ||= \"a.toggle { color: #444 }\nbody { margin: 0; padding: 0; background: #fff; color: #000; }\nh1 { margin: 0 0 .5em; padding: .25em .5em .1em 1.5em; border-bottom: thick solid #002; background: #444; color: #eee; font-size: x-large; }\npre.message { margin: .5em 1em; }\nli.frame { font-size: small; margin-top: 3em }\nli.frame:nth-child(1) { margin-top: 0 }\npre.context { border: 1px solid #aaa; padding: 0.2em 0; background: #fff; color: #444; font-size: medium; }\npre .match { color: #000;background-color: #f99; font-weight: bold }\npre.vardump { margin:0 }\npre code strong { color: #000; background: #f88; }\n\ntable.lexicals, table.arguments { border-collapse: collapse }\ntable.lexicals td, table.arguments td { border: 1px solid #000; margin: 0; padding: .3em }\ntable.lexicals tr:nth-child(2n) { background: #DDDDFF }\ntable.arguments tr:nth-child(2n) { background: #DDFFDD }\n.lexicals, .arguments { display: none }\n.variable, .value { font-family: monospace; white-space: pre }\ntd.variable { vertical-align: top }\n"
l!l
02
MC/DC0%0/1
$opt{"style"} ||= \"a.toggle { color: #444 }\nbody { margin: 0; padding: 0; background: #fff; color: #000; }\nh1 { margin: 0 0 .5em; padding: .25em .5em .1em 1.5em; border-bottom: thick solid #002; background: #444; color: #eee; font-size: x-large; }\npre.message { margin: .5em 1em; }\nli.frame { font-size: small; margin-top: 3em }\nli.frame:nth-child(1) { margin-top: 0 }\npre.context { border: 1px solid #aaa; padding: 0.2em 0; background: #fff; color: #444; font-size: medium; }\npre .match { color: #000;background-color: #f99; font-weight: bold }\npre.vardump { margin:0 }\npre code strong { color: #000; background: #f88; }\n\ntable.lexicals, table.arguments { border-collapse: collapse }\ntable.lexicals td, table.arguments td { border: 1px solid #000; margin: 0; padding: .3em }\ntable.lexicals tr:nth-child(2n) { background: #DDDDFF }\ntable.arguments tr:nth-child(2n) { background: #DDFFDD }\n.lexicals, .arguments { display: none }\n.variable, .value { font-family: monospace; white-space: pre }\ntd.variable { vertical-align: top }\n"
$opt{"style"}
Truth table50%1/2
A
A= $opt{"style"}
Aresult
11
00
42 a.toggle { color: #444 }
43 body { margin: 0; padding: 0; background: #fff; color: #000; }
44 h1 { margin: 0 0 .5em; padding: .25em .5em .1em 1.5em; border-bottom: thick solid #002; background: #444; color: #eee; font-size: x-large; }
45 pre.message { margin: .5em 1em; }
46 li.frame { font-size: small; margin-top: 3em }
47 li.frame:nth-child(1) { margin-top: 0 }
48 pre.context { border: 1px solid #aaa; padding: 0.2em 0; background: #fff; color: #444; font-size: medium; }
49 pre .match { color: #000;background-color: #f99; font-weight: bold }
50 pre.vardump { margin:0 }
51 pre code strong { color: #000; background: #f88; }
52  
53 table.lexicals, table.arguments { border-collapse: collapse }
54 table.lexicals td, table.arguments td { border: 1px solid #000; margin: 0; padding: .3em }
55 table.lexicals tr:nth-child(2n) { background: #DDDDFF }
56 table.arguments tr:nth-child(2n) { background: #DDFFDD }
57 .lexicals, .arguments { display: none }
58 .variable, .value { font-family: monospace; white-space: pre }
59 td.variable { vertical-align: top }
60 STYLE
61  
62 2 ▶ if (ref $opt{style}) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $opt{'style'}) { } 2 0 2
63 2 $out .= qq(<style type="text/css">${$opt{style}}</style>);
64 } else {
65 0 $out .= qq(<link rel="stylesheet" type="text/css" href=") . encode_html($opt{style}) . q(" />);
66 }
67  
68 2 $out .= <<HEAD;
69 <script language="JavaScript" type="text/javascript">
70 function toggleThing(ref, type, hideMsg, showMsg) {
71 var css = document.getElementById(type+'-'+ref).style;
72 css.display = css.display == 'block' ? 'none' : 'block';
73  
74 var hyperlink = document.getElementById('toggle-'+ref);
75 hyperlink.textContent = css.display == 'block' ? hideMsg : showMsg;
76 }
77  
78 function toggleArguments(ref) {
79 toggleThing(ref, 'arguments', 'Hide function arguments', 'Show function arguments');
80 }
81  
82 function toggleLexicals(ref) {
83 toggleThing(ref, 'lexicals', 'Hide lexical variables', 'Show lexical variables');
84 }
85 </script>
86 </head>
87 <body>
88 <h1>Error trace</h1><pre class="message">$msg</pre><ol>
89 HEAD
90  
91 2 my $i = 0;
92 2 ▶ while (my $frame = $trace->next_frame) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $frame = $trace->next_frame) 4 2 6
93 4 $i++;
94 4 my $next_frame = $trace->frame($i); # peek next
95 4 ▶ $out .= join(
Branch: 3 branches
BranchTrueFalseTotal
$next_frame && $next_frame->subroutine ? : 2 2 4
$frame->filename ? : 4 0 4
$frame->can('lexicals') ? : 0 4 4
Condition60%3/5
$next_frame and $next_frame->subroutine
!ll&&!rl&&r
202
_build_context($frame) || ''
l!l
40
MC/DC50%1/2
$next_frame and $next_frame->subroutine
$next_frame $next_frame->subroutine
MC/DC0%0/1
_build_context($frame) || ''
_build_context($frame)
Truth table66%2/3
A and B
A= $next_frameB= $next_frame->subroutine
ABresult
0X0
100
111
Truth table50%1/2
A
A= _build_context($frame)
Aresult
11
00
96 '',
97 '<li class="frame">',
98 ($next_frame && $next_frame->subroutine) ? encode_html("in " . $next_frame->subroutine) : '',
99 ' at ',
100 $frame->filename ? encode_html($frame->filename) : '',
101 ' line ',
102 $frame->line,
103 q(<pre class="context"><code>),
104 _build_context($frame) || '',
105 q(</code></pre>),
106 _build_arguments($i, $next_frame),
107 $frame->can('lexicals') ? _build_lexicals($i, $frame->lexicals) : '',
108 q(</li>),
109 );
110 }
111 2 $out .= qq{</ol>};
112 2 $out .= "</body></html>";
113  
114 2 $out;
115 }
116  
117 2 ▶my $dumper = sub {
Subroutine __ANON__: called CC=3 SCAR=11.6
118 2 my $value = shift;
119 2 ▶ $value = $$value if ref $value eq 'SCALAR' or ref $value eq 'REF';
Branch: 1 branch
BranchTrueFalseTotal
if ref $value eq "SCALAR" or ref $value eq "REF" 0 2 2
Condition33%1/3
ref $value eq "SCALAR" or ref $value eq "REF"
l!l&&r!l&&!r
002
MC/DC0%0/2
ref $value eq "SCALAR" or ref $value eq "REF"
ref $value eq "SCALAR" ref $value eq "REF"
Truth table33%1/3
A or B
A= ref $value eq "SCALAR"B= ref $value eq "REF"
ABresult
1X1
011
000
120 2 my $d = Data::Dumper->new([ $value ]);
121 2 $d->Indent(1)->Terse(1)->Deparse(1);
122 2 chomp(my $dump = $d->Dump);
123 2 $dump;
124 3 };
125  
126 4 ▶sub _build_arguments {
Subroutine _build_arguments: called CC=4 SCAR=13.9
127 4 my($id, $frame) = @_;
128 4 my $ref = "arg-$id";
129  
130 4 ▶ return '' unless $frame && $frame->args;
Branch: 1 branch
BranchTrueFalseTotal
unless $frame and $frame->args 2 2 4
Condition66%2/3
$frame and $frame->args
!ll&&!rl&&r
202
MC/DC50%1/2
$frame and $frame->args
$frame $frame->args
Truth table66%2/3
A and B
A= $frameB= $frame->args
ABresult
0X0
100
111
131  
132 2 my @args = $frame->args;
133  
134 2 my $html = qq(<p><a class="toggle" id="toggle-$ref" href="javascript:toggleArguments('$ref')">Show function arguments</a></p><table class="arguments" id="arguments-$ref">);
135  
136 # Don't use while each since Dumper confuses that
137 2 for my $idx (0 .. @args - 1) {
138 2 my $value = $args[$idx];
139 2 my $dump = $dumper->($value);
140 2 $html .= qq{<tr>};
141 2 $html .= qq{<td class="variable">\$_[$idx]</td>};
142 2 $html .= qq{<td class="value">} . encode_html($dump) . qq{</td>};
143 2 $html .= qq{</tr>};
144 }
145 2 $html .= qq(</table>);
146  
147 2 return $html;
148 }
149  
150 0 ▶sub _build_lexicals {
Subroutine _build_lexicals: not called CC=5 SCAR=34.0
151 0 my($id, $lexicals) = @_;
152 0 my $ref = "lex-$id";
153  
154 0 ▶ return '' unless keys %$lexicals;
Branch: 1 branch
BranchTrueFalseTotal
unless keys %$lexicals 0 0 0
155  
156 0 my $html = qq(<p><a class="toggle" id="toggle-$ref" href="javascript:toggleLexicals('$ref')">Show lexical variables</a></p><table class="lexicals" id="lexicals-$ref">);
157  
158 # Don't use while each since Dumper confuses that
159 0 for my $var (sort keys %$lexicals) {
160 0 my $value = $lexicals->{$var};
161 0 my $dump = $dumper->($value);
162 0 ▶ $dump =~ s/^\{(.*)\}$/($1)/s if $var =~ /^\%/;
Branch: 1 branch
BranchTrueFalseTotal
if $var =~ /^\%/ 0 0 0
163 0 ▶ $dump =~ s/^\[(.*)\]$/($1)/s if $var =~ /^\@/;
Branch: 1 branch
BranchTrueFalseTotal
if $var =~ /^\@/ 0 0 0
164 0 $html .= qq{<tr>};
165 0 $html .= qq{<td class="variable">} . encode_html($var) . qq{</td>};
166 0 $html .= qq{<td class="value">} . encode_html($dump) . qq{</td>};
167 0 $html .= qq{</tr>};
168 }
169 0 $html .= qq(</table>);
170  
171 0 return $html;
172 }
173  
174 4 ▶sub _build_context {
Subroutine _build_context: called CC=8 SCAR=20.9
175 4 my $frame = shift;
176 4 my $file = $frame->filename;
177 4 my $linenum = $frame->line;
178 4 my $code;
179 4 ▶ if (-f $file) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $file) 4 0 4
180 4 my $start = $linenum - 3;
181 4 my $end = $linenum + 3;
182 4 ▶ $start = $start < 1 ? 1 : $start;
Branch: 1 branch
BranchTrueFalseTotal
$start < 1 ? : 0 4 4
183 4 ▶ open my $fh, '<', $file
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $file or die "cannot open ${file}:$!" 4 0 4
184 or die "cannot open $file:$!";
185 4 my $cur_line = 0;
186 4 ▶ while (my $line = <$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $fh)) 54 0 54
187 54 ++$cur_line;
188 54 ▶ last if $cur_line > $end;
Branch: 1 branch
BranchTrueFalseTotal
if $cur_line > $end 4 50 54
189 50 ▶ next if $cur_line < $start;
Branch: 1 branch
BranchTrueFalseTotal
if $cur_line < $start 22 28 50
190 28 $line =~ s|\t| |g;
191 28 ▶ my @tag = $cur_line == $linenum
Branch: 1 branch
BranchTrueFalseTotal
$cur_line == $linenum ? : 4 24 28
192 ? (q{<strong class="match">}, '</strong>')
193 : ('', '');
194 28 $code .= sprintf(
195 '%s%5d: %s%s', $tag[0], $cur_line, encode_html($line),
196 $tag[1],
197 );
198 }
199 4 close $file;
200 }
201 4 return $code;
202 }
203  
204 3 1;
205 __END__
↑ Summary