]>
Dogcows Code - chaz/p5-CGI-Ex/blob - lib/CGI/Ex/Dump.pm
fd762913ed77fa99b799c7dac29d12d1f247f314
3 ### CGI Extended Data::Dumper Extension
5 ###----------------------------------------------------------------###
6 # Copyright 2004 - Paul Seamons #
7 # Distributed under the Perl Artistic License without warranty #
8 ###----------------------------------------------------------------###
10 ### See perldoc at bottom
12 use vars
qw(@ISA @EXPORT @EXPORT_OK $ON $SUB $QR1 $QR2 $full_filename);
17 @EXPORT = qw(dex dex_warn dex_text dex_html ctrace dex_trace);
18 @EXPORT_OK = qw(dex dex_warn dex_text dex_html ctrace dex_trace debug what_is_this);
26 $Data::Dumper
::Deparse
= eval {require B
::Deparse
};
29 ###----------------------------------------------------------------###
32 ### setup the Data::Dumper usage
33 $Data::Dumper
::Sortkeys
= 1 if ! defined $Data::Dumper
::Sortkeys
; # not avail pre 5.8
34 $Data::Dumper
::Useqq
= 1 if ! defined $Data::Dumper
::Useqq
;
35 $Data::Dumper
::Quotekeys
= 0 if ! defined $Data::Dumper
::Quotekeys
;
36 $Data::Dumper
::Pad
= ' ' if ! defined $Data::Dumper
::Pad
;
37 #$Data::Dumper::Deparse = 1 if ! defined $Data::Dumper::Deparse; # very useful
40 return Data
::Dumper-
>Dumpperl(\
@_);
43 ### how to display or parse the filename
44 $QR1 = qr{\A(?:/[^/]+){2,}/(?:perl|lib)/(.+)\Z
};
45 $QR2 = qr{\A.+?([\w\.\-]+/[\w\.\-]+)\Z};
48 ###----------------------------------------------------------------###
51 ### same as dumper but with more descriptive output and auto-formatting
55 ### figure out which sub we called
56 my ($pkg, $file, $line_n, $called) = caller(0);
57 ($pkg, $file, $line_n, $called) = caller(1) if $pkg eq __PACKAGE__
;
58 substr($called, 0, length(__PACKAGE__
) + 2, '');
60 ### get the actual line
63 $line = <IN
> for 1 .. $line_n;
67 ### get rid of extended filename
68 if (! $full_filename) {
69 $file =~ s/$QR1/$1/ || $file =~ s/$QR2/$1/;
73 my @dump = map {&$SUB($_)} @_;
74 my @var = ('$VAR') x
($#dump + 1);
75 if ($line =~ s/^ .*\b \Q$called\E ( \(?\s* | \s+ )//x
76 && $line =~ s/(?:\s+if\s+.+)? ;? \s*$//x) {
77 $line =~ s/ \s*\) $ //x if $1 && $1 =~ /\(/;
78 my @_var = map {/^[\"\']/ ? 'String' : $_} split (/\s*,\s*/, $line);
79 @var = @_var if $#var == $#_var;
83 if ($called eq 'dex_text'
84 || $called eq 'dex_warn'
85 || ! $ENV{REQUEST_METHOD
}) {
86 my $txt = "$called: $file line $line_n\n";
88 $dump[$_] =~ s
|\
$VAR1|$var[$_]|g
;
91 if ($called eq 'dex_text') { return $txt }
92 elsif ($called eq 'dex_warn') { warn $txt }
95 my $html = "<pre><b>$called: $file line $line_n</b>\n";
97 $dump[$_] =~ s/\\n/\n/g;
98 $dump[$_] = _html_quote
($dump[$_]);
99 $dump[$_] =~ s
|\
$VAR1|<b
>$var[$_]</b
>|g
;
103 return $html if $called eq 'dex_html';
105 &CGI
::Ex
::print_content_type
();
111 sub debug
{ &what_is_this
}
112 sub dex
{ &what_is_this
}
113 sub dex_warn
{ &what_is_this
}
114 sub dex_text
{ &what_is_this
}
115 sub dex_html
{ &what_is_this
}
119 return '' if ! defined $value;
120 $value =~ s/&/&/g;
121 $value =~ s/</</g;
122 $value =~ s/>/>/g;
123 # $value =~ s/\"/"/g;
127 ### ctrace is intended for work with perl 5.8 or higher's Carp
131 local $Carp::MaxArgNums
= 3;
132 local $Carp::MaxArgLen
= 20;
138 while (my %i = &Carp
::caller_info
(++$i)) {
139 $i{sub_name
} =~ s/\((.*)\)$//;
140 $i{args
} = $i{has_args
} ? $1 : "";
141 $i{sub_name
} =~ s/^.*?([^:]+)$/$1/;
142 $i{file
} =~ s/$QR1/$1/ || $i{file
} =~ s/$QR2/$1/;
143 $max1 = length($i{sub_name
}) if length($i{sub_name
}) > $max1;
144 $max2 = length($i{file
}) if length($i{file
}) > $max2;
145 $max3 = length($i{line
}) if length($i{line
}) > $max3;
148 foreach my $ref (@i) {
149 $ref = sprintf("%-${max1}s at %-${max2}s line %${max3}s", $ref->{sub_name
}, $ref->{file
}, $ref->{line
})
150 . ($ref->{args
} ? " ($ref->{args})" : "");
156 &what_is_this
(ctrace
(1));
159 ###----------------------------------------------------------------###
167 CGI::Ex::Dump - A debug utility
171 use CGI::Ex::Dump; # auto imports dex, dex_warn, dex_text and others
174 foo => ['a', 'b', 'Foo','a', 'b', 'Foo','a', 'b', 'Foo','a'],
177 dex $hash; # or dex_warn $hash;
183 dex $hash, "hi", $hash;
185 dex \@INC; # print to STDOUT, or format for web if $ENV{REQUEST_METHOD}
187 dex_warn \@INC; # same as dex but to STDOUT
189 print FOO dex_text \@INC; # same as dex but return dump
193 use CGI::Ex::Dump qw(debug);
199 Uses the base Data::Dumper of the distribution and gives it nicer formatting - and
200 allows for calling just about anytime during execution.
202 Calling &CGI::Ex::set_deparse() will allow for dumped output of subroutines
205 perl -e 'use CGI::Ex::Dump; dex "foo";'
207 See also L<Data::Dumper>.
209 Setting any of the Data::Dumper globals will alter the output.
215 =item C<dex>, C<debug>
217 Prints out pretty output to STDOUT. Formatted for the web if on the web.
225 Return the text as a scalar.
229 Caller trace returned as an arrayref. Suitable for use like "debug ctrace".
230 This does require at least perl 5.8.0's Carp.
234 Turns calls to routines on or off. Default is to be on.
240 Paul Seamons <perlspam at seamons dot com>
This page took 0.050104 seconds and 3 git commands to generate.