Ex/Dump.pm

filter: Statementstmt 18.8% 19 / 101 Branchbran 0.0% 0 / 40 Conditioncond 0.0% 0 / 25 MC/DCmcdc 0.0% 0 / 12 Subroutinesub 29.4% 5 / 17 Podpod 70.0% 7 / 10 totaltotal 15.1% 31 / 205 CC 34 SCAR 56.2
CC
34
Cov
11%
_what_is_this
552.0
ctrace
110.0
__ANON__
6.0
CRAP 276.8
« Ex/Die.pm ↑ Summary Ex/Conf.pm »
1 package CGI::Ex::Dump;
2  
3 =head1 NAME
4  
5 CGI::Ex::Dump - A debug utility
6  
7 =head1 VERSION
8  
9 version 2.54
10  
11 =cut
12  
13 ###----------------------------------------------------------------###
14 # Copyright - Paul Seamons #
15 # Distributed under the Perl Artistic License without warranty #
16 ###----------------------------------------------------------------###
17  
18 6 ▶use vars qw($CALL_LEVEL $ON $SUB $QR1 $QR2 $full_filename $DEPARSE);
Subroutine BEGIN: called CC=1 SCAR=0.0
19 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 6 ▶use Exporter qw(import);
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 6 our $VERSION = '2.54'; # VERSION
23 6 our @EXPORT = qw(dex dex_warn dex_text dex_html ctrace dex_trace);
24 6 our @EXPORT_OK = qw(dex dex_warn dex_text dex_html ctrace dex_trace debug caller_trace);
25  
26 ### is on or off
27 6 ▶sub on { $ON = 1 };
Subroutine on: called CC=1 SCAR=0.0
Pod documented
28 0 ▶sub off { $ON = 0; }
Subroutine off: not called CC=1 SCAR=6.9
Pod documented
29  
30 0 ▶sub set_deparse { $DEPARSE = 1 }
Subroutine set_deparse: not called CC=1 SCAR=6.9
Pod undocumented
31  
32 ###----------------------------------------------------------------###
33  
34 BEGIN {
35 6 ▶ on();
Subroutine BEGIN: called CC=1 SCAR=3.3
36  
37 $SUB = sub {
38 ### setup the Data::Dumper usage
39 0 ▶ local $Data::Dumper::Deparse = $DEPARSE && eval {require B::Deparse};
Subroutine __ANON__: not called CC=2 SCAR=17.9
Condition0%0/3
$CGI::Ex::Dump::DEPARSE && eval { do { require B::Deparse } }
!ll&&!rl&&r
000
MC/DC0%0/2
$CGI::Ex::Dump::DEPARSE && eval { do { require B::Deparse } }
$CGI::Ex::Dump::DEPARSE eval { do { require B::Deparse } }
Truth table0%0/3
A && B
A= $CGI::Ex::Dump::DEPARSEB= eval { do { require B::Deparse } }
ABresult
0X0
100
111
40 0 local $Data::Dumper::Pad = ' ';
41 0 local $Data::Dumper::Sortkeys = 1;
42 0 local $Data::Dumper::Useqq = 1;
43 0 local $Data::Dumper::Quotekeys = 0;
44  
45 0 require Data::Dumper;
46 0 return Data::Dumper->Dumpperl(\@_);
47 6 };
48  
49 ### how to display or parse the filename
50 6 $QR1 = qr{\A(?:/[^/]+){2,}/(?:perl|lib)/(.+)\Z};
51 6 $QR2 = qr{\A.+?([\w\.\-]+/[\w\.\-]+)\Z};
52 }
53  
54 ###----------------------------------------------------------------###
55  
56  
57 ### same as dumper but with more descriptive output and auto-formatting
58 ### for cgi output
59 sub _what_is_this {
60 0 ▶ return if ! $ON;
Subroutine _what_is_this: not called CC=23 SCAR=63.1
Branch: 1 branch
BranchTrueFalseTotal
unless $CGI::Ex::Dump::ON 0 0 0
61 ### figure out which sub we called
62 0 ▶ my ($pkg, $file, $line_n, $called) = caller(1 + ($CALL_LEVEL || 0));
Condition0%0/2
$CGI::Ex::Dump::CALL_LEVEL || 0
l!l
00
MC/DC0%0/1
$CGI::Ex::Dump::CALL_LEVEL || 0
$CGI::Ex::Dump::CALL_LEVEL
Truth table0%0/2
A
A= $CGI::Ex::Dump::CALL_LEVEL
Aresult
11
00
63 0 substr($called, 0, length(__PACKAGE__) + 2, '');
64  
65 ### get the actual line
66 0 my $line = '';
67 0 ▶ if (open(IN,$file)) {
Branch: 1 branch
BranchTrueFalseTotal
if (open IN, $file) 0 0 0
68 0 $line = <IN> for 1 .. $line_n;
69 0 close IN;
70 }
71  
72 ### get rid of extended filename
73 0 ▶ if (! $full_filename) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($CGI::Ex::Dump::full_filename) 0 0 0
74 0 ▶ $file =~ s/$QR1/$1/ || $file =~ s/$QR2/$1/;
Branch: 1 branch
BranchTrueFalseTotal
$file =~ s/$CGI::Ex::Dump::QR1/$1/ or $file =~ s/$CGI::Ex::Dump::QR2/$1/ 0 0 0
75 }
76  
77 ### dump it out
78 0 my @dump = map {&$SUB($_)} @_;
79 0 my @var = ('$VAR') x ($#dump + 1);
80 0 my $hold;
81 0 ▶ if ($line =~ s/^ .*\b \Q$called\E ( \s* \( \s* | \s+ )//x
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ s/^ .*\b \Q$called\E ( \s* \( \s* | \s+ )//x and $hold = $1 and $line =~ s/ \s* \b if \b .* \n? $ //x || $line =~ s/ \s* ; \s* $ //x || $line =~ s/ \s+ $ //x) 0 0 0
Condition0%0/12
$line =~ s/^ .*\b \Q$called\E ( \s* \( \s* | \s+ )//x and $hold = $1 and $line =~ s/ \s* \b if \b .* \n? $ //x || $line =~ s/ \s* ; \s* $ //x || $line =~ s/ \s+ $ //x
!ll&&!rl&&r
000
$line =~ s/^ .*\b \Q$called\E ( \s* \( \s* | \s+ )//x and $hold = $1
!ll&&!rl&&r
000
$line =~ s/ \s* \b if \b .* \n? $ //x or $line =~ s/ \s* ; \s* $ //x or $line =~ s/ \s+ $ //x
l!l&&r!l&&!r
000
$line =~ s/ \s* \b if \b .* \n? $ //x or $line =~ s/ \s* ; \s* $ //x
l!l&&r!l&&!r
000
MC/DC0%0/5
$line =~ s/^ .*\b \Q$called\E ( \s* \( \s* | \s+ )//x and $hold = $1 and $line =~ s/ \s* \b if \b .* \n? $ //x || $line =~ s/ \s* ; \s* $ //x || $line =~ s/ \s+ $ //x
$line =~ s/^ .*\b \Q$called\E ( \s* \( \s* | \s+ )//x $hold = $1 $line =~ s/ \s* \b if \b .* \n? $ //x $line =~ s/ \s* ; \s* $ //x $line =~ s/ \s+ $ //x
Truth table0%0/6
A and B and C or D or E
A= $line =~ s/^ .*\b \Q$called\E ( \s* \( \s* | \s+ )//xB= $hold = $1C= $line =~ s/ \s* \b if \b .* \n? $ //xD= $line =~ s/ \s* ; \s* $ //xE= $line =~ s/ \s+ $ //x
ABCDEresult
0XXXX0
10XXX0
110000
111XX1
1101X1
110011
82 && ($hold = $1)
83 && ( $line =~ s/ \s* \b if \b .* \n? $ //x
84 || $line =~ s/ \s* ; \s* $ //x
85 || $line =~ s/ \s+ $ //x)) {
86 0 ▶ $line =~ s/ \s*\) $ //x if $hold =~ /^\s*\(/;
Branch: 1 branch
BranchTrueFalseTotal
if $hold =~ /^\s*\(/ 0 0 0
87 0 ▶ my @_var = map {/^[\"\']/ ? 'String' : $_} split (/\s*,\s*/, $line);
Branch: 1 branch
BranchTrueFalseTotal
/^[\"\']/ ? : 0 0 0
88 0 ▶ @var = @_var if $#var == $#_var;
Branch: 1 branch
BranchTrueFalseTotal
if $#var == $#_var 0 0 0
89 }
90  
91 ### spit it out
92 0 ▶ if ($called eq 'dex_text'
Branch: 1 branch
BranchTrueFalseTotal
if ($called eq 'dex_text' or $called eq 'dex_warn' or not $ENV{'REQUEST_METHOD'}) { } 0 0 0
Condition0%0/6
$called eq "dex_text" or $called eq "dex_warn" or not $ENV{"REQUEST_METHOD"}
l!l&&r!l&&!r
000
$called eq "dex_text" or $called eq "dex_warn"
l!l&&r!l&&!r
000
MC/DC0%0/3
$called eq "dex_text" or $called eq "dex_warn" or not $ENV{"REQUEST_METHOD"}
$called eq "dex_text" $called eq "dex_warn" not $ENV{"REQUEST_METHOD"}
Truth table0%0/4
A or B or not(C)
A= $called eq "dex_text"B= $called eq "dex_warn"C= not $ENV{"REQUEST_METHOD"}
ABCresult
1XX1
01X1
0011
0000
93 || $called eq 'dex_warn'
94 || ! $ENV{REQUEST_METHOD}) {
95 0 my $txt = "$called: $file line $line_n\n";
96 0 for (0 .. $#dump) {
97 0 $dump[$_] =~ s|\$VAR1|$var[$_]|g;
98 0 $txt .= $dump[$_];
99 }
100 0 ▶ if ($called eq 'dex_text') { return $txt }
Branch: 2 branches
BranchTrueFalseTotal
if ($called eq 'dex_text') { } 0 0 0
elsif ($called eq 'dex_warn') { } 0 0 0
101 0 elsif ($called eq 'dex_warn') { warn $txt }
102 0 else { print $txt }
103 } else {
104 0 my $html = "<pre class=debug><span class=debughead><b>$called: $file line $line_n</b></span>\n";
105 0 for (0 .. $#dump) {
106 0 $dump[$_] =~ s/(?<!\\)\\n/\n/g;
107 0 $dump[$_] = _html_quote($dump[$_]);
108 0 $dump[$_] =~ s|\$VAR1|<span class=debugvar><b>$var[$_]</b></span>|g;
109 0 $html .= $dump[$_];
110 }
111 0 $html .= "</pre>\n";
112 0 ▶ return $html if $called eq 'dex_html';
Branch: 1 branch
BranchTrueFalseTotal
if $called eq "dex_html" 0 0 0
113 0 require CGI::Ex;
114 0 CGI::Ex::print_content_type();
115 0 print $html;
116 }
117 0 return @_[0..$#_];
118 }
119  
120 ### some aliases
121 0 ▶sub debug { &_what_is_this }
Subroutine debug: not called CC=1 SCAR=6.9
Pod documented
122 0 ▶sub dex { &_what_is_this }
Subroutine dex: not called CC=1 SCAR=6.9
Pod documented
123 0 ▶sub dex_warn { &_what_is_this }
Subroutine dex_warn: not called CC=1 SCAR=6.9
Pod documented
124 0 ▶sub dex_text { &_what_is_this }
Subroutine dex_text: not called CC=1 SCAR=6.9
Pod documented
125 0 ▶sub dex_html { &_what_is_this }
Subroutine dex_html: not called CC=1 SCAR=6.9
Pod undocumented
126  
127 sub _html_quote {
128 0 ▶ my $value = shift;
Subroutine _html_quote: not called CC=2 SCAR=17.9
129 0 ▶ return '' if ! defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 0 0 0
130 0 $value =~ s/&/&amp;/g;
131 0 $value =~ s/</&lt;/g;
132 0 $value =~ s/>/&gt;/g;
133 # $value =~ s/\"/&quot;/g;
134 0 return $value;
135 }
136  
137 ### ctrace is intended for work with perl 5.8 or higher's Carp
138 sub ctrace {
139 0 ▶ require 5.8.0;
Subroutine ctrace: not called CC=10 SCAR=47.0
Pod documented
140 0 require Carp::Heavy;
141 0 local $Carp::MaxArgNums = 3;
142 0 local $Carp::MaxArgLen = 20;
143 0 ▶ my $i = shift || 0;
Condition0%0/2
shift() || 0
l!l
00
MC/DC0%0/1
shift() || 0
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
144 0 my @i = ();
145 0 my $max1 = 0;
146 0 my $max2 = 0;
147 0 my $max3 = 0;
148 0 ▶ while (my %i = Carp::caller_info(++$i)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(%i) = Carp::caller_info(++$i)) 0 0 0
149 0 $i{sub_name} =~ s/\((.*)\)$//;
150 0 ▶ $i{args} = $i{has_args} ? $1 : "";
Branch: 1 branch
BranchTrueFalseTotal
$i{'has_args'} ? : 0 0 0
151 0 $i{sub_name} =~ s/^.*?([^:]+)$/$1/;
152 0 ▶ $i{file} =~ s/$QR1/$1/ || $i{file} =~ s/$QR2/$1/;
Branch: 1 branch
BranchTrueFalseTotal
$i{"file"} =~ s/$CGI::Ex::Dump::QR1/$1/ or $i{"file"} =~ s/$CGI::Ex::Dump::QR2/$1/ 0 0 0
153 0 ▶ $max1 = length($i{sub_name}) if length($i{sub_name}) > $max1;
Branch: 1 branch
BranchTrueFalseTotal
if length $i{"sub_name"} > $max1 0 0 0
154 0 ▶ $max2 = length($i{file}) if length($i{file}) > $max2;
Branch: 1 branch
BranchTrueFalseTotal
if length $i{"file"} > $max2 0 0 0
155 0 ▶ $max3 = length($i{line}) if length($i{line}) > $max3;
Branch: 1 branch
BranchTrueFalseTotal
if length $i{"line"} > $max3 0 0 0
156 0 push @i, \%i;
157 }
158 0 foreach my $ref (@i) {
159 $ref = sprintf("%-${max1}s at %-${max2}s line %${max3}s", $ref->{sub_name}, $ref->{file}, $ref->{line})
160 0 ▶ . ($ref->{args} ? " ($ref->{args})" : "");
Branch: 1 branch
BranchTrueFalseTotal
$ref->{'args'} ? : 0 0 0
161 }
162 0 return \@i;
163 }
164  
165 6 *caller_trace = \&ctrace;
166  
167 sub dex_trace {
168 0 ▶ _what_is_this(ctrace(1));
Subroutine dex_trace: not called CC=1 SCAR=6.9
Pod undocumented
169 }
170  
171 ###----------------------------------------------------------------###
172  
173 6 1;
174  
175 __END__
« Ex/Die.pm ↑ Summary Ex/Conf.pm »