File Coverage

blib/lib/Data/Debug.pm
Criterion Covered Total %
statement 12 140 8.5
branch 4 82 4.8
condition 0 27 0.0
subroutine 5 19 26.3
pod 5 10 50.0
total 26 278 9.3


line stmt bran cond sub pod time code
1             package Data::Debug;
2              
3             =head1 NAME
4              
5             Data::Debug - allows for basic data dumping and introspection.
6              
7             =cut
8              
9             ####----------------------------------------------------------------###
10             ## Copyright 2014 - Bluehost #
11             ## Distributed under the Perl Artistic License without warranty #
12             ####----------------------------------------------------------------###
13              
14 1     1   15037 use strict;
  1         2  
  1         41  
15 1     1   4 use base qw(Exporter);
  1         1  
  1         437  
16             our @EXPORT = qw(debug debug_warn);
17             our @EXPORT_OK = qw(debug_text debug_html debug_plain caller_trace);
18             our $QR_TRACE1 = qr{ \A (?: /[^/]+ | \.)* / (?: perl | lib | cgi(?:-bin)? ) / (.+) \Z }x;
19             our $QR_TRACE2 = qr{ \A .+ / ( [\w\.\-]+ / [\w\.\-]+ ) \Z }x;
20              
21             our $VERSION = '0.03';
22              
23             BEGIN {
24             ### cache mod_perl version (light if or if not mod_perl)
25 1 0   1   4 my $v = (! $ENV{'MOD_PERL'}) ? 0
    50          
26             # mod_perl/1.27 or mod_perl/1.99_16 or mod_perl/2.0.1
27             # if MOD_PERL is set - don't die if regex fails - just assume 1.0
28             : ($ENV{'MOD_PERL'} =~ m{ ^ mod_perl / (\d+\.[\d_]+) (?: \.\d+)? $ }x) ? $1
29             : '1.0_0';
30 0     0   0 sub _mod_perl_version () { $v }
31 1 50   1   13 sub _is_mod_perl_1 () { $v < 1.98 && $v > 0 }
32 1     1   3 sub _is_mod_perl_2 () { $v >= 1.98 }
33              
34             ### cache apache request getter (light if or if not mod_perl)
35 1         1 my $sub;
36 1 50       1 if (_is_mod_perl_1) { # old mod_perl
    50          
37 0         0 require Apache;
38 0         0 $sub = sub { Apache->request };
  0         0  
39             } elsif (_is_mod_perl_2) {
40 0 0       0 if (eval { require Apache2::RequestRec }) { # debian style
  0         0  
41 0         0 require Apache2::RequestUtil;
42 0         0 $sub = sub { Apache2::RequestUtil->request };
  0         0  
43             } else { # fedora and mandrake style
44 0         0 require Apache::RequestUtil;
45 0         0 $sub = sub { Apache->request };
  0         0  
46             }
47             } else {
48 1         1407 $sub = sub {};
  0         0  
49             }
50 0     0 0   sub apache_request_sub () { $sub }
51             }
52              
53             my %LINE_CACHE;
54             my $DEPARSE;
55              
56 0     0 0   sub set_deparse { $DEPARSE = 1 }
57              
58             sub _dump {
59 0   0 0     local $Data::Dumper::Deparse = $DEPARSE && eval {require B::Deparse};
60 0           local $Data::Dumper::Sortkeys = 1;
61 0           local $Data::Dumper::Useqq = 1;
62 0           local $Data::Dumper::Quotekeys = 0;
63              
64 0           my $ref;
65 0 0 0       for (ref($_[0]) eq 'ARRAY' ? @{ $_[0] } : @_) { last if UNIVERSAL::isa($_, 'HASH') && ($ref = $_->{'dbh_cache'}) }
  0 0          
  0            
66 0 0         local @$ref{keys %$ref} = ('hidden')x(keys %$ref) if $ref;
67              
68 0           return Data::Dumper->Dumpperl(\@_);
69             }
70              
71             ###----------------------------------------------------------------###
72              
73             sub _what_is_this {
74 0     0     my ($pkg, $file, $line_n, $called) = caller(1);
75 0           $called =~ s/.+:://;
76              
77 0           my $line = '';
78 0 0         if (defined $LINE_CACHE{"$file:$line_n"}) {
79             # Just use global cache
80 0           $line = $LINE_CACHE{"$file:$line_n"};
81             }
82             else {
83 0 0         if (open my $fh, '<', $file) {
84 0           my $n = 0;
85 0           my $ignore_after = $line_n + 1000;
86 0           while (defined(my $l = <$fh>)) {
87 0 0         if (++$n == $line_n) {
    0          
    0          
88 0           $LINE_CACHE{"$file:$line_n"} = $line = $l;
89             }
90             elsif ($l =~ /debug/) {
91 0           $LINE_CACHE{"$file:$n"} = $l;
92             }
93             elsif ($n > $ignore_after) {
94 0           last;
95             }
96             }
97 0           close $fh;
98             }
99 0   0       $line ||= "";
100 0           $LINE_CACHE{"$file:$line_n"} = $line;
101             }
102              
103 0 0         $file =~ s/$QR_TRACE1/$1/ || $file =~ s/$QR_TRACE2/$1/; # trim up extended filename
104              
105 0           require Data::Dumper;
106 0 0         local $Data::Dumper::Indent = 1 if $called eq 'debug_warn';
107              
108             # dump it out
109 0           my @dump = map {_dump($_)} @_;
  0            
110 0           my @var = ('$VAR') x @dump;
111 0           my $hold;
112 0 0 0       if ($line =~ s/^ .*\b \Q$called\E ( \s* \( \s* | \s+ )//x
      0        
      0        
113             && ($hold = $1)
114             && ($line =~ s/ \s* \b if \b .* \n? $ //x
115             || $line =~ s/ \s* ; \s* $ //x
116             || $line =~ s/ \s+ $ //x)) {
117 0 0         $line =~ s/ \s*\) $ //x if $hold =~ /^\s*\(/;
118 0 0         my @_var = map {/^[\"\']/ ? 'String' : $_} split (/\s*,\s*/, $line);
  0            
119 0 0         @var = @_var if $#var == $#_var;
120             }
121              
122             # spit it out
123 0 0 0       if ($called eq 'debug_html'
      0        
124             || ($called eq 'debug' && $ENV{'REQUEST_METHOD'})) {
125 0           my $html = "
$called: $file line $line_n\n"; 
126 0           for (0 .. $#dump) {
127 0           $dump[$_] =~ s/(?
128 0           $dump[$_] = _html_quote($dump[$_]);
129 0           $dump[$_] =~ s|\$VAR1|$var[$_]|g;
130 0           $html .= $dump[$_];
131             }
132 0           $html .= "\n";
133 0 0         return $html if $called eq 'debug_html';
134 0           my $typed = content_typed();
135 0           print_content_type();
136 0 0         print $typed ? $html : "$html";
137             } else {
138 0           my $txt = "$called: $file line $line_n\n";
139 0           for (0 .. $#dump) {
140 0           $dump[$_] =~ s|\$VAR1|$var[$_]|g;
141 0           $txt .= $dump[$_];
142             }
143 0           $txt =~ s/\s*$/\n/;
144 0 0         return $txt if $called eq 'debug_text';
145              
146 0 0         if ($called eq 'debug_warn') {
147 0           warn $txt;
148             }
149             else {
150 0           print $txt;
151             }
152             }
153 0           return @_[0..$#_];
154             }
155              
156 0     0 1   sub debug { &_what_is_this }
157 0     0 1   sub debug_warn { &_what_is_this }
158 0     0 1   sub debug_text { &_what_is_this }
159 0     0 0   sub debug_html { &_what_is_this }
160              
161             sub debug_plain {
162 0     0 1   require Data::Dumper;
163 0           local $Data::Dumper::Indent = 1;
164 0           local $Data::Dumper::Terse = 1;
165 0           my $dump = join "\n", map {_dump($_)} @_;
  0            
166 0 0         print $dump if !defined wantarray;
167 0           return $dump;
168             }
169              
170             sub content_typed {
171 0 0   0 0   if (my $r = apache_request_sub()->()) {
172 0           return $r->bytes_sent;
173             } else {
174 0 0         return $ENV{'CONTENT_TYPED'} ? 1 : undef;
175             }
176             }
177              
178             sub print_content_type {
179 0     0 0   my $type = "text/html";
180              
181 0 0         if (my $r = apache_request_sub()->()) {
182 0 0         return if $r->bytes_sent;
183 0           $r->content_type($type);
184 0 0         $r->send_http_header if _is_mod_perl_1;
185             } else {
186 0 0         if (! $ENV{'CONTENT_TYPED'}) {
187 0           print "Content-Type: $type\r\n\r\n";
188 0           $ENV{'CONTENT_TYPED'} = '';
189             }
190 0           $ENV{'CONTENT_TYPED'} .= sprintf("%s, %d\n", (caller)[1,2]);
191             }
192             }
193              
194             sub _html_quote {
195 0     0     my $value = shift;
196 0 0         return '' if ! defined $value;
197 0           $value =~ s/&/&/g;
198 0           $value =~ s/
199 0           $value =~ s/>/>/g;
200 0           return $value;
201             }
202              
203             sub caller_trace {
204 0 0   0 1   eval { require 5.8.0 } || return ['Caller trace requires perl 5.8'];
  0            
205 0           require Carp::Heavy;
206 0           local $Carp::MaxArgNums = 5;
207 0           local $Carp::MaxArgLen = 20;
208 0   0       my $i = shift || 0;
209 0   0       my $skip = shift || {};
210 0           my @i = ();
211 0           my $max1 = 0;
212 0           my $max2 = 0;
213 0           my $max3 = 0;
214 0           while (my %i = Carp::caller_info(++$i)) {
215 0 0         next if $skip->{$i{file}};
216 0           $i{sub_name} =~ s/\((.*)\)$//;
217 0 0         $i{args} = $i{has_args} ? $1 : "";
218 0           $i{sub_name} =~ s/^.*?([^:]+)$/$1/;
219 0 0         $i{file} =~ s/$QR_TRACE1/$1/ || $i{file} =~ s/$QR_TRACE2/$1/;
220 0 0         $max1 = length($i{sub_name}) if length($i{sub_name}) > $max1;
221 0 0         $max2 = length($i{file}) if length($i{file}) > $max2;
222 0 0         $max3 = length($i{line}) if length($i{line}) > $max3;
223 0           push @i, \%i;
224             }
225 0           foreach my $ref (@i) {
226 0 0         $ref = sprintf("%-${max1}s at %-${max2}s line %${max3}s", $ref->{sub_name}, $ref->{file}, $ref->{line})
227             . ($ref->{args} ? " ($ref->{args})" : "");
228             }
229 0           return \@i;
230             }
231              
232             ###----------------------------------------------------------------###
233              
234             1;
235              
236             __END__