Filename | /usr/lib/x86_64-linux-gnu/perl/5.20/Data/Dumper.pm |
Statements | Executed 39 statements in 4.10ms |
Calls | P | F | Exclusive Time |
Inclusive Time |
Subroutine |
---|---|---|---|---|---|
1 | 1 | 1 | 16µs | 178µs | BEGIN@24 | Data::Dumper::
1 | 1 | 1 | 16µs | 43µs | BEGIN@272 | Data::Dumper::
1 | 1 | 1 | 14µs | 14µs | BEGIN@18 | Data::Dumper::
1 | 1 | 1 | 11µs | 14µs | BEGIN@731 | Data::Dumper::
1 | 1 | 1 | 9µs | 9µs | BEGIN@12 | Data::Dumper::
1 | 1 | 1 | 7µs | 36µs | BEGIN@22 | Data::Dumper::
1 | 1 | 1 | 2µs | 2µs | _vstring (xsub) | Data::Dumper::
0 | 0 | 0 | 0s | 0s | Bless | Data::Dumper::
0 | 0 | 0 | 0s | 0s | DESTROY | Data::Dumper::
0 | 0 | 0 | 0s | 0s | Deepcopy | Data::Dumper::
0 | 0 | 0 | 0s | 0s | Deparse | Data::Dumper::
0 | 0 | 0 | 0s | 0s | Dump | Data::Dumper::
0 | 0 | 0 | 0s | 0s | Dumper | Data::Dumper::
0 | 0 | 0 | 0s | 0s | DumperX | Data::Dumper::
0 | 0 | 0 | 0s | 0s | Dumpperl | Data::Dumper::
0 | 0 | 0 | 0s | 0s | Freezer | Data::Dumper::
0 | 0 | 0 | 0s | 0s | Indent | Data::Dumper::
0 | 0 | 0 | 0s | 0s | Maxdepth | Data::Dumper::
0 | 0 | 0 | 0s | 0s | Maxrecurse | Data::Dumper::
0 | 0 | 0 | 0s | 0s | Names | Data::Dumper::
0 | 0 | 0 | 0s | 0s | Pad | Data::Dumper::
0 | 0 | 0 | 0s | 0s | Pair | Data::Dumper::
0 | 0 | 0 | 0s | 0s | Purity | Data::Dumper::
0 | 0 | 0 | 0s | 0s | Quotekeys | Data::Dumper::
0 | 0 | 0 | 0s | 0s | Reset | Data::Dumper::
0 | 0 | 0 | 0s | 0s | Seen | Data::Dumper::
0 | 0 | 0 | 0s | 0s | Sortkeys | Data::Dumper::
0 | 0 | 0 | 0s | 0s | Sparseseen | Data::Dumper::
0 | 0 | 0 | 0s | 0s | Terse | Data::Dumper::
0 | 0 | 0 | 0s | 0s | Toaster | Data::Dumper::
0 | 0 | 0 | 0s | 0s | Useperl | Data::Dumper::
0 | 0 | 0 | 0s | 0s | Useqq | Data::Dumper::
0 | 0 | 0 | 0s | 0s | Values | Data::Dumper::
0 | 0 | 0 | 0s | 0s | Varname | Data::Dumper::
0 | 0 | 0 | 0s | 0s | _compose_out | Data::Dumper::
0 | 0 | 0 | 0s | 0s | _dump | Data::Dumper::
0 | 0 | 0 | 0s | 0s | _quote | Data::Dumper::
0 | 0 | 0 | 0s | 0s | _refine_name | Data::Dumper::
0 | 0 | 0 | 0s | 0s | _sortkeys | Data::Dumper::
0 | 0 | 0 | 0s | 0s | format_refaddr | Data::Dumper::
0 | 0 | 0 | 0s | 0s | init_refaddr_format | Data::Dumper::
0 | 0 | 0 | 0s | 0s | new | Data::Dumper::
0 | 0 | 0 | 0s | 0s | qquote | Data::Dumper::
Line | State ments |
Time on line |
Calls | Time in subs |
Code |
---|---|---|---|---|---|
1 | # | ||||
2 | # Data/Dumper.pm | ||||
3 | # | ||||
4 | # convert perl data structures into perl syntax suitable for both printing | ||||
5 | # and eval | ||||
6 | # | ||||
7 | # Documentation at the __END__ | ||||
8 | # | ||||
9 | |||||
10 | package Data::Dumper; | ||||
11 | |||||
12 | # spent 9µs within Data::Dumper::BEGIN@12 which was called:
# once (9µs+0s) by XML::LibXML::Error::BEGIN@257 at line 14 | ||||
13 | 1 | 5µs | $VERSION = '2.151'; # Don't forget to set version and release | ||
14 | 1 | 20µs | 1 | 9µs | } # date in POD below! # spent 9µs making 1 call to Data::Dumper::BEGIN@12 |
15 | |||||
16 | #$| = 1; | ||||
17 | |||||
18 | 2 | 50µs | 1 | 14µs | # spent 14µs within Data::Dumper::BEGIN@18 which was called:
# once (14µs+0s) by XML::LibXML::Error::BEGIN@257 at line 18 # spent 14µs making 1 call to Data::Dumper::BEGIN@18 |
19 | 1 | 600ns | require Exporter; | ||
20 | 1 | 400ns | require overload; | ||
21 | |||||
22 | 2 | 72µs | 2 | 64µs | # spent 36µs (7+28) within Data::Dumper::BEGIN@22 which was called:
# once (7µs+28µs) by XML::LibXML::Error::BEGIN@257 at line 22 # spent 36µs making 1 call to Data::Dumper::BEGIN@22
# spent 28µs making 1 call to Exporter::import |
23 | |||||
24 | # spent 178µs (16+162) within Data::Dumper::BEGIN@24 which was called:
# once (16µs+162µs) by XML::LibXML::Error::BEGIN@257 at line 38 | ||||
25 | 1 | 5µs | @ISA = qw(Exporter); | ||
26 | 1 | 400ns | @EXPORT = qw(Dumper); | ||
27 | 1 | 200ns | @EXPORT_OK = qw(DumperX); | ||
28 | |||||
29 | # if run under miniperl, or otherwise lacking dynamic loading, | ||||
30 | # XSLoader should be attempted to load, or the pure perl flag | ||||
31 | # toggled on load failure. | ||||
32 | 1 | 3µs | eval { | ||
33 | 1 | 500ns | require XSLoader; | ||
34 | 1 | 168µs | 1 | 162µs | XSLoader::load( 'Data::Dumper' ); # spent 162µs making 1 call to XSLoader::load |
35 | 1 | 500ns | 1 | ||
36 | } | ||||
37 | or $Useperl = 1; | ||||
38 | 1 | 849µs | 1 | 178µs | } # spent 178µs making 1 call to Data::Dumper::BEGIN@24 |
39 | |||||
40 | # module vars and their defaults | ||||
41 | 1 | 500ns | $Indent = 2 unless defined $Indent; | ||
42 | 1 | 100ns | $Purity = 0 unless defined $Purity; | ||
43 | 1 | 200ns | $Pad = "" unless defined $Pad; | ||
44 | 1 | 100ns | $Varname = "VAR" unless defined $Varname; | ||
45 | 1 | 100ns | $Useqq = 0 unless defined $Useqq; | ||
46 | 1 | 100ns | $Terse = 0 unless defined $Terse; | ||
47 | 1 | 100ns | $Freezer = "" unless defined $Freezer; | ||
48 | 1 | 100ns | $Toaster = "" unless defined $Toaster; | ||
49 | 1 | 100ns | $Deepcopy = 0 unless defined $Deepcopy; | ||
50 | 1 | 0s | $Quotekeys = 1 unless defined $Quotekeys; | ||
51 | 1 | 100ns | $Bless = "bless" unless defined $Bless; | ||
52 | #$Expdepth = 0 unless defined $Expdepth; | ||||
53 | 1 | 100ns | $Maxdepth = 0 unless defined $Maxdepth; | ||
54 | 1 | 100ns | $Pair = ' => ' unless defined $Pair; | ||
55 | 1 | 100ns | $Useperl = 0 unless defined $Useperl; | ||
56 | 1 | 100ns | $Sortkeys = 0 unless defined $Sortkeys; | ||
57 | 1 | 0s | $Deparse = 0 unless defined $Deparse; | ||
58 | 1 | 0s | $Sparseseen = 0 unless defined $Sparseseen; | ||
59 | 1 | 100ns | $Maxrecurse = 1000 unless defined $Maxrecurse; | ||
60 | |||||
61 | # | ||||
62 | # expects an arrayref of values to be dumped. | ||||
63 | # can optionally pass an arrayref of names for the values. | ||||
64 | # names must have leading $ sign stripped. begin the name with * | ||||
65 | # to cause output of arrays and hashes rather than refs. | ||||
66 | # | ||||
67 | sub new { | ||||
68 | my($c, $v, $n) = @_; | ||||
69 | |||||
70 | croak "Usage: PACKAGE->new(ARRAYREF, [ARRAYREF])" | ||||
71 | unless (defined($v) && (ref($v) eq 'ARRAY')); | ||||
72 | $n = [] unless (defined($n) && (ref($n) eq 'ARRAY')); | ||||
73 | |||||
74 | my($s) = { | ||||
75 | level => 0, # current recursive depth | ||||
76 | indent => $Indent, # various styles of indenting | ||||
77 | pad => $Pad, # all lines prefixed by this string | ||||
78 | xpad => "", # padding-per-level | ||||
79 | apad => "", # added padding for hash keys n such | ||||
80 | sep => "", # list separator | ||||
81 | pair => $Pair, # hash key/value separator: defaults to ' => ' | ||||
82 | seen => {}, # local (nested) refs (id => [name, val]) | ||||
83 | todump => $v, # values to dump [] | ||||
84 | names => $n, # optional names for values [] | ||||
85 | varname => $Varname, # prefix to use for tagging nameless ones | ||||
86 | purity => $Purity, # degree to which output is evalable | ||||
87 | useqq => $Useqq, # use "" for strings (backslashitis ensues) | ||||
88 | terse => $Terse, # avoid name output (where feasible) | ||||
89 | freezer => $Freezer, # name of Freezer method for objects | ||||
90 | toaster => $Toaster, # name of method to revive objects | ||||
91 | deepcopy => $Deepcopy, # do not cross-ref, except to stop recursion | ||||
92 | quotekeys => $Quotekeys, # quote hash keys | ||||
93 | 'bless' => $Bless, # keyword to use for "bless" | ||||
94 | # expdepth => $Expdepth, # cutoff depth for explicit dumping | ||||
95 | maxdepth => $Maxdepth, # depth beyond which we give up | ||||
96 | maxrecurse => $Maxrecurse, # depth beyond which we abort | ||||
97 | useperl => $Useperl, # use the pure Perl implementation | ||||
98 | sortkeys => $Sortkeys, # flag or filter for sorting hash keys | ||||
99 | deparse => $Deparse, # use B::Deparse for coderefs | ||||
100 | noseen => $Sparseseen, # do not populate the seen hash unless necessary | ||||
101 | }; | ||||
102 | |||||
103 | if ($Indent > 0) { | ||||
104 | $s->{xpad} = " "; | ||||
105 | $s->{sep} = "\n"; | ||||
106 | } | ||||
107 | return bless($s, $c); | ||||
108 | } | ||||
109 | |||||
110 | # Packed numeric addresses take less memory. Plus pack is faster than sprintf | ||||
111 | |||||
112 | # Most users of current versions of Data::Dumper will be 5.008 or later. | ||||
113 | # Anyone on 5.6.1 and 5.6.2 upgrading will be rare (particularly judging by | ||||
114 | # the bug reports from users on those platforms), so for the common case avoid | ||||
115 | # complexity, and avoid even compiling the unneeded code. | ||||
116 | |||||
117 | sub init_refaddr_format { | ||||
118 | } | ||||
119 | |||||
120 | sub format_refaddr { | ||||
121 | require Scalar::Util; | ||||
122 | pack "J", Scalar::Util::refaddr(shift); | ||||
123 | }; | ||||
124 | |||||
125 | 1 | 700ns | if ($] < 5.008) { | ||
126 | eval <<'EOC' or die; | ||||
127 | no warnings 'redefine'; | ||||
128 | my $refaddr_format; | ||||
129 | sub init_refaddr_format { | ||||
130 | require Config; | ||||
131 | my $f = $Config::Config{uvxformat}; | ||||
132 | $f =~ tr/"//d; | ||||
133 | $refaddr_format = "0x%" . $f; | ||||
134 | } | ||||
135 | |||||
136 | sub format_refaddr { | ||||
137 | require Scalar::Util; | ||||
138 | sprintf $refaddr_format, Scalar::Util::refaddr(shift); | ||||
139 | } | ||||
140 | |||||
141 | 1 | ||||
142 | EOC | ||||
143 | } | ||||
144 | |||||
145 | # | ||||
146 | # add-to or query the table of already seen references | ||||
147 | # | ||||
148 | sub Seen { | ||||
149 | my($s, $g) = @_; | ||||
150 | if (defined($g) && (ref($g) eq 'HASH')) { | ||||
151 | init_refaddr_format(); | ||||
152 | my($k, $v, $id); | ||||
153 | while (($k, $v) = each %$g) { | ||||
154 | if (defined $v) { | ||||
155 | if (ref $v) { | ||||
156 | $id = format_refaddr($v); | ||||
157 | if ($k =~ /^[*](.*)$/) { | ||||
158 | $k = (ref $v eq 'ARRAY') ? ( "\\\@" . $1 ) : | ||||
159 | (ref $v eq 'HASH') ? ( "\\\%" . $1 ) : | ||||
160 | (ref $v eq 'CODE') ? ( "\\\&" . $1 ) : | ||||
161 | ( "\$" . $1 ) ; | ||||
162 | } | ||||
163 | elsif ($k !~ /^\$/) { | ||||
164 | $k = "\$" . $k; | ||||
165 | } | ||||
166 | $s->{seen}{$id} = [$k, $v]; | ||||
167 | } | ||||
168 | else { | ||||
169 | carp "Only refs supported, ignoring non-ref item \$$k"; | ||||
170 | } | ||||
171 | } | ||||
172 | else { | ||||
173 | carp "Value of ref must be defined; ignoring undefined item \$$k"; | ||||
174 | } | ||||
175 | } | ||||
176 | return $s; | ||||
177 | } | ||||
178 | else { | ||||
179 | return map { @$_ } values %{$s->{seen}}; | ||||
180 | } | ||||
181 | } | ||||
182 | |||||
183 | # | ||||
184 | # set or query the values to be dumped | ||||
185 | # | ||||
186 | sub Values { | ||||
187 | my($s, $v) = @_; | ||||
188 | if (defined($v)) { | ||||
189 | if (ref($v) eq 'ARRAY') { | ||||
190 | $s->{todump} = [@$v]; # make a copy | ||||
191 | return $s; | ||||
192 | } | ||||
193 | else { | ||||
194 | croak "Argument to Values, if provided, must be array ref"; | ||||
195 | } | ||||
196 | } | ||||
197 | else { | ||||
198 | return @{$s->{todump}}; | ||||
199 | } | ||||
200 | } | ||||
201 | |||||
202 | # | ||||
203 | # set or query the names of the values to be dumped | ||||
204 | # | ||||
205 | sub Names { | ||||
206 | my($s, $n) = @_; | ||||
207 | if (defined($n)) { | ||||
208 | if (ref($n) eq 'ARRAY') { | ||||
209 | $s->{names} = [@$n]; # make a copy | ||||
210 | return $s; | ||||
211 | } | ||||
212 | else { | ||||
213 | croak "Argument to Names, if provided, must be array ref"; | ||||
214 | } | ||||
215 | } | ||||
216 | else { | ||||
217 | return @{$s->{names}}; | ||||
218 | } | ||||
219 | } | ||||
220 | |||||
221 | sub DESTROY {} | ||||
222 | |||||
223 | sub Dump { | ||||
224 | return &Dumpxs | ||||
225 | unless $Data::Dumper::Useperl || (ref($_[0]) && $_[0]->{useperl}) || | ||||
226 | $Data::Dumper::Deparse || (ref($_[0]) && $_[0]->{deparse}); | ||||
227 | return &Dumpperl; | ||||
228 | } | ||||
229 | |||||
230 | # | ||||
231 | # dump the refs in the current dumper object. | ||||
232 | # expects same args as new() if called via package name. | ||||
233 | # | ||||
234 | sub Dumpperl { | ||||
235 | my($s) = shift; | ||||
236 | my(@out, $val, $name); | ||||
237 | my($i) = 0; | ||||
238 | local(@post); | ||||
239 | init_refaddr_format(); | ||||
240 | |||||
241 | $s = $s->new(@_) unless ref $s; | ||||
242 | |||||
243 | for $val (@{$s->{todump}}) { | ||||
244 | @post = (); | ||||
245 | $name = $s->{names}[$i++]; | ||||
246 | $name = $s->_refine_name($name, $val, $i); | ||||
247 | |||||
248 | my $valstr; | ||||
249 | { | ||||
250 | local($s->{apad}) = $s->{apad}; | ||||
251 | $s->{apad} .= ' ' x (length($name) + 3) if $s->{indent} >= 2 and !$s->{terse}; | ||||
252 | $valstr = $s->_dump($val, $name); | ||||
253 | } | ||||
254 | |||||
255 | $valstr = "$name = " . $valstr . ';' if @post or !$s->{terse}; | ||||
256 | my $out = $s->_compose_out($valstr, \@post); | ||||
257 | |||||
258 | push @out, $out; | ||||
259 | } | ||||
260 | return wantarray ? @out : join('', @out); | ||||
261 | } | ||||
262 | |||||
263 | # wrap string in single quotes (escaping if needed) | ||||
264 | sub _quote { | ||||
265 | my $val = shift; | ||||
266 | $val =~ s/([\\\'])/\\$1/g; | ||||
267 | return "'" . $val . "'"; | ||||
268 | } | ||||
269 | |||||
270 | # Old Perls (5.14-) have trouble resetting vstring magic when it is no | ||||
271 | # longer valid. | ||||
272 | 2 | 2.41ms | 3 | 70µs | # spent 43µs (16+27) within Data::Dumper::BEGIN@272 which was called:
# once (16µs+27µs) by XML::LibXML::Error::BEGIN@257 at line 272 # spent 43µs making 1 call to Data::Dumper::BEGIN@272
# spent 25µs making 1 call to constant::import
# spent 2µs making 1 call to Data::Dumper::_vstring |
273 | |||||
274 | # | ||||
275 | # twist, toil and turn; | ||||
276 | # and recurse, of course. | ||||
277 | # sometimes sordidly; | ||||
278 | # and curse if no recourse. | ||||
279 | # | ||||
280 | sub _dump { | ||||
281 | my($s, $val, $name) = @_; | ||||
282 | my($out, $type, $id, $sname); | ||||
283 | |||||
284 | $type = ref $val; | ||||
285 | $out = ""; | ||||
286 | |||||
287 | if ($type) { | ||||
288 | |||||
289 | # Call the freezer method if it's specified and the object has the | ||||
290 | # method. Trap errors and warn() instead of die()ing, like the XS | ||||
291 | # implementation. | ||||
292 | my $freezer = $s->{freezer}; | ||||
293 | if ($freezer and UNIVERSAL::can($val, $freezer)) { | ||||
294 | eval { $val->$freezer() }; | ||||
295 | warn "WARNING(Freezer method call failed): $@" if $@; | ||||
296 | } | ||||
297 | |||||
298 | require Scalar::Util; | ||||
299 | my $realpack = Scalar::Util::blessed($val); | ||||
300 | my $realtype = $realpack ? Scalar::Util::reftype($val) : ref $val; | ||||
301 | $id = format_refaddr($val); | ||||
302 | |||||
303 | # Note: By this point $name is always defined and of non-zero length. | ||||
304 | # Keep a tab on it so that we do not fall into recursive pit. | ||||
305 | if (exists $s->{seen}{$id}) { | ||||
306 | if ($s->{purity} and $s->{level} > 0) { | ||||
307 | $out = ($realtype eq 'HASH') ? '{}' : | ||||
308 | ($realtype eq 'ARRAY') ? '[]' : | ||||
309 | 'do{my $o}' ; | ||||
310 | push @post, $name . " = " . $s->{seen}{$id}[0]; | ||||
311 | } | ||||
312 | else { | ||||
313 | $out = $s->{seen}{$id}[0]; | ||||
314 | if ($name =~ /^([\@\%])/) { | ||||
315 | my $start = $1; | ||||
316 | if ($out =~ /^\\$start/) { | ||||
317 | $out = substr($out, 1); | ||||
318 | } | ||||
319 | else { | ||||
320 | $out = $start . '{' . $out . '}'; | ||||
321 | } | ||||
322 | } | ||||
323 | } | ||||
324 | return $out; | ||||
325 | } | ||||
326 | else { | ||||
327 | # store our name | ||||
328 | $s->{seen}{$id} = [ ( | ||||
329 | ($name =~ /^[@%]/) | ||||
330 | ? ('\\' . $name ) | ||||
331 | : ($realtype eq 'CODE' and $name =~ /^[*](.*)$/) | ||||
332 | ? ('\\&' . $1 ) | ||||
333 | : $name | ||||
334 | ), $val ]; | ||||
335 | } | ||||
336 | my $no_bless = 0; | ||||
337 | my $is_regex = 0; | ||||
338 | if ( $realpack and ($] >= 5.009005 ? re::is_regexp($val) : $realpack eq 'Regexp') ) { | ||||
339 | $is_regex = 1; | ||||
340 | $no_bless = $realpack eq 'Regexp'; | ||||
341 | } | ||||
342 | |||||
343 | # If purity is not set and maxdepth is set, then check depth: | ||||
344 | # if we have reached maximum depth, return the string | ||||
345 | # representation of the thing we are currently examining | ||||
346 | # at this depth (i.e., 'Foo=ARRAY(0xdeadbeef)'). | ||||
347 | if (!$s->{purity} | ||||
348 | and defined($s->{maxdepth}) | ||||
349 | and $s->{maxdepth} > 0 | ||||
350 | and $s->{level} >= $s->{maxdepth}) | ||||
351 | { | ||||
352 | return qq['$val']; | ||||
353 | } | ||||
354 | |||||
355 | # avoid recursing infinitely [perl #122111] | ||||
356 | if ($s->{maxrecurse} > 0 | ||||
357 | and $s->{level} >= $s->{maxrecurse}) { | ||||
358 | die "Recursion limit of $s->{maxrecurse} exceeded"; | ||||
359 | } | ||||
360 | |||||
361 | # we have a blessed ref | ||||
362 | my ($blesspad); | ||||
363 | if ($realpack and !$no_bless) { | ||||
364 | $out = $s->{'bless'} . '( '; | ||||
365 | $blesspad = $s->{apad}; | ||||
366 | $s->{apad} .= ' ' if ($s->{indent} >= 2); | ||||
367 | } | ||||
368 | |||||
369 | $s->{level}++; | ||||
370 | my $ipad = $s->{xpad} x $s->{level}; | ||||
371 | |||||
372 | if ($is_regex) { | ||||
373 | my $pat; | ||||
374 | my $flags = ""; | ||||
375 | if (defined(*re::regexp_pattern{CODE})) { | ||||
376 | ($pat, $flags) = re::regexp_pattern($val); | ||||
377 | } | ||||
378 | else { | ||||
379 | $pat = "$val"; | ||||
380 | } | ||||
381 | $pat =~ s <(\\.)|/> { $1 || '\\/' }ge; | ||||
382 | $out .= "qr/$pat/$flags"; | ||||
383 | } | ||||
384 | elsif ($realtype eq 'SCALAR' || $realtype eq 'REF' | ||||
385 | || $realtype eq 'VSTRING') { | ||||
386 | if ($realpack) { | ||||
387 | $out .= 'do{\\(my $o = ' . $s->_dump($$val, "\${$name}") . ')}'; | ||||
388 | } | ||||
389 | else { | ||||
390 | $out .= '\\' . $s->_dump($$val, "\${$name}"); | ||||
391 | } | ||||
392 | } | ||||
393 | elsif ($realtype eq 'GLOB') { | ||||
394 | $out .= '\\' . $s->_dump($$val, "*{$name}"); | ||||
395 | } | ||||
396 | elsif ($realtype eq 'ARRAY') { | ||||
397 | my($pad, $mname); | ||||
398 | my($i) = 0; | ||||
399 | $out .= ($name =~ /^\@/) ? '(' : '['; | ||||
400 | $pad = $s->{sep} . $s->{pad} . $s->{apad}; | ||||
401 | ($name =~ /^\@(.*)$/) ? ($mname = "\$" . $1) : | ||||
402 | # omit -> if $foo->[0]->{bar}, but not ${$foo->[0]}->{bar} | ||||
403 | ($name =~ /^\\?[\%\@\*\$][^{].*[]}]$/) ? ($mname = $name) : | ||||
404 | ($mname = $name . '->'); | ||||
405 | $mname .= '->' if $mname =~ /^\*.+\{[A-Z]+\}$/; | ||||
406 | for my $v (@$val) { | ||||
407 | $sname = $mname . '[' . $i . ']'; | ||||
408 | $out .= $pad . $ipad . '#' . $i | ||||
409 | if $s->{indent} >= 3; | ||||
410 | $out .= $pad . $ipad . $s->_dump($v, $sname); | ||||
411 | $out .= "," if $i++ < $#$val; | ||||
412 | } | ||||
413 | $out .= $pad . ($s->{xpad} x ($s->{level} - 1)) if $i; | ||||
414 | $out .= ($name =~ /^\@/) ? ')' : ']'; | ||||
415 | } | ||||
416 | elsif ($realtype eq 'HASH') { | ||||
417 | my ($k, $v, $pad, $lpad, $mname, $pair); | ||||
418 | $out .= ($name =~ /^\%/) ? '(' : '{'; | ||||
419 | $pad = $s->{sep} . $s->{pad} . $s->{apad}; | ||||
420 | $lpad = $s->{apad}; | ||||
421 | $pair = $s->{pair}; | ||||
422 | ($name =~ /^\%(.*)$/) ? ($mname = "\$" . $1) : | ||||
423 | # omit -> if $foo->[0]->{bar}, but not ${$foo->[0]}->{bar} | ||||
424 | ($name =~ /^\\?[\%\@\*\$][^{].*[]}]$/) ? ($mname = $name) : | ||||
425 | ($mname = $name . '->'); | ||||
426 | $mname .= '->' if $mname =~ /^\*.+\{[A-Z]+\}$/; | ||||
427 | my $sortkeys = defined($s->{sortkeys}) ? $s->{sortkeys} : ''; | ||||
428 | my $keys = []; | ||||
429 | if ($sortkeys) { | ||||
430 | if (ref($s->{sortkeys}) eq 'CODE') { | ||||
431 | $keys = $s->{sortkeys}($val); | ||||
432 | unless (ref($keys) eq 'ARRAY') { | ||||
433 | carp "Sortkeys subroutine did not return ARRAYREF"; | ||||
434 | $keys = []; | ||||
435 | } | ||||
436 | } | ||||
437 | else { | ||||
438 | $keys = [ sort keys %$val ]; | ||||
439 | } | ||||
440 | } | ||||
441 | |||||
442 | # Ensure hash iterator is reset | ||||
443 | keys(%$val); | ||||
444 | |||||
445 | my $key; | ||||
446 | while (($k, $v) = ! $sortkeys ? (each %$val) : | ||||
447 | @$keys ? ($key = shift(@$keys), $val->{$key}) : | ||||
448 | () ) | ||||
449 | { | ||||
450 | my $nk = $s->_dump($k, ""); | ||||
451 | |||||
452 | # _dump doesn't quote numbers of this form | ||||
453 | if ($s->{quotekeys} && $nk =~ /^(?:0|-?[1-9][0-9]{0,8})\z/) { | ||||
454 | $nk = $s->{useqq} ? qq("$nk") : qq('$nk'); | ||||
455 | } | ||||
456 | elsif (!$s->{quotekeys} and $nk =~ /^[\"\']([A-Za-z_]\w*)[\"\']$/) { | ||||
457 | $nk = $1 | ||||
458 | } | ||||
459 | |||||
460 | $sname = $mname . '{' . $nk . '}'; | ||||
461 | $out .= $pad . $ipad . $nk . $pair; | ||||
462 | |||||
463 | # temporarily alter apad | ||||
464 | $s->{apad} .= (" " x (length($nk) + 4)) | ||||
465 | if $s->{indent} >= 2; | ||||
466 | $out .= $s->_dump($val->{$k}, $sname) . ","; | ||||
467 | $s->{apad} = $lpad | ||||
468 | if $s->{indent} >= 2; | ||||
469 | } | ||||
470 | if (substr($out, -1) eq ',') { | ||||
471 | chop $out; | ||||
472 | $out .= $pad . ($s->{xpad} x ($s->{level} - 1)); | ||||
473 | } | ||||
474 | $out .= ($name =~ /^\%/) ? ')' : '}'; | ||||
475 | } | ||||
476 | elsif ($realtype eq 'CODE') { | ||||
477 | if ($s->{deparse}) { | ||||
478 | require B::Deparse; | ||||
479 | my $sub = 'sub ' . (B::Deparse->new)->coderef2text($val); | ||||
480 | $pad = $s->{sep} . $s->{pad} . $s->{apad} . $s->{xpad} x ($s->{level} - 1); | ||||
481 | $sub =~ s/\n/$pad/gse; | ||||
482 | $out .= $sub; | ||||
483 | } | ||||
484 | else { | ||||
485 | $out .= 'sub { "DUMMY" }'; | ||||
486 | carp "Encountered CODE ref, using dummy placeholder" if $s->{purity}; | ||||
487 | } | ||||
488 | } | ||||
489 | else { | ||||
490 | croak "Can't handle '$realtype' type"; | ||||
491 | } | ||||
492 | |||||
493 | if ($realpack and !$no_bless) { # we have a blessed ref | ||||
494 | $out .= ', ' . _quote($realpack) . ' )'; | ||||
495 | $out .= '->' . $s->{toaster} . '()' | ||||
496 | if $s->{toaster} ne ''; | ||||
497 | $s->{apad} = $blesspad; | ||||
498 | } | ||||
499 | $s->{level}--; | ||||
500 | } | ||||
501 | else { # simple scalar | ||||
502 | |||||
503 | my $ref = \$_[1]; | ||||
504 | my $v; | ||||
505 | # first, catalog the scalar | ||||
506 | if ($name ne '') { | ||||
507 | $id = format_refaddr($ref); | ||||
508 | if (exists $s->{seen}{$id}) { | ||||
509 | if ($s->{seen}{$id}[2]) { | ||||
510 | $out = $s->{seen}{$id}[0]; | ||||
511 | #warn "[<$out]\n"; | ||||
512 | return "\${$out}"; | ||||
513 | } | ||||
514 | } | ||||
515 | else { | ||||
516 | #warn "[>\\$name]\n"; | ||||
517 | $s->{seen}{$id} = ["\\$name", $ref]; | ||||
518 | } | ||||
519 | } | ||||
520 | $ref = \$val; | ||||
521 | if (ref($ref) eq 'GLOB') { # glob | ||||
522 | my $name = substr($val, 1); | ||||
523 | if ($name =~ /^[A-Za-z_][\w:]*$/ && $name ne 'main::') { | ||||
524 | $name =~ s/^main::/::/; | ||||
525 | $sname = $name; | ||||
526 | } | ||||
527 | else { | ||||
528 | $sname = $s->_dump( | ||||
529 | $name eq 'main::' || $] < 5.007 && $name eq "main::\0" | ||||
530 | ? '' | ||||
531 | : $name, | ||||
532 | "", | ||||
533 | ); | ||||
534 | $sname = '{' . $sname . '}'; | ||||
535 | } | ||||
536 | if ($s->{purity}) { | ||||
537 | my $k; | ||||
538 | local ($s->{level}) = 0; | ||||
539 | for $k (qw(SCALAR ARRAY HASH)) { | ||||
540 | my $gval = *$val{$k}; | ||||
541 | next unless defined $gval; | ||||
542 | next if $k eq "SCALAR" && ! defined $$gval; # always there | ||||
543 | |||||
544 | # _dump can push into @post, so we hold our place using $postlen | ||||
545 | my $postlen = scalar @post; | ||||
546 | $post[$postlen] = "\*$sname = "; | ||||
547 | local ($s->{apad}) = " " x length($post[$postlen]) if $s->{indent} >= 2; | ||||
548 | $post[$postlen] .= $s->_dump($gval, "\*$sname\{$k\}"); | ||||
549 | } | ||||
550 | } | ||||
551 | $out .= '*' . $sname; | ||||
552 | } | ||||
553 | elsif (!defined($val)) { | ||||
554 | $out .= "undef"; | ||||
555 | } | ||||
556 | elsif (defined &_vstring and $v = _vstring($val) | ||||
557 | and !_bad_vsmg || eval $v eq $val) { | ||||
558 | $out .= $v; | ||||
559 | } | ||||
560 | elsif (!defined &_vstring | ||||
561 | and ref $ref eq 'VSTRING' || eval{Scalar::Util::isvstring($val)}) { | ||||
562 | $out .= sprintf "%vd", $val; | ||||
563 | } | ||||
564 | # \d here would treat "1\x{660}" as a safe decimal number | ||||
565 | elsif ($val =~ /^(?:0|-?[1-9][0-9]{0,8})\z/) { # safe decimal number | ||||
566 | $out .= $val; | ||||
567 | } | ||||
568 | else { # string | ||||
569 | if ($s->{useqq} or $val =~ tr/\0-\377//c) { | ||||
570 | # Fall back to qq if there's Unicode | ||||
571 | $out .= qquote($val, $s->{useqq}); | ||||
572 | } | ||||
573 | else { | ||||
574 | $out .= _quote($val); | ||||
575 | } | ||||
576 | } | ||||
577 | } | ||||
578 | if ($id) { | ||||
579 | # if we made it this far, $id was added to seen list at current | ||||
580 | # level, so remove it to get deep copies | ||||
581 | if ($s->{deepcopy}) { | ||||
582 | delete($s->{seen}{$id}); | ||||
583 | } | ||||
584 | elsif ($name) { | ||||
585 | $s->{seen}{$id}[2] = 1; | ||||
586 | } | ||||
587 | } | ||||
588 | return $out; | ||||
589 | } | ||||
590 | |||||
591 | # | ||||
592 | # non-OO style of earlier version | ||||
593 | # | ||||
594 | sub Dumper { | ||||
595 | return Data::Dumper->Dump([@_]); | ||||
596 | } | ||||
597 | |||||
598 | # compat stub | ||||
599 | sub DumperX { | ||||
600 | return Data::Dumper->Dumpxs([@_], []); | ||||
601 | } | ||||
602 | |||||
603 | # | ||||
604 | # reset the "seen" cache | ||||
605 | # | ||||
606 | sub Reset { | ||||
607 | my($s) = shift; | ||||
608 | $s->{seen} = {}; | ||||
609 | return $s; | ||||
610 | } | ||||
611 | |||||
612 | sub Indent { | ||||
613 | my($s, $v) = @_; | ||||
614 | if (defined($v)) { | ||||
615 | if ($v == 0) { | ||||
616 | $s->{xpad} = ""; | ||||
617 | $s->{sep} = ""; | ||||
618 | } | ||||
619 | else { | ||||
620 | $s->{xpad} = " "; | ||||
621 | $s->{sep} = "\n"; | ||||
622 | } | ||||
623 | $s->{indent} = $v; | ||||
624 | return $s; | ||||
625 | } | ||||
626 | else { | ||||
627 | return $s->{indent}; | ||||
628 | } | ||||
629 | } | ||||
630 | |||||
631 | sub Pair { | ||||
632 | my($s, $v) = @_; | ||||
633 | defined($v) ? (($s->{pair} = $v), return $s) : $s->{pair}; | ||||
634 | } | ||||
635 | |||||
636 | sub Pad { | ||||
637 | my($s, $v) = @_; | ||||
638 | defined($v) ? (($s->{pad} = $v), return $s) : $s->{pad}; | ||||
639 | } | ||||
640 | |||||
641 | sub Varname { | ||||
642 | my($s, $v) = @_; | ||||
643 | defined($v) ? (($s->{varname} = $v), return $s) : $s->{varname}; | ||||
644 | } | ||||
645 | |||||
646 | sub Purity { | ||||
647 | my($s, $v) = @_; | ||||
648 | defined($v) ? (($s->{purity} = $v), return $s) : $s->{purity}; | ||||
649 | } | ||||
650 | |||||
651 | sub Useqq { | ||||
652 | my($s, $v) = @_; | ||||
653 | defined($v) ? (($s->{useqq} = $v), return $s) : $s->{useqq}; | ||||
654 | } | ||||
655 | |||||
656 | sub Terse { | ||||
657 | my($s, $v) = @_; | ||||
658 | defined($v) ? (($s->{terse} = $v), return $s) : $s->{terse}; | ||||
659 | } | ||||
660 | |||||
661 | sub Freezer { | ||||
662 | my($s, $v) = @_; | ||||
663 | defined($v) ? (($s->{freezer} = $v), return $s) : $s->{freezer}; | ||||
664 | } | ||||
665 | |||||
666 | sub Toaster { | ||||
667 | my($s, $v) = @_; | ||||
668 | defined($v) ? (($s->{toaster} = $v), return $s) : $s->{toaster}; | ||||
669 | } | ||||
670 | |||||
671 | sub Deepcopy { | ||||
672 | my($s, $v) = @_; | ||||
673 | defined($v) ? (($s->{deepcopy} = $v), return $s) : $s->{deepcopy}; | ||||
674 | } | ||||
675 | |||||
676 | sub Quotekeys { | ||||
677 | my($s, $v) = @_; | ||||
678 | defined($v) ? (($s->{quotekeys} = $v), return $s) : $s->{quotekeys}; | ||||
679 | } | ||||
680 | |||||
681 | sub Bless { | ||||
682 | my($s, $v) = @_; | ||||
683 | defined($v) ? (($s->{'bless'} = $v), return $s) : $s->{'bless'}; | ||||
684 | } | ||||
685 | |||||
686 | sub Maxdepth { | ||||
687 | my($s, $v) = @_; | ||||
688 | defined($v) ? (($s->{'maxdepth'} = $v), return $s) : $s->{'maxdepth'}; | ||||
689 | } | ||||
690 | |||||
691 | sub Maxrecurse { | ||||
692 | my($s, $v) = @_; | ||||
693 | defined($v) ? (($s->{'maxrecurse'} = $v), return $s) : $s->{'maxrecurse'}; | ||||
694 | } | ||||
695 | |||||
696 | sub Useperl { | ||||
697 | my($s, $v) = @_; | ||||
698 | defined($v) ? (($s->{'useperl'} = $v), return $s) : $s->{'useperl'}; | ||||
699 | } | ||||
700 | |||||
701 | sub Sortkeys { | ||||
702 | my($s, $v) = @_; | ||||
703 | defined($v) ? (($s->{'sortkeys'} = $v), return $s) : $s->{'sortkeys'}; | ||||
704 | } | ||||
705 | |||||
706 | sub Deparse { | ||||
707 | my($s, $v) = @_; | ||||
708 | defined($v) ? (($s->{'deparse'} = $v), return $s) : $s->{'deparse'}; | ||||
709 | } | ||||
710 | |||||
711 | sub Sparseseen { | ||||
712 | my($s, $v) = @_; | ||||
713 | defined($v) ? (($s->{'noseen'} = $v), return $s) : $s->{'noseen'}; | ||||
714 | } | ||||
715 | |||||
716 | # used by qquote below | ||||
717 | 1 | 3µs | my %esc = ( | ||
718 | "\a" => "\\a", | ||||
719 | "\b" => "\\b", | ||||
720 | "\t" => "\\t", | ||||
721 | "\n" => "\\n", | ||||
722 | "\f" => "\\f", | ||||
723 | "\r" => "\\r", | ||||
724 | "\e" => "\\e", | ||||
725 | ); | ||||
726 | |||||
727 | # put a string value in double quotes | ||||
728 | sub qquote { | ||||
729 | local($_) = shift; | ||||
730 | s/([\\\"\@\$])/\\$1/g; | ||||
731 | 2 | 500µs | 2 | 17µs | # spent 14µs (11+3) within Data::Dumper::BEGIN@731 which was called:
# once (11µs+3µs) by XML::LibXML::Error::BEGIN@257 at line 731 # spent 14µs making 1 call to Data::Dumper::BEGIN@731
# spent 3µs making 1 call to bytes::import |
732 | s/([[:^ascii:]])/'\x{'.sprintf("%x",ord($1)).'}'/ge if $bytes > length; | ||||
733 | return qq("$_") unless | ||||
734 | /[^ !"\#\$%&'()*+,\-.\/0-9:;<=>?\@A-Z[\\\]^_`a-z{|}~]/; # fast exit | ||||
735 | |||||
736 | my $high = shift || ""; | ||||
737 | s/([\a\b\t\n\f\r\e])/$esc{$1}/g; | ||||
738 | |||||
739 | if (ord('^')==94) { # ascii | ||||
740 | # no need for 3 digits in escape for these | ||||
741 | s/([\0-\037])(?!\d)/'\\'.sprintf('%o',ord($1))/eg; | ||||
742 | s/([\0-\037\177])/'\\'.sprintf('%03o',ord($1))/eg; | ||||
743 | # all but last branch below not supported --BEHAVIOR SUBJECT TO CHANGE-- | ||||
744 | if ($high eq "iso8859") { | ||||
745 | s/([\200-\240])/'\\'.sprintf('%o',ord($1))/eg; | ||||
746 | } elsif ($high eq "utf8") { | ||||
747 | # use utf8; | ||||
748 | # $str =~ s/([^\040-\176])/sprintf "\\x{%04x}", ord($1)/ge; | ||||
749 | } elsif ($high eq "8bit") { | ||||
750 | # leave it as it is | ||||
751 | } else { | ||||
752 | s/([\200-\377])/'\\'.sprintf('%03o',ord($1))/eg; | ||||
753 | s/([^\040-\176])/sprintf "\\x{%04x}", ord($1)/ge; | ||||
754 | } | ||||
755 | } | ||||
756 | else { # ebcdic | ||||
757 | s{([^ !"\#\$%&'()*+,\-.\/0-9:;<=>?\@A-Z[\\\]^_`a-z{|}~])(?!\d)} | ||||
758 | {my $v = ord($1); '\\'.sprintf(($v <= 037 ? '%o' : '%03o'), $v)}eg; | ||||
759 | s{([^ !"\#\$%&'()*+,\-.\/0-9:;<=>?\@A-Z[\\\]^_`a-z{|}~])} | ||||
760 | {'\\'.sprintf('%03o',ord($1))}eg; | ||||
761 | } | ||||
762 | |||||
763 | return qq("$_"); | ||||
764 | } | ||||
765 | |||||
766 | # helper sub to sort hash keys in Perl < 5.8.0 where we don't have | ||||
767 | # access to sortsv() from XS | ||||
768 | sub _sortkeys { [ sort keys %{$_[0]} ] } | ||||
769 | |||||
770 | sub _refine_name { | ||||
771 | my $s = shift; | ||||
772 | my ($name, $val, $i) = @_; | ||||
773 | if (defined $name) { | ||||
774 | if ($name =~ /^[*](.*)$/) { | ||||
775 | if (defined $val) { | ||||
776 | $name = (ref $val eq 'ARRAY') ? ( "\@" . $1 ) : | ||||
777 | (ref $val eq 'HASH') ? ( "\%" . $1 ) : | ||||
778 | (ref $val eq 'CODE') ? ( "\*" . $1 ) : | ||||
779 | ( "\$" . $1 ) ; | ||||
780 | } | ||||
781 | else { | ||||
782 | $name = "\$" . $1; | ||||
783 | } | ||||
784 | } | ||||
785 | elsif ($name !~ /^\$/) { | ||||
786 | $name = "\$" . $name; | ||||
787 | } | ||||
788 | } | ||||
789 | else { # no names provided | ||||
790 | $name = "\$" . $s->{varname} . $i; | ||||
791 | } | ||||
792 | return $name; | ||||
793 | } | ||||
794 | |||||
795 | sub _compose_out { | ||||
796 | my $s = shift; | ||||
797 | my ($valstr, $postref) = @_; | ||||
798 | my $out = ""; | ||||
799 | $out .= $s->{pad} . $valstr . $s->{sep}; | ||||
800 | if (@{$postref}) { | ||||
801 | $out .= $s->{pad} . | ||||
802 | join(';' . $s->{sep} . $s->{pad}, @{$postref}) . | ||||
803 | ';' . | ||||
804 | $s->{sep}; | ||||
805 | } | ||||
806 | return $out; | ||||
807 | } | ||||
808 | |||||
809 | 1 | 7µs | 1; | ||
810 | __END__ | ||||
# spent 2µs within Data::Dumper::_vstring which was called:
# once (2µs+0s) by Data::Dumper::BEGIN@272 at line 272 |