Commit | Line | Data |
---|---|---|
823edd99 GS |
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 | ||
2bc27c6c TR |
12 | use strict; |
13 | use warnings; | |
823edd99 GS |
14 | |
15 | #$| = 1; | |
16 | ||
ed904d96 | 17 | use 5.008_001; |
823edd99 | 18 | require Exporter; |
823edd99 | 19 | |
00ec40a9 | 20 | use constant IS_PRE_516_PERL => $] < 5.016; |
e1416070 | 21 | |
a261571f | 22 | use Carp (); |
823edd99 | 23 | |
2bc27c6c TR |
24 | # Globals people alter. |
25 | our ( $Indent, $Trailingcomma, $Purity, $Pad, $Varname, $Useqq, $Terse, $Freezer, | |
26 | $Toaster, $Deepcopy, $Quotekeys, $Bless, $Maxdepth, $Pair, $Sortkeys, | |
27 | $Deparse, $Sparseseen, $Maxrecurse, $Useperl ); | |
28 | ||
29 | our ( @ISA, @EXPORT, @EXPORT_OK, $VERSION ); | |
30 | ||
907e5114 | 31 | BEGIN { |
805d541f | 32 | $VERSION = '2.180'; # Don't forget to set version and release |
2bc27c6c TR |
33 | # date in POD below! |
34 | ||
907e5114 JB |
35 | @ISA = qw(Exporter); |
36 | @EXPORT = qw(Dumper); | |
37 | @EXPORT_OK = qw(DumperX); | |
823edd99 | 38 | |
907e5114 JB |
39 | # if run under miniperl, or otherwise lacking dynamic loading, |
40 | # XSLoader should be attempted to load, or the pure perl flag | |
41 | # toggled on load failure. | |
42 | eval { | |
3bd791fa JK |
43 | require XSLoader; |
44 | XSLoader::load( 'Data::Dumper' ); | |
45 | 1 | |
d036e907 | 46 | } |
1e9285c2 | 47 | or $Useperl = 1; |
907e5114 | 48 | } |
823edd99 | 49 | |
31ac59b6 KW |
50 | my $IS_ASCII = ord 'A' == 65; |
51 | ||
823edd99 | 52 | # module vars and their defaults |
907e5114 | 53 | $Indent = 2 unless defined $Indent; |
7089d392 | 54 | $Trailingcomma = 0 unless defined $Trailingcomma; |
907e5114 JB |
55 | $Purity = 0 unless defined $Purity; |
56 | $Pad = "" unless defined $Pad; | |
57 | $Varname = "VAR" unless defined $Varname; | |
58 | $Useqq = 0 unless defined $Useqq; | |
59 | $Terse = 0 unless defined $Terse; | |
60 | $Freezer = "" unless defined $Freezer; | |
61 | $Toaster = "" unless defined $Toaster; | |
62 | $Deepcopy = 0 unless defined $Deepcopy; | |
63 | $Quotekeys = 1 unless defined $Quotekeys; | |
64 | $Bless = "bless" unless defined $Bless; | |
65 | #$Expdepth = 0 unless defined $Expdepth; | |
66 | $Maxdepth = 0 unless defined $Maxdepth; | |
67 | $Pair = ' => ' unless defined $Pair; | |
68 | $Useperl = 0 unless defined $Useperl; | |
69 | $Sortkeys = 0 unless defined $Sortkeys; | |
70 | $Deparse = 0 unless defined $Deparse; | |
d424882c | 71 | $Sparseseen = 0 unless defined $Sparseseen; |
19be3be6 | 72 | $Maxrecurse = 1000 unless defined $Maxrecurse; |
823edd99 GS |
73 | |
74 | # | |
75 | # expects an arrayref of values to be dumped. | |
76 | # can optionally pass an arrayref of names for the values. | |
77 | # names must have leading $ sign stripped. begin the name with * | |
78 | # to cause output of arrays and hashes rather than refs. | |
79 | # | |
80 | sub new { | |
81 | my($c, $v, $n) = @_; | |
82 | ||
a261571f | 83 | Carp::croak("Usage: PACKAGE->new(ARRAYREF, [ARRAYREF])") |
823edd99 | 84 | unless (defined($v) && (ref($v) eq 'ARRAY')); |
b09a1111 | 85 | $n = [] unless (defined($n) && (ref($n) eq 'ARRAY')); |
823edd99 | 86 | |
3bd791fa JK |
87 | my($s) = { |
88 | level => 0, # current recursive depth | |
89 | indent => $Indent, # various styles of indenting | |
7089d392 | 90 | trailingcomma => $Trailingcomma, # whether to add comma after last elem |
3bd791fa JK |
91 | pad => $Pad, # all lines prefixed by this string |
92 | xpad => "", # padding-per-level | |
93 | apad => "", # added padding for hash keys n such | |
94 | sep => "", # list separator | |
95 | pair => $Pair, # hash key/value separator: defaults to ' => ' | |
96 | seen => {}, # local (nested) refs (id => [name, val]) | |
97 | todump => $v, # values to dump [] | |
98 | names => $n, # optional names for values [] | |
99 | varname => $Varname, # prefix to use for tagging nameless ones | |
100 | purity => $Purity, # degree to which output is evalable | |
101 | useqq => $Useqq, # use "" for strings (backslashitis ensues) | |
102 | terse => $Terse, # avoid name output (where feasible) | |
103 | freezer => $Freezer, # name of Freezer method for objects | |
104 | toaster => $Toaster, # name of method to revive objects | |
436d4ccf | 105 | deepcopy => $Deepcopy, # do not cross-ref, except to stop recursion |
3bd791fa JK |
106 | quotekeys => $Quotekeys, # quote hash keys |
107 | 'bless' => $Bless, # keyword to use for "bless" | |
108 | # expdepth => $Expdepth, # cutoff depth for explicit dumping | |
109 | maxdepth => $Maxdepth, # depth beyond which we give up | |
19be3be6 | 110 | maxrecurse => $Maxrecurse, # depth beyond which we abort |
3bd791fa JK |
111 | useperl => $Useperl, # use the pure Perl implementation |
112 | sortkeys => $Sortkeys, # flag or filter for sorting hash keys | |
113 | deparse => $Deparse, # use B::Deparse for coderefs | |
114 | noseen => $Sparseseen, # do not populate the seen hash unless necessary | |
115 | }; | |
823edd99 GS |
116 | |
117 | if ($Indent > 0) { | |
118 | $s->{xpad} = " "; | |
119 | $s->{sep} = "\n"; | |
120 | } | |
121 | return bless($s, $c); | |
122 | } | |
123 | ||
53095d08 | 124 | # Packed numeric addresses take less memory. Plus pack is faster than sprintf |
e52c0e5a | 125 | |
53095d08 | 126 | sub format_refaddr { |
e52c0e5a NC |
127 | require Scalar::Util; |
128 | pack "J", Scalar::Util::refaddr(shift); | |
53095d08 NC |
129 | }; |
130 | ||
823edd99 GS |
131 | # |
132 | # add-to or query the table of already seen references | |
133 | # | |
134 | sub Seen { | |
135 | my($s, $g) = @_; | |
136 | if (defined($g) && (ref($g) eq 'HASH')) { | |
137 | my($k, $v, $id); | |
138 | while (($k, $v) = each %$g) { | |
3bd791fa JK |
139 | if (defined $v) { |
140 | if (ref $v) { | |
141 | $id = format_refaddr($v); | |
142 | if ($k =~ /^[*](.*)$/) { | |
143 | $k = (ref $v eq 'ARRAY') ? ( "\\\@" . $1 ) : | |
144 | (ref $v eq 'HASH') ? ( "\\\%" . $1 ) : | |
145 | (ref $v eq 'CODE') ? ( "\\\&" . $1 ) : | |
146 | ( "\$" . $1 ) ; | |
147 | } | |
148 | elsif ($k !~ /^\$/) { | |
149 | $k = "\$" . $k; | |
150 | } | |
151 | $s->{seen}{$id} = [$k, $v]; | |
152 | } | |
153 | else { | |
a261571f | 154 | Carp::carp("Only refs supported, ignoring non-ref item \$$k"); |
3bd791fa | 155 | } |
823edd99 GS |
156 | } |
157 | else { | |
a261571f | 158 | Carp::carp("Value of ref must be defined; ignoring undefined item \$$k"); |
823edd99 GS |
159 | } |
160 | } | |
161 | return $s; | |
162 | } | |
163 | else { | |
164 | return map { @$_ } values %{$s->{seen}}; | |
165 | } | |
166 | } | |
167 | ||
168 | # | |
169 | # set or query the values to be dumped | |
170 | # | |
171 | sub Values { | |
172 | my($s, $v) = @_; | |
3bd791fa JK |
173 | if (defined($v)) { |
174 | if (ref($v) eq 'ARRAY') { | |
175 | $s->{todump} = [@$v]; # make a copy | |
176 | return $s; | |
177 | } | |
178 | else { | |
a261571f | 179 | Carp::croak("Argument to Values, if provided, must be array ref"); |
3bd791fa | 180 | } |
823edd99 GS |
181 | } |
182 | else { | |
183 | return @{$s->{todump}}; | |
184 | } | |
185 | } | |
186 | ||
187 | # | |
188 | # set or query the names of the values to be dumped | |
189 | # | |
190 | sub Names { | |
191 | my($s, $n) = @_; | |
3bd791fa JK |
192 | if (defined($n)) { |
193 | if (ref($n) eq 'ARRAY') { | |
194 | $s->{names} = [@$n]; # make a copy | |
195 | return $s; | |
196 | } | |
197 | else { | |
a261571f | 198 | Carp::croak("Argument to Names, if provided, must be array ref"); |
3bd791fa | 199 | } |
823edd99 GS |
200 | } |
201 | else { | |
202 | return @{$s->{names}}; | |
203 | } | |
204 | } | |
205 | ||
206 | sub DESTROY {} | |
207 | ||
0f1923bd | 208 | sub Dump { |
e1416070 S |
209 | return &Dumpxs |
210 | unless $Data::Dumper::Useperl || (ref($_[0]) && $_[0]->{useperl}) | |
31ac59b6 KW |
211 | # Use pure perl version on earlier releases on EBCDIC platforms |
212 | || (! $IS_ASCII && $] lt 5.021_010); | |
e1416070 | 213 | return &Dumpperl; |
0f1923bd GS |
214 | } |
215 | ||
823edd99 GS |
216 | # |
217 | # dump the refs in the current dumper object. | |
218 | # expects same args as new() if called via package name. | |
219 | # | |
2bc27c6c | 220 | our @post; |
0f1923bd | 221 | sub Dumpperl { |
823edd99 GS |
222 | my($s) = shift; |
223 | my(@out, $val, $name); | |
224 | my($i) = 0; | |
225 | local(@post); | |
226 | ||
227 | $s = $s->new(@_) unless ref $s; | |
228 | ||
229 | for $val (@{$s->{todump}}) { | |
823edd99 GS |
230 | @post = (); |
231 | $name = $s->{names}[$i++]; | |
3bd791fa | 232 | $name = $s->_refine_name($name, $val, $i); |
823edd99 GS |
233 | |
234 | my $valstr; | |
235 | { | |
236 | local($s->{apad}) = $s->{apad}; | |
d34e9bd9 | 237 | $s->{apad} .= ' ' x (length($name) + 3) if $s->{indent} >= 2 and !$s->{terse}; |
823edd99 GS |
238 | $valstr = $s->_dump($val, $name); |
239 | } | |
240 | ||
241 | $valstr = "$name = " . $valstr . ';' if @post or !$s->{terse}; | |
3bd791fa | 242 | my $out = $s->_compose_out($valstr, \@post); |
823edd99 GS |
243 | |
244 | push @out, $out; | |
245 | } | |
246 | return wantarray ? @out : join('', @out); | |
247 | } | |
248 | ||
d0c214fd AF |
249 | # wrap string in single quotes (escaping if needed) |
250 | sub _quote { | |
251 | my $val = shift; | |
252 | $val =~ s/([\\\'])/\\$1/g; | |
253 | return "'" . $val . "'"; | |
254 | } | |
255 | ||
d036e907 FC |
256 | # Old Perls (5.14-) have trouble resetting vstring magic when it is no |
257 | # longer valid. | |
258 | use constant _bad_vsmg => defined &_vstring && (_vstring(~v0)||'') eq "v0"; | |
259 | ||
823edd99 GS |
260 | # |
261 | # twist, toil and turn; | |
262 | # and recurse, of course. | |
31a725b3 JH |
263 | # sometimes sordidly; |
264 | # and curse if no recourse. | |
823edd99 GS |
265 | # |
266 | sub _dump { | |
267 | my($s, $val, $name) = @_; | |
3bd791fa | 268 | my($out, $type, $id, $sname); |
823edd99 | 269 | |
823edd99 GS |
270 | $type = ref $val; |
271 | $out = ""; | |
272 | ||
273 | if ($type) { | |
274 | ||
c5f7c514 ST |
275 | # Call the freezer method if it's specified and the object has the |
276 | # method. Trap errors and warn() instead of die()ing, like the XS | |
277 | # implementation. | |
278 | my $freezer = $s->{freezer}; | |
279 | if ($freezer and UNIVERSAL::can($val, $freezer)) { | |
280 | eval { $val->$freezer() }; | |
281 | warn "WARNING(Freezer method call failed): $@" if $@; | |
823edd99 GS |
282 | } |
283 | ||
2728842d | 284 | require Scalar::Util; |
3bd791fa JK |
285 | my $realpack = Scalar::Util::blessed($val); |
286 | my $realtype = $realpack ? Scalar::Util::reftype($val) : ref $val; | |
2728842d | 287 | $id = format_refaddr($val); |
a2126434 | 288 | |
3bd791fa | 289 | # Note: By this point $name is always defined and of non-zero length. |
436d4ccf | 290 | # Keep a tab on it so that we do not fall into recursive pit. |
3bd791fa JK |
291 | if (exists $s->{seen}{$id}) { |
292 | if ($s->{purity} and $s->{level} > 0) { | |
293 | $out = ($realtype eq 'HASH') ? '{}' : | |
294 | ($realtype eq 'ARRAY') ? '[]' : | |
295 | 'do{my $o}' ; | |
296 | push @post, $name . " = " . $s->{seen}{$id}[0]; | |
7820172a GS |
297 | } |
298 | else { | |
3bd791fa JK |
299 | $out = $s->{seen}{$id}[0]; |
300 | if ($name =~ /^([\@\%])/) { | |
301 | my $start = $1; | |
302 | if ($out =~ /^\\$start/) { | |
303 | $out = substr($out, 1); | |
304 | } | |
305 | else { | |
306 | $out = $start . '{' . $out . '}'; | |
307 | } | |
308 | } | |
823edd99 | 309 | } |
3bd791fa JK |
310 | return $out; |
311 | } | |
312 | else { | |
313 | # store our name | |
314 | $s->{seen}{$id} = [ ( | |
315 | ($name =~ /^[@%]/) | |
316 | ? ('\\' . $name ) | |
317 | : ($realtype eq 'CODE' and $name =~ /^[*](.*)$/) | |
318 | ? ('\\&' . $1 ) | |
319 | : $name | |
320 | ), $val ]; | |
823edd99 | 321 | } |
3bd791fa | 322 | my $no_bless = 0; |
4ab99479 YO |
323 | my $is_regex = 0; |
324 | if ( $realpack and ($] >= 5.009005 ? re::is_regexp($val) : $realpack eq 'Regexp') ) { | |
325 | $is_regex = 1; | |
326 | $no_bless = $realpack eq 'Regexp'; | |
a2126434 JN |
327 | } |
328 | ||
3bd791fa | 329 | # If purity is not set and maxdepth is set, then check depth: |
a2126434 JN |
330 | # if we have reached maximum depth, return the string |
331 | # representation of the thing we are currently examining | |
3bd791fa | 332 | # at this depth (i.e., 'Foo=ARRAY(0xdeadbeef)'). |
a2126434 | 333 | if (!$s->{purity} |
3bd791fa JK |
334 | and defined($s->{maxdepth}) |
335 | and $s->{maxdepth} > 0 | |
336 | and $s->{level} >= $s->{maxdepth}) | |
a2126434 JN |
337 | { |
338 | return qq['$val']; | |
339 | } | |
340 | ||
19be3be6 TC |
341 | # avoid recursing infinitely [perl #122111] |
342 | if ($s->{maxrecurse} > 0 | |
343 | and $s->{level} >= $s->{maxrecurse}) { | |
344 | die "Recursion limit of $s->{maxrecurse} exceeded"; | |
345 | } | |
346 | ||
a2126434 | 347 | # we have a blessed ref |
3bd791fa | 348 | my ($blesspad); |
4ab99479 | 349 | if ($realpack and !$no_bless) { |
a2126434 JN |
350 | $out = $s->{'bless'} . '( '; |
351 | $blesspad = $s->{apad}; | |
352 | $s->{apad} .= ' ' if ($s->{indent} >= 2); | |
7894fbab GS |
353 | } |
354 | ||
823edd99 | 355 | $s->{level}++; |
3bd791fa | 356 | my $ipad = $s->{xpad} x $s->{level}; |
823edd99 | 357 | |
4ab99479 YO |
358 | if ($is_regex) { |
359 | my $pat; | |
b183d514 TC |
360 | my $flags = ""; |
361 | if (defined(*re::regexp_pattern{CODE})) { | |
362 | ($pat, $flags) = re::regexp_pattern($val); | |
3bd791fa JK |
363 | } |
364 | else { | |
365 | $pat = "$val"; | |
4ab99479 | 366 | } |
de5ef703 | 367 | $pat =~ s <(\\.)|/> { $1 || '\\/' }ge; |
b183d514 | 368 | $out .= "qr/$pat/$flags"; |
4ab99479 | 369 | } |
d036e907 | 370 | elsif ($realtype eq 'SCALAR' || $realtype eq 'REF' |
3bd791fa | 371 | || $realtype eq 'VSTRING') { |
823edd99 | 372 | if ($realpack) { |
3bd791fa | 373 | $out .= 'do{\\(my $o = ' . $s->_dump($$val, "\${$name}") . ')}'; |
823edd99 GS |
374 | } |
375 | else { | |
3bd791fa | 376 | $out .= '\\' . $s->_dump($$val, "\${$name}"); |
823edd99 GS |
377 | } |
378 | } | |
379 | elsif ($realtype eq 'GLOB') { | |
3bd791fa | 380 | $out .= '\\' . $s->_dump($$val, "*{$name}"); |
823edd99 GS |
381 | } |
382 | elsif ($realtype eq 'ARRAY') { | |
a36ee16f | 383 | my($pad, $mname); |
823edd99 GS |
384 | my($i) = 0; |
385 | $out .= ($name =~ /^\@/) ? '(' : '['; | |
386 | $pad = $s->{sep} . $s->{pad} . $s->{apad}; | |
3bd791fa JK |
387 | ($name =~ /^\@(.*)$/) ? ($mname = "\$" . $1) : |
388 | # omit -> if $foo->[0]->{bar}, but not ${$foo->[0]}->{bar} | |
389 | ($name =~ /^\\?[\%\@\*\$][^{].*[]}]$/) ? ($mname = $name) : | |
390 | ($mname = $name . '->'); | |
823edd99 | 391 | $mname .= '->' if $mname =~ /^\*.+\{[A-Z]+\}$/; |
a36ee16f | 392 | for my $v (@$val) { |
3bd791fa JK |
393 | $sname = $mname . '[' . $i . ']'; |
394 | $out .= $pad . $ipad . '#' . $i | |
395 | if $s->{indent} >= 3; | |
396 | $out .= $pad . $ipad . $s->_dump($v, $sname); | |
7089d392 AC |
397 | $out .= "," |
398 | if $i++ < $#$val | |
399 | || ($s->{trailingcomma} && $s->{indent} >= 1); | |
823edd99 GS |
400 | } |
401 | $out .= $pad . ($s->{xpad} x ($s->{level} - 1)) if $i; | |
402 | $out .= ($name =~ /^\@/) ? ')' : ']'; | |
403 | } | |
404 | elsif ($realtype eq 'HASH') { | |
3bd791fa | 405 | my ($k, $v, $pad, $lpad, $mname, $pair); |
823edd99 GS |
406 | $out .= ($name =~ /^\%/) ? '(' : '{'; |
407 | $pad = $s->{sep} . $s->{pad} . $s->{apad}; | |
408 | $lpad = $s->{apad}; | |
30b4f386 | 409 | $pair = $s->{pair}; |
7820172a | 410 | ($name =~ /^\%(.*)$/) ? ($mname = "\$" . $1) : |
3bd791fa JK |
411 | # omit -> if $foo->[0]->{bar}, but not ${$foo->[0]}->{bar} |
412 | ($name =~ /^\\?[\%\@\*\$][^{].*[]}]$/) ? ($mname = $name) : | |
413 | ($mname = $name . '->'); | |
823edd99 | 414 | $mname .= '->' if $mname =~ /^\*.+\{[A-Z]+\}$/; |
3bd791fa JK |
415 | my $sortkeys = defined($s->{sortkeys}) ? $s->{sortkeys} : ''; |
416 | my $keys = []; | |
31a725b3 | 417 | if ($sortkeys) { |
3bd791fa JK |
418 | if (ref($s->{sortkeys}) eq 'CODE') { |
419 | $keys = $s->{sortkeys}($val); | |
420 | unless (ref($keys) eq 'ARRAY') { | |
a261571f | 421 | Carp::carp("Sortkeys subroutine did not return ARRAYREF"); |
3bd791fa JK |
422 | $keys = []; |
423 | } | |
424 | } | |
425 | else { | |
426 | $keys = [ sort keys %$val ]; | |
427 | } | |
31a725b3 | 428 | } |
b36d99fa AV |
429 | |
430 | # Ensure hash iterator is reset | |
431 | keys(%$val); | |
432 | ||
3bd791fa | 433 | my $key; |
31a725b3 | 434 | while (($k, $v) = ! $sortkeys ? (each %$val) : |
3bd791fa JK |
435 | @$keys ? ($key = shift(@$keys), $val->{$key}) : |
436 | () ) | |
31a725b3 | 437 | { |
3bd791fa | 438 | my $nk = $s->_dump($k, ""); |
5b50ddc0 TC |
439 | |
440 | # _dump doesn't quote numbers of this form | |
441 | if ($s->{quotekeys} && $nk =~ /^(?:0|-?[1-9][0-9]{0,8})\z/) { | |
442 | $nk = $s->{useqq} ? qq("$nk") : qq('$nk'); | |
443 | } | |
444 | elsif (!$s->{quotekeys} and $nk =~ /^[\"\']([A-Za-z_]\w*)[\"\']$/) { | |
445 | $nk = $1 | |
446 | } | |
447 | ||
3bd791fa JK |
448 | $sname = $mname . '{' . $nk . '}'; |
449 | $out .= $pad . $ipad . $nk . $pair; | |
450 | ||
451 | # temporarily alter apad | |
452 | $s->{apad} .= (" " x (length($nk) + 4)) | |
453 | if $s->{indent} >= 2; | |
454 | $out .= $s->_dump($val->{$k}, $sname) . ","; | |
455 | $s->{apad} = $lpad | |
456 | if $s->{indent} >= 2; | |
823edd99 GS |
457 | } |
458 | if (substr($out, -1) eq ',') { | |
7089d392 | 459 | chop $out if !$s->{trailingcomma} || !$s->{indent}; |
3bd791fa | 460 | $out .= $pad . ($s->{xpad} x ($s->{level} - 1)); |
823edd99 GS |
461 | } |
462 | $out .= ($name =~ /^\%/) ? ')' : '}'; | |
463 | } | |
464 | elsif ($realtype eq 'CODE') { | |
8e5f9a6e | 465 | if ($s->{deparse}) { |
3bd791fa JK |
466 | require B::Deparse; |
467 | my $sub = 'sub ' . (B::Deparse->new)->coderef2text($val); | |
2bc27c6c | 468 | my $pad = $s->{sep} . $s->{pad} . $s->{apad} . $s->{xpad} x ($s->{level} - 1); |
942cf643 | 469 | $sub =~ s/\n/$pad/gs; |
3bd791fa JK |
470 | $out .= $sub; |
471 | } | |
472 | else { | |
8e5f9a6e | 473 | $out .= 'sub { "DUMMY" }'; |
a261571f | 474 | Carp::carp("Encountered CODE ref, using dummy placeholder") if $s->{purity}; |
8e5f9a6e | 475 | } |
823edd99 GS |
476 | } |
477 | else { | |
a261571f | 478 | Carp::croak("Can't handle '$realtype' type"); |
823edd99 | 479 | } |
3bd791fa | 480 | |
4ab99479 | 481 | if ($realpack and !$no_bless) { # we have a blessed ref |
d0c214fd | 482 | $out .= ', ' . _quote($realpack) . ' )'; |
3bd791fa JK |
483 | $out .= '->' . $s->{toaster} . '()' |
484 | if $s->{toaster} ne ''; | |
823edd99 GS |
485 | $s->{apad} = $blesspad; |
486 | } | |
487 | $s->{level}--; | |
823edd99 GS |
488 | } |
489 | else { # simple scalar | |
490 | ||
491 | my $ref = \$_[1]; | |
d036e907 | 492 | my $v; |
823edd99 GS |
493 | # first, catalog the scalar |
494 | if ($name ne '') { | |
2728842d | 495 | $id = format_refaddr($ref); |
823edd99 | 496 | if (exists $s->{seen}{$id}) { |
7820172a | 497 | if ($s->{seen}{$id}[2]) { |
3bd791fa JK |
498 | $out = $s->{seen}{$id}[0]; |
499 | #warn "[<$out]\n"; | |
500 | return "\${$out}"; | |
501 | } | |
823edd99 GS |
502 | } |
503 | else { | |
3bd791fa JK |
504 | #warn "[>\\$name]\n"; |
505 | $s->{seen}{$id} = ["\\$name", $ref]; | |
823edd99 GS |
506 | } |
507 | } | |
c1205a1e FC |
508 | $ref = \$val; |
509 | if (ref($ref) eq 'GLOB') { # glob | |
823edd99 | 510 | my $name = substr($val, 1); |
abda9fe0 Z |
511 | $name =~ s/^main::(?!\z)/::/; |
512 | if ($name =~ /\A(?:[A-Z_a-z][0-9A-Z_a-z]*)?::(?:[0-9A-Z_a-z]+::)*[0-9A-Z_a-z]*\z/ && $name ne 'main::') { | |
3bd791fa | 513 | $sname = $name; |
823edd99 GS |
514 | } |
515 | else { | |
00ec40a9 | 516 | local $s->{useqq} = IS_PRE_516_PERL && ($s->{useqq} || $name =~ /[^\x00-\x7f]/) ? 1 : $s->{useqq}; |
3bd791fa | 517 | $sname = $s->_dump( |
ed904d96 | 518 | $name eq 'main::' |
3bd791fa JK |
519 | ? '' |
520 | : $name, | |
521 | "", | |
522 | ); | |
523 | $sname = '{' . $sname . '}'; | |
823edd99 GS |
524 | } |
525 | if ($s->{purity}) { | |
3bd791fa JK |
526 | my $k; |
527 | local ($s->{level}) = 0; | |
528 | for $k (qw(SCALAR ARRAY HASH)) { | |
529 | my $gval = *$val{$k}; | |
530 | next unless defined $gval; | |
531 | next if $k eq "SCALAR" && ! defined $$gval; # always there | |
532 | ||
533 | # _dump can push into @post, so we hold our place using $postlen | |
534 | my $postlen = scalar @post; | |
535 | $post[$postlen] = "\*$sname = "; | |
536 | local ($s->{apad}) = " " x length($post[$postlen]) if $s->{indent} >= 2; | |
537 | $post[$postlen] .= $s->_dump($gval, "\*$sname\{$k\}"); | |
538 | } | |
823edd99 GS |
539 | } |
540 | $out .= '*' . $sname; | |
541 | } | |
7820172a GS |
542 | elsif (!defined($val)) { |
543 | $out .= "undef"; | |
544 | } | |
d036e907 | 545 | elsif (defined &_vstring and $v = _vstring($val) |
3bd791fa | 546 | and !_bad_vsmg || eval $v eq $val) { |
d036e907 FC |
547 | $out .= $v; |
548 | } | |
549 | elsif (!defined &_vstring | |
c1205a1e | 550 | and ref $ref eq 'VSTRING' || eval{Scalar::Util::isvstring($val)}) { |
d036e907 FC |
551 | $out .= sprintf "%vd", $val; |
552 | } | |
5b50ddc0 TC |
553 | # \d here would treat "1\x{660}" as a safe decimal number |
554 | elsif ($val =~ /^(?:0|-?[1-9][0-9]{0,8})\z/) { # safe decimal number | |
823edd99 GS |
555 | $out .= $val; |
556 | } | |
3bd791fa | 557 | else { # string |
c4cce848 | 558 | if ($s->{useqq} or $val =~ tr/\0-\377//c) { |
38a44b82 | 559 | # Fall back to qq if there's Unicode |
3bd791fa | 560 | $out .= qquote($val, $s->{useqq}); |
823edd99 GS |
561 | } |
562 | else { | |
d0c214fd | 563 | $out .= _quote($val); |
823edd99 GS |
564 | } |
565 | } | |
566 | } | |
7820172a GS |
567 | if ($id) { |
568 | # if we made it this far, $id was added to seen list at current | |
569 | # level, so remove it to get deep copies | |
570 | if ($s->{deepcopy}) { | |
571 | delete($s->{seen}{$id}); | |
572 | } | |
573 | elsif ($name) { | |
574 | $s->{seen}{$id}[2] = 1; | |
575 | } | |
576 | } | |
823edd99 GS |
577 | return $out; |
578 | } | |
3bd791fa | 579 | |
823edd99 GS |
580 | # |
581 | # non-OO style of earlier version | |
582 | # | |
583 | sub Dumper { | |
584 | return Data::Dumper->Dump([@_]); | |
585 | } | |
586 | ||
0f1923bd | 587 | # compat stub |
823edd99 GS |
588 | sub DumperX { |
589 | return Data::Dumper->Dumpxs([@_], []); | |
590 | } | |
591 | ||
823edd99 | 592 | # |
3bd791fa | 593 | # reset the "seen" cache |
823edd99 GS |
594 | # |
595 | sub Reset { | |
596 | my($s) = shift; | |
597 | $s->{seen} = {}; | |
598 | return $s; | |
599 | } | |
600 | ||
601 | sub Indent { | |
602 | my($s, $v) = @_; | |
dd9e86b4 | 603 | if (@_ >= 2) { |
823edd99 GS |
604 | if ($v == 0) { |
605 | $s->{xpad} = ""; | |
606 | $s->{sep} = ""; | |
607 | } | |
608 | else { | |
609 | $s->{xpad} = " "; | |
610 | $s->{sep} = "\n"; | |
611 | } | |
612 | $s->{indent} = $v; | |
613 | return $s; | |
614 | } | |
615 | else { | |
616 | return $s->{indent}; | |
617 | } | |
618 | } | |
619 | ||
7089d392 AC |
620 | sub Trailingcomma { |
621 | my($s, $v) = @_; | |
dd9e86b4 | 622 | @_ >= 2 ? (($s->{trailingcomma} = $v), return $s) : $s->{trailingcomma}; |
7089d392 AC |
623 | } |
624 | ||
30b4f386 | 625 | sub Pair { |
626 | my($s, $v) = @_; | |
dd9e86b4 | 627 | @_ >= 2 ? (($s->{pair} = $v), return $s) : $s->{pair}; |
30b4f386 | 628 | } |
629 | ||
823edd99 GS |
630 | sub Pad { |
631 | my($s, $v) = @_; | |
dd9e86b4 | 632 | @_ >= 2 ? (($s->{pad} = $v), return $s) : $s->{pad}; |
823edd99 GS |
633 | } |
634 | ||
635 | sub Varname { | |
636 | my($s, $v) = @_; | |
dd9e86b4 | 637 | @_ >= 2 ? (($s->{varname} = $v), return $s) : $s->{varname}; |
823edd99 GS |
638 | } |
639 | ||
640 | sub Purity { | |
641 | my($s, $v) = @_; | |
dd9e86b4 | 642 | @_ >= 2 ? (($s->{purity} = $v), return $s) : $s->{purity}; |
823edd99 GS |
643 | } |
644 | ||
645 | sub Useqq { | |
646 | my($s, $v) = @_; | |
dd9e86b4 | 647 | @_ >= 2 ? (($s->{useqq} = $v), return $s) : $s->{useqq}; |
823edd99 GS |
648 | } |
649 | ||
650 | sub Terse { | |
651 | my($s, $v) = @_; | |
dd9e86b4 | 652 | @_ >= 2 ? (($s->{terse} = $v), return $s) : $s->{terse}; |
823edd99 GS |
653 | } |
654 | ||
655 | sub Freezer { | |
656 | my($s, $v) = @_; | |
dd9e86b4 | 657 | @_ >= 2 ? (($s->{freezer} = $v), return $s) : $s->{freezer}; |
823edd99 GS |
658 | } |
659 | ||
660 | sub Toaster { | |
661 | my($s, $v) = @_; | |
dd9e86b4 | 662 | @_ >= 2 ? (($s->{toaster} = $v), return $s) : $s->{toaster}; |
823edd99 GS |
663 | } |
664 | ||
665 | sub Deepcopy { | |
666 | my($s, $v) = @_; | |
dd9e86b4 | 667 | @_ >= 2 ? (($s->{deepcopy} = $v), return $s) : $s->{deepcopy}; |
823edd99 GS |
668 | } |
669 | ||
670 | sub Quotekeys { | |
671 | my($s, $v) = @_; | |
dd9e86b4 | 672 | @_ >= 2 ? (($s->{quotekeys} = $v), return $s) : $s->{quotekeys}; |
823edd99 GS |
673 | } |
674 | ||
675 | sub Bless { | |
676 | my($s, $v) = @_; | |
dd9e86b4 | 677 | @_ >= 2 ? (($s->{'bless'} = $v), return $s) : $s->{'bless'}; |
823edd99 GS |
678 | } |
679 | ||
a2126434 JN |
680 | sub Maxdepth { |
681 | my($s, $v) = @_; | |
dd9e86b4 | 682 | @_ >= 2 ? (($s->{'maxdepth'} = $v), return $s) : $s->{'maxdepth'}; |
a2126434 JN |
683 | } |
684 | ||
19be3be6 TC |
685 | sub Maxrecurse { |
686 | my($s, $v) = @_; | |
dd9e86b4 | 687 | @_ >= 2 ? (($s->{'maxrecurse'} = $v), return $s) : $s->{'maxrecurse'}; |
19be3be6 TC |
688 | } |
689 | ||
31a725b3 JH |
690 | sub Useperl { |
691 | my($s, $v) = @_; | |
dd9e86b4 | 692 | @_ >= 2 ? (($s->{'useperl'} = $v), return $s) : $s->{'useperl'}; |
31a725b3 JH |
693 | } |
694 | ||
695 | sub Sortkeys { | |
696 | my($s, $v) = @_; | |
dd9e86b4 | 697 | @_ >= 2 ? (($s->{'sortkeys'} = $v), return $s) : $s->{'sortkeys'}; |
31a725b3 JH |
698 | } |
699 | ||
8e5f9a6e RGS |
700 | sub Deparse { |
701 | my($s, $v) = @_; | |
dd9e86b4 | 702 | @_ >= 2 ? (($s->{'deparse'} = $v), return $s) : $s->{'deparse'}; |
8e5f9a6e | 703 | } |
a2126434 | 704 | |
d424882c S |
705 | sub Sparseseen { |
706 | my($s, $v) = @_; | |
dd9e86b4 | 707 | @_ >= 2 ? (($s->{'noseen'} = $v), return $s) : $s->{'noseen'}; |
d424882c S |
708 | } |
709 | ||
7820172a | 710 | # used by qquote below |
3bd791fa | 711 | my %esc = ( |
7820172a GS |
712 | "\a" => "\\a", |
713 | "\b" => "\\b", | |
714 | "\t" => "\\t", | |
715 | "\n" => "\\n", | |
716 | "\f" => "\\f", | |
717 | "\r" => "\\r", | |
718 | "\e" => "\\e", | |
719 | ); | |
720 | ||
31ac59b6 KW |
721 | my $low_controls = ($IS_ASCII) |
722 | ||
723 | # This includes \177, because traditionally it has been | |
724 | # output as octal, even though it isn't really a "low" | |
725 | # control | |
726 | ? qr/[\0-\x1f\177]/ | |
727 | ||
728 | # EBCDIC low controls. | |
729 | : qr/[\0-\x3f]/; | |
730 | ||
823edd99 GS |
731 | # put a string value in double quotes |
732 | sub qquote { | |
733 | local($_) = shift; | |
7820172a | 734 | s/([\\\"\@\$])/\\$1/g; |
31ac59b6 KW |
735 | |
736 | # This efficiently changes the high ordinal characters to \x{} if the utf8 | |
737 | # flag is on. On ASCII platforms, the high ordinals are all the | |
738 | # non-ASCII's. On EBCDIC platforms, we don't include in these the non-ASCII | |
739 | # controls whose ordinals are less than SPACE, excluded below by the range | |
740 | # \0-\x3f. On ASCII platforms this range just compiles as part of :ascii:. | |
741 | # On EBCDIC platforms, there is just one outlier high ordinal control, and | |
742 | # it gets output as \x{}. | |
dc71dc59 | 743 | my $bytes; { use bytes; $bytes = length } |
31ac59b6 KW |
744 | s/([^[:ascii:]\0-\x3f])/sprintf("\\x{%x}",ord($1))/ge |
745 | if $bytes > length | |
7820172a | 746 | |
31ac59b6 KW |
747 | # The above doesn't get the EBCDIC outlier high ordinal control when |
748 | # the string is UTF-8 but there are no UTF-8 variant characters in it. | |
749 | # We want that to come out as \x{} anyway. We need is_utf8() to do | |
750 | # this. | |
ed904d96 | 751 | || (! $IS_ASCII && utf8::is_utf8($_)); |
31ac59b6 | 752 | |
b8cae652 | 753 | return qq("$_") unless /[[:^print:]]/; # fast exit if only printables |
31ac59b6 KW |
754 | |
755 | # Here, there is at least one non-printable to output. First, translate the | |
756 | # escapes. | |
7820172a GS |
757 | s/([\a\b\t\n\f\r\e])/$esc{$1}/g; |
758 | ||
31ac59b6 KW |
759 | # no need for 3 digits in escape for octals not followed by a digit. |
760 | s/($low_controls)(?!\d)/'\\'.sprintf('%o',ord($1))/eg; | |
761 | ||
762 | # But otherwise use 3 digits | |
763 | s/($low_controls)/'\\'.sprintf('%03o',ord($1))/eg; | |
764 | ||
43948175 | 765 | # all but last branch below not supported --BEHAVIOR SUBJECT TO CHANGE-- |
31ac59b6 KW |
766 | my $high = shift || ""; |
767 | if ($high eq "iso8859") { # Doesn't escape the Latin1 printables | |
768 | if ($IS_ASCII) { | |
769 | s/([\200-\240])/'\\'.sprintf('%o',ord($1))/eg; | |
770 | } | |
ed904d96 | 771 | else { |
31ac59b6 KW |
772 | my $high_control = utf8::unicode_to_native(0x9F); |
773 | s/$high_control/sprintf('\\%o',ord($1))/eg; | |
774 | } | |
0407a77b | 775 | } elsif ($high eq "utf8") { |
31ac59b6 KW |
776 | # Some discussion of what to do here is in |
777 | # https://rt.perl.org/Ticket/Display.html?id=113088 | |
0407a77b GS |
778 | # use utf8; |
779 | # $str =~ s/([^\040-\176])/sprintf "\\x{%04x}", ord($1)/ge; | |
780 | } elsif ($high eq "8bit") { | |
781 | # leave it as it is | |
782 | } else { | |
31ac59b6 KW |
783 | s/([[:^ascii:]])/'\\'.sprintf('%03o',ord($1))/eg; |
784 | #s/([^\040-\176])/sprintf "\\x{%04x}", ord($1)/ge; | |
0407a77b | 785 | } |
0407a77b | 786 | |
7820172a | 787 | return qq("$_"); |
823edd99 GS |
788 | } |
789 | ||
fec5e1eb IM |
790 | # helper sub to sort hash keys in Perl < 5.8.0 where we don't have |
791 | # access to sortsv() from XS | |
792 | sub _sortkeys { [ sort keys %{$_[0]} ] } | |
793 | ||
3bd791fa JK |
794 | sub _refine_name { |
795 | my $s = shift; | |
796 | my ($name, $val, $i) = @_; | |
797 | if (defined $name) { | |
798 | if ($name =~ /^[*](.*)$/) { | |
799 | if (defined $val) { | |
800 | $name = (ref $val eq 'ARRAY') ? ( "\@" . $1 ) : | |
801 | (ref $val eq 'HASH') ? ( "\%" . $1 ) : | |
802 | (ref $val eq 'CODE') ? ( "\*" . $1 ) : | |
803 | ( "\$" . $1 ) ; | |
804 | } | |
805 | else { | |
806 | $name = "\$" . $1; | |
807 | } | |
808 | } | |
809 | elsif ($name !~ /^\$/) { | |
810 | $name = "\$" . $name; | |
811 | } | |
812 | } | |
813 | else { # no names provided | |
814 | $name = "\$" . $s->{varname} . $i; | |
815 | } | |
816 | return $name; | |
817 | } | |
818 | ||
819 | sub _compose_out { | |
820 | my $s = shift; | |
821 | my ($valstr, $postref) = @_; | |
822 | my $out = ""; | |
823 | $out .= $s->{pad} . $valstr . $s->{sep}; | |
824 | if (@{$postref}) { | |
825 | $out .= $s->{pad} . | |
826 | join(';' . $s->{sep} . $s->{pad}, @{$postref}) . | |
827 | ';' . | |
828 | $s->{sep}; | |
829 | } | |
830 | return $out; | |
831 | } | |
832 | ||
823edd99 GS |
833 | 1; |
834 | __END__ | |
835 | ||
836 | =head1 NAME | |
837 | ||
838 | Data::Dumper - stringified perl data structures, suitable for both printing and C<eval> | |
839 | ||
823edd99 GS |
840 | =head1 SYNOPSIS |
841 | ||
842 | use Data::Dumper; | |
843 | ||
844 | # simple procedural interface | |
845 | print Dumper($foo, $bar); | |
846 | ||
847 | # extended usage with names | |
848 | print Data::Dumper->Dump([$foo, $bar], [qw(foo *ary)]); | |
849 | ||
850 | # configuration variables | |
851 | { | |
82df27e1 | 852 | local $Data::Dumper::Purity = 1; |
823edd99 GS |
853 | eval Data::Dumper->Dump([$foo, $bar], [qw(foo *ary)]); |
854 | } | |
855 | ||
856 | # OO usage | |
857 | $d = Data::Dumper->new([$foo, $bar], [qw(foo *ary)]); | |
858 | ... | |
859 | print $d->Dump; | |
860 | ... | |
861 | $d->Purity(1)->Terse(1)->Deepcopy(1); | |
862 | eval $d->Dump; | |
863 | ||
864 | ||
865 | =head1 DESCRIPTION | |
866 | ||
867 | Given a list of scalars or reference variables, writes out their contents in | |
5e603302 | 868 | perl syntax. The references can also be objects. The content of each |
823edd99 GS |
869 | variable is output in a single Perl statement. Handles self-referential |
870 | structures correctly. | |
871 | ||
872 | The return value can be C<eval>ed to get back an identical copy of the | |
d22722a1 S |
873 | original reference structure. (Please do consider the security implications |
874 | of eval'ing code from untrusted sources!) | |
823edd99 GS |
875 | |
876 | Any references that are the same as one of those passed in will be named | |
877 | C<$VAR>I<n> (where I<n> is a numeric suffix), and other duplicate references | |
878 | to substructures within C<$VAR>I<n> will be appropriately labeled using arrow | |
879 | notation. You can specify names for individual values to be dumped if you | |
880 | use the C<Dump()> method, or you can change the default C<$VAR> prefix to | |
881 | something else. See C<$Data::Dumper::Varname> and C<$Data::Dumper::Terse> | |
882 | below. | |
883 | ||
884 | The default output of self-referential structures can be C<eval>ed, but the | |
885 | nested references to C<$VAR>I<n> will be undefined, since a recursive | |
886 | structure cannot be constructed using one Perl statement. You should set the | |
887 | C<Purity> flag to 1 to get additional statements that will correctly fill in | |
fc3a748c RGS |
888 | these references. Moreover, if C<eval>ed when strictures are in effect, |
889 | you need to ensure that any variables it accesses are previously declared. | |
823edd99 GS |
890 | |
891 | In the extended usage form, the references to be dumped can be given | |
3bd791fa | 892 | user-specified names. If a name begins with a C<*>, the output will |
823edd99 GS |
893 | describe the dereferenced type of the supplied reference for hashes and |
894 | arrays, and coderefs. Output of names will be avoided where possible if | |
895 | the C<Terse> flag is set. | |
896 | ||
897 | In many cases, methods that are used to set the internal state of the | |
898 | object will return the object itself, so method calls can be conveniently | |
899 | chained together. | |
900 | ||
901 | Several styles of output are possible, all controlled by setting | |
3bd791fa | 902 | the C<Indent> flag. See L<Configuration Variables or Methods> below |
823edd99 GS |
903 | for details. |
904 | ||
905 | ||
906 | =head2 Methods | |
907 | ||
908 | =over 4 | |
909 | ||
910 | =item I<PACKAGE>->new(I<ARRAYREF [>, I<ARRAYREF]>) | |
911 | ||
912 | Returns a newly created C<Data::Dumper> object. The first argument is an | |
913 | anonymous array of values to be dumped. The optional second argument is an | |
914 | anonymous array of names for the values. The names need not have a leading | |
915 | C<$> sign, and must be comprised of alphanumeric characters. You can begin | |
916 | a name with a C<*> to specify that the dereferenced type must be dumped | |
917 | instead of the reference itself, for ARRAY and HASH references. | |
918 | ||
919 | The prefix specified by C<$Data::Dumper::Varname> will be used with a | |
920 | numeric suffix if the name for a value is undefined. | |
921 | ||
922 | Data::Dumper will catalog all references encountered while dumping the | |
923 | values. Cross-references (in the form of names of substructures in perl | |
924 | syntax) will be inserted at all possible points, preserving any structural | |
925 | interdependencies in the original set of values. Structure traversal is | |
926 | depth-first, and proceeds in order from the first supplied value to | |
927 | the last. | |
928 | ||
929 | =item I<$OBJ>->Dump I<or> I<PACKAGE>->Dump(I<ARRAYREF [>, I<ARRAYREF]>) | |
930 | ||
931 | Returns the stringified form of the values stored in the object (preserving | |
932 | the order in which they were supplied to C<new>), subject to the | |
91e74348 | 933 | configuration options below. In a list context, it returns a list |
823edd99 GS |
934 | of strings corresponding to the supplied values. |
935 | ||
936 | The second form, for convenience, simply calls the C<new> method on its | |
937 | arguments before dumping the object immediately. | |
938 | ||
823edd99 GS |
939 | =item I<$OBJ>->Seen(I<[HASHREF]>) |
940 | ||
941 | Queries or adds to the internal table of already encountered references. | |
942 | You must use C<Reset> to explicitly clear the table if needed. Such | |
943 | references are not dumped; instead, their names are inserted wherever they | |
944 | are encountered subsequently. This is useful especially for properly | |
945 | dumping subroutine references. | |
946 | ||
d1be9408 | 947 | Expects an anonymous hash of name => value pairs. Same rules apply for names |
823edd99 | 948 | as in C<new>. If no argument is supplied, will return the "seen" list of |
91e74348 | 949 | name => value pairs, in a list context. Otherwise, returns the object |
823edd99 GS |
950 | itself. |
951 | ||
952 | =item I<$OBJ>->Values(I<[ARRAYREF]>) | |
953 | ||
3bd791fa JK |
954 | Queries or replaces the internal array of values that will be dumped. When |
955 | called without arguments, returns the values as a list. When called with a | |
956 | reference to an array of replacement values, returns the object itself. When | |
957 | called with any other type of argument, dies. | |
823edd99 GS |
958 | |
959 | =item I<$OBJ>->Names(I<[ARRAYREF]>) | |
960 | ||
961 | Queries or replaces the internal array of user supplied names for the values | |
3bd791fa JK |
962 | that will be dumped. When called without arguments, returns the names. When |
963 | called with an array of replacement names, returns the object itself. If the | |
436d4ccf | 964 | number of replacement names exceeds the number of values to be named, the |
3bd791fa | 965 | excess names will not be used. If the number of replacement names falls short |
436d4ccf | 966 | of the number of values to be named, the list of replacement names will be |
3bd791fa JK |
967 | exhausted and remaining values will not be renamed. When |
968 | called with any other type of argument, dies. | |
823edd99 GS |
969 | |
970 | =item I<$OBJ>->Reset | |
971 | ||
972 | Clears the internal table of "seen" references and returns the object | |
973 | itself. | |
974 | ||
975 | =back | |
976 | ||
977 | =head2 Functions | |
978 | ||
979 | =over 4 | |
980 | ||
981 | =item Dumper(I<LIST>) | |
982 | ||
983 | Returns the stringified form of the values in the list, subject to the | |
984 | configuration options below. The values will be named C<$VAR>I<n> in the | |
985 | output, where I<n> is a numeric suffix. Will return a list of strings | |
91e74348 | 986 | in a list context. |
823edd99 | 987 | |
823edd99 GS |
988 | =back |
989 | ||
990 | =head2 Configuration Variables or Methods | |
991 | ||
992 | Several configuration variables can be used to control the kind of output | |
993 | generated when using the procedural interface. These variables are usually | |
994 | C<local>ized in a block so that other parts of the code are not affected by | |
3bd791fa | 995 | the change. |
823edd99 GS |
996 | |
997 | These variables determine the default state of the object created by calling | |
998 | the C<new> method, but cannot be used to alter the state of the object | |
999 | thereafter. The equivalent method names should be used instead to query | |
1000 | or set the internal state of the object. | |
1001 | ||
1002 | The method forms return the object itself when called with arguments, | |
1003 | so that they can be chained together nicely. | |
1004 | ||
1005 | =over 4 | |
1006 | ||
28bf64cc JH |
1007 | =item * |
1008 | ||
1009 | $Data::Dumper::Indent I<or> I<$OBJ>->Indent(I<[NEWVAL]>) | |
823edd99 GS |
1010 | |
1011 | Controls the style of indentation. It can be set to 0, 1, 2 or 3. Style 0 | |
1c48faa6 JK |
1012 | spews output without any newlines, indentation, or spaces between list items. |
1013 | It is the most compact format possible that can still be called valid perl. | |
1014 | Style 1 outputs a readable form with newlines but no fancy indentation (each | |
1015 | level in the structure is simply indented by a fixed amount of whitespace). | |
1016 | Style 2 (the default) outputs a very readable form which lines up the hash | |
1017 | keys. Style 3 is like style 2, but also annotates the elements of arrays with | |
1018 | their index (but the comment is on its own line, so array output consumes | |
1019 | twice the number of lines). Style 2 is the default. | |
823edd99 | 1020 | |
28bf64cc JH |
1021 | =item * |
1022 | ||
7089d392 AC |
1023 | $Data::Dumper::Trailingcomma I<or> I<$OBJ>->Trailingcomma(I<[NEWVAL]>) |
1024 | ||
1025 | Controls whether a comma is added after the last element of an array or | |
1026 | hash. Even when true, no comma is added between the last element of an array | |
1027 | or hash and a closing bracket when they appear on the same line. The default | |
1028 | is false. | |
1029 | ||
1030 | =item * | |
1031 | ||
28bf64cc | 1032 | $Data::Dumper::Purity I<or> I<$OBJ>->Purity(I<[NEWVAL]>) |
823edd99 GS |
1033 | |
1034 | Controls the degree to which the output can be C<eval>ed to recreate the | |
1035 | supplied reference structures. Setting it to 1 will output additional perl | |
1036 | statements that will correctly recreate nested references. The default is | |
1037 | 0. | |
1038 | ||
28bf64cc JH |
1039 | =item * |
1040 | ||
1041 | $Data::Dumper::Pad I<or> I<$OBJ>->Pad(I<[NEWVAL]>) | |
823edd99 GS |
1042 | |
1043 | Specifies the string that will be prefixed to every line of the output. | |
1044 | Empty string by default. | |
1045 | ||
28bf64cc JH |
1046 | =item * |
1047 | ||
1048 | $Data::Dumper::Varname I<or> I<$OBJ>->Varname(I<[NEWVAL]>) | |
823edd99 GS |
1049 | |
1050 | Contains the prefix to use for tagging variable names in the output. The | |
1051 | default is "VAR". | |
1052 | ||
28bf64cc JH |
1053 | =item * |
1054 | ||
1055 | $Data::Dumper::Useqq I<or> I<$OBJ>->Useqq(I<[NEWVAL]>) | |
823edd99 GS |
1056 | |
1057 | When set, enables the use of double quotes for representing string values. | |
1058 | Whitespace other than space will be represented as C<[\n\t\r]>, "unsafe" | |
1059 | characters will be backslashed, and unprintable characters will be output as | |
aef2570a | 1060 | quoted octal integers. The default is 0. |
823edd99 | 1061 | |
28bf64cc JH |
1062 | =item * |
1063 | ||
1064 | $Data::Dumper::Terse I<or> I<$OBJ>->Terse(I<[NEWVAL]>) | |
823edd99 GS |
1065 | |
1066 | When set, Data::Dumper will emit single, non-self-referential values as | |
1067 | atoms/terms rather than statements. This means that the C<$VAR>I<n> names | |
1068 | will be avoided where possible, but be advised that such output may not | |
1069 | always be parseable by C<eval>. | |
1070 | ||
28bf64cc JH |
1071 | =item * |
1072 | ||
1073 | $Data::Dumper::Freezer I<or> $I<OBJ>->Freezer(I<[NEWVAL]>) | |
823edd99 GS |
1074 | |
1075 | Can be set to a method name, or to an empty string to disable the feature. | |
1076 | Data::Dumper will invoke that method via the object before attempting to | |
1077 | stringify it. This method can alter the contents of the object (if, for | |
1078 | instance, it contains data allocated from C), and even rebless it in a | |
1079 | different package. The client is responsible for making sure the specified | |
1080 | method can be called via the object, and that the object ends up containing | |
1081 | only perl data types after the method has been called. Defaults to an empty | |
1082 | string. | |
1083 | ||
c5f7c514 ST |
1084 | If an object does not support the method specified (determined using |
1085 | UNIVERSAL::can()) then the call will be skipped. If the method dies a | |
1086 | warning will be generated. | |
1087 | ||
28bf64cc JH |
1088 | =item * |
1089 | ||
1090 | $Data::Dumper::Toaster I<or> $I<OBJ>->Toaster(I<[NEWVAL]>) | |
823edd99 GS |
1091 | |
1092 | Can be set to a method name, or to an empty string to disable the feature. | |
1093 | Data::Dumper will emit a method call for any objects that are to be dumped | |
8e5f9a6e | 1094 | using the syntax C<bless(DATA, CLASS)-E<gt>METHOD()>. Note that this means that |
823edd99 GS |
1095 | the method specified will have to perform any modifications required on the |
1096 | object (like creating new state within it, and/or reblessing it in a | |
1097 | different package) and then return it. The client is responsible for making | |
1098 | sure the method can be called via the object, and that it returns a valid | |
1099 | object. Defaults to an empty string. | |
1100 | ||
28bf64cc JH |
1101 | =item * |
1102 | ||
1103 | $Data::Dumper::Deepcopy I<or> $I<OBJ>->Deepcopy(I<[NEWVAL]>) | |
823edd99 GS |
1104 | |
1105 | Can be set to a boolean value to enable deep copies of structures. | |
1106 | Cross-referencing will then only be done when absolutely essential | |
1107 | (i.e., to break reference cycles). Default is 0. | |
1108 | ||
28bf64cc JH |
1109 | =item * |
1110 | ||
1111 | $Data::Dumper::Quotekeys I<or> $I<OBJ>->Quotekeys(I<[NEWVAL]>) | |
823edd99 GS |
1112 | |
1113 | Can be set to a boolean value to control whether hash keys are quoted. | |
3bd791fa | 1114 | A defined false value will avoid quoting hash keys when it looks like a simple |
823edd99 GS |
1115 | string. Default is 1, which will always enclose hash keys in quotes. |
1116 | ||
28bf64cc JH |
1117 | =item * |
1118 | ||
1119 | $Data::Dumper::Bless I<or> $I<OBJ>->Bless(I<[NEWVAL]>) | |
823edd99 GS |
1120 | |
1121 | Can be set to a string that specifies an alternative to the C<bless> | |
1122 | builtin operator used to create objects. A function with the specified | |
1123 | name should exist, and should accept the same arguments as the builtin. | |
1124 | Default is C<bless>. | |
1125 | ||
28bf64cc JH |
1126 | =item * |
1127 | ||
30b4f386 | 1128 | $Data::Dumper::Pair I<or> $I<OBJ>->Pair(I<[NEWVAL]>) |
1129 | ||
1130 | Can be set to a string that specifies the separator between hash keys | |
1131 | and values. To dump nested hash, array and scalar values to JavaScript, | |
1132 | use: C<$Data::Dumper::Pair = ' : ';>. Implementing C<bless> in JavaScript | |
1133 | is left as an exercise for the reader. | |
1134 | A function with the specified name exists, and accepts the same arguments | |
1135 | as the builtin. | |
1136 | ||
1137 | Default is: C< =E<gt> >. | |
1138 | ||
1139 | =item * | |
1140 | ||
28bf64cc | 1141 | $Data::Dumper::Maxdepth I<or> $I<OBJ>->Maxdepth(I<[NEWVAL]>) |
a2126434 JN |
1142 | |
1143 | Can be set to a positive integer that specifies the depth beyond which | |
5e603302 | 1144 | we don't venture into a structure. Has no effect when |
a2126434 | 1145 | C<Data::Dumper::Purity> is set. (Useful in debugger when we often don't |
3bd791fa JK |
1146 | want to see more than enough). Default is 0, which means there is |
1147 | no maximum depth. | |
a2126434 | 1148 | |
28bf64cc JH |
1149 | =item * |
1150 | ||
19be3be6 TC |
1151 | $Data::Dumper::Maxrecurse I<or> $I<OBJ>->Maxrecurse(I<[NEWVAL]>) |
1152 | ||
1153 | Can be set to a positive integer that specifies the depth beyond which | |
1154 | recursion into a structure will throw an exception. This is intended | |
1155 | as a security measure to prevent perl running out of stack space when | |
1156 | dumping an excessively deep structure. Can be set to 0 to remove the | |
1157 | limit. Default is 1000. | |
1158 | ||
1159 | =item * | |
1160 | ||
28bf64cc | 1161 | $Data::Dumper::Useperl I<or> $I<OBJ>->Useperl(I<[NEWVAL]>) |
31a725b3 JH |
1162 | |
1163 | Can be set to a boolean value which controls whether the pure Perl | |
1164 | implementation of C<Data::Dumper> is used. The C<Data::Dumper> module is | |
1165 | a dual implementation, with almost all functionality written in both | |
1166 | pure Perl and also in XS ('C'). Since the XS version is much faster, it | |
1167 | will always be used if possible. This option lets you override the | |
1168 | default behavior, usually for testing purposes only. Default is 0, which | |
1169 | means the XS implementation will be used if possible. | |
1170 | ||
28bf64cc JH |
1171 | =item * |
1172 | ||
1173 | $Data::Dumper::Sortkeys I<or> $I<OBJ>->Sortkeys(I<[NEWVAL]>) | |
31a725b3 JH |
1174 | |
1175 | Can be set to a boolean value to control whether hash keys are dumped in | |
1176 | sorted order. A true value will cause the keys of all hashes to be | |
1177 | dumped in Perl's default sort order. Can also be set to a subroutine | |
1178 | reference which will be called for each hash that is dumped. In this | |
1179 | case C<Data::Dumper> will call the subroutine once for each hash, | |
1180 | passing it the reference of the hash. The purpose of the subroutine is | |
1181 | to return a reference to an array of the keys that will be dumped, in | |
1182 | the order that they should be dumped. Using this feature, you can | |
1183 | control both the order of the keys, and which keys are actually used. In | |
1184 | other words, this subroutine acts as a filter by which you can exclude | |
1185 | certain keys from being dumped. Default is 0, which means that hash keys | |
1186 | are not sorted. | |
1187 | ||
28bf64cc JH |
1188 | =item * |
1189 | ||
1190 | $Data::Dumper::Deparse I<or> $I<OBJ>->Deparse(I<[NEWVAL]>) | |
8e5f9a6e RGS |
1191 | |
1192 | Can be set to a boolean value to control whether code references are | |
1193 | turned into perl source code. If set to a true value, C<B::Deparse> | |
b5048e7b AC |
1194 | will be used to get the source of the code reference. In older versions, |
1195 | using this option imposed a significant performance penalty when dumping | |
1196 | parts of a data structure other than code references, but that is no | |
1197 | longer the case. | |
8e5f9a6e RGS |
1198 | |
1199 | Caution : use this option only if you know that your coderefs will be | |
1200 | properly reconstructed by C<B::Deparse>. | |
1201 | ||
d424882c S |
1202 | =item * |
1203 | ||
1204 | $Data::Dumper::Sparseseen I<or> $I<OBJ>->Sparseseen(I<[NEWVAL]>) | |
1205 | ||
1206 | By default, Data::Dumper builds up the "seen" hash of scalars that | |
1207 | it has encountered during serialization. This is very expensive. | |
1208 | This seen hash is necessary to support and even just detect circular | |
1209 | references. It is exposed to the user via the C<Seen()> call both | |
1210 | for writing and reading. | |
1211 | ||
1212 | If you, as a user, do not need explicit access to the "seen" hash, | |
1213 | then you can set the C<Sparseseen> option to allow Data::Dumper | |
1214 | to eschew building the "seen" hash for scalars that are known not | |
1215 | to possess more than one reference. This speeds up serialization | |
1216 | considerably if you use the XS implementation. | |
1217 | ||
1218 | Note: If you turn on C<Sparseseen>, then you must not rely on the | |
1219 | content of the seen hash since its contents will be an | |
1220 | implementation detail! | |
1221 | ||
823edd99 GS |
1222 | =back |
1223 | ||
1224 | =head2 Exports | |
1225 | ||
1226 | =over 4 | |
1227 | ||
1228 | =item Dumper | |
1229 | ||
1230 | =back | |
1231 | ||
1232 | =head1 EXAMPLES | |
1233 | ||
1234 | Run these code snippets to get a quick feel for the behavior of this | |
1235 | module. When you are through with these examples, you may want to | |
1236 | add or change the various configuration variables described above, | |
1237 | to see their behavior. (See the testsuite in the Data::Dumper | |
1238 | distribution for more examples.) | |
1239 | ||
1240 | ||
1241 | use Data::Dumper; | |
1242 | ||
1243 | package Foo; | |
1244 | sub new {bless {'a' => 1, 'b' => sub { return "foo" }}, $_[0]}; | |
1245 | ||
1246 | package Fuz; # a weird REF-REF-SCALAR object | |
1247 | sub new {bless \($_ = \ 'fu\'z'), $_[0]}; | |
1248 | ||
1249 | package main; | |
1250 | $foo = Foo->new; | |
1251 | $fuz = Fuz->new; | |
1252 | $boo = [ 1, [], "abcd", \*foo, | |
3bd791fa | 1253 | {1 => 'a', 023 => 'b', 0x45 => 'c'}, |
823edd99 | 1254 | \\"p\q\'r", $foo, $fuz]; |
3cb6de81 | 1255 | |
823edd99 GS |
1256 | ######## |
1257 | # simple usage | |
1258 | ######## | |
1259 | ||
1260 | $bar = eval(Dumper($boo)); | |
1261 | print($@) if $@; | |
1262 | print Dumper($boo), Dumper($bar); # pretty print (no array indices) | |
1263 | ||
b877fea2 FC |
1264 | $Data::Dumper::Terse = 1; # don't output names where feasible |
1265 | $Data::Dumper::Indent = 0; # turn off all pretty print | |
823edd99 GS |
1266 | print Dumper($boo), "\n"; |
1267 | ||
b877fea2 | 1268 | $Data::Dumper::Indent = 1; # mild pretty print |
823edd99 GS |
1269 | print Dumper($boo); |
1270 | ||
b877fea2 | 1271 | $Data::Dumper::Indent = 3; # pretty print with array indices |
823edd99 GS |
1272 | print Dumper($boo); |
1273 | ||
b877fea2 | 1274 | $Data::Dumper::Useqq = 1; # print strings in double quotes |
823edd99 | 1275 | print Dumper($boo); |
3cb6de81 | 1276 | |
b877fea2 | 1277 | $Data::Dumper::Pair = " : "; # specify hash key/value separator |
30b4f386 | 1278 | print Dumper($boo); |
1279 | ||
3cb6de81 | 1280 | |
823edd99 GS |
1281 | ######## |
1282 | # recursive structures | |
1283 | ######## | |
3cb6de81 | 1284 | |
823edd99 GS |
1285 | @c = ('c'); |
1286 | $c = \@c; | |
1287 | $b = {}; | |
1288 | $a = [1, $b, $c]; | |
1289 | $b->{a} = $a; | |
1290 | $b->{b} = $a->[1]; | |
1291 | $b->{c} = $a->[2]; | |
1292 | print Data::Dumper->Dump([$a,$b,$c], [qw(a b c)]); | |
3cb6de81 GS |
1293 | |
1294 | ||
823edd99 GS |
1295 | $Data::Dumper::Purity = 1; # fill in the holes for eval |
1296 | print Data::Dumper->Dump([$a, $b], [qw(*a b)]); # print as @a | |
1297 | print Data::Dumper->Dump([$b, $a], [qw(*b a)]); # print as %b | |
3cb6de81 GS |
1298 | |
1299 | ||
823edd99 GS |
1300 | $Data::Dumper::Deepcopy = 1; # avoid cross-refs |
1301 | print Data::Dumper->Dump([$b, $a], [qw(*b a)]); | |
3cb6de81 GS |
1302 | |
1303 | ||
823edd99 GS |
1304 | $Data::Dumper::Purity = 0; # avoid cross-refs |
1305 | print Data::Dumper->Dump([$b, $a], [qw(*b a)]); | |
3cb6de81 | 1306 | |
a2126434 JN |
1307 | ######## |
1308 | # deep structures | |
1309 | ######## | |
3cb6de81 | 1310 | |
a2126434 JN |
1311 | $a = "pearl"; |
1312 | $b = [ $a ]; | |
1313 | $c = { 'b' => $b }; | |
1314 | $d = [ $c ]; | |
1315 | $e = { 'd' => $d }; | |
1316 | $f = { 'e' => $e }; | |
1317 | print Data::Dumper->Dump([$f], [qw(f)]); | |
1318 | ||
1319 | $Data::Dumper::Maxdepth = 3; # no deeper than 3 refs down | |
1320 | print Data::Dumper->Dump([$f], [qw(f)]); | |
1321 | ||
3cb6de81 | 1322 | |
823edd99 GS |
1323 | ######## |
1324 | # object-oriented usage | |
1325 | ######## | |
3cb6de81 | 1326 | |
823edd99 GS |
1327 | $d = Data::Dumper->new([$a,$b], [qw(a b)]); |
1328 | $d->Seen({'*c' => $c}); # stash a ref without printing it | |
1329 | $d->Indent(3); | |
1330 | print $d->Dump; | |
1331 | $d->Reset->Purity(0); # empty the seen cache | |
1332 | print join "----\n", $d->Dump; | |
3cb6de81 GS |
1333 | |
1334 | ||
823edd99 GS |
1335 | ######## |
1336 | # persistence | |
1337 | ######## | |
3cb6de81 | 1338 | |
823edd99 GS |
1339 | package Foo; |
1340 | sub new { bless { state => 'awake' }, shift } | |
1341 | sub Freeze { | |
1342 | my $s = shift; | |
3bd791fa JK |
1343 | print STDERR "preparing to sleep\n"; |
1344 | $s->{state} = 'asleep'; | |
1345 | return bless $s, 'Foo::ZZZ'; | |
823edd99 | 1346 | } |
3cb6de81 | 1347 | |
823edd99 GS |
1348 | package Foo::ZZZ; |
1349 | sub Thaw { | |
1350 | my $s = shift; | |
3bd791fa JK |
1351 | print STDERR "waking up\n"; |
1352 | $s->{state} = 'awake'; | |
1353 | return bless $s, 'Foo'; | |
823edd99 | 1354 | } |
3cb6de81 | 1355 | |
3bd791fa | 1356 | package main; |
823edd99 GS |
1357 | use Data::Dumper; |
1358 | $a = Foo->new; | |
1359 | $b = Data::Dumper->new([$a], ['c']); | |
1360 | $b->Freezer('Freeze'); | |
1361 | $b->Toaster('Thaw'); | |
1362 | $c = $b->Dump; | |
1363 | print $c; | |
1364 | $d = eval $c; | |
1365 | print Data::Dumper->Dump([$d], ['d']); | |
3cb6de81 GS |
1366 | |
1367 | ||
823edd99 GS |
1368 | ######## |
1369 | # symbol substitution (useful for recreating CODE refs) | |
1370 | ######## | |
3cb6de81 | 1371 | |
823edd99 GS |
1372 | sub foo { print "foo speaking\n" } |
1373 | *other = \&foo; | |
1374 | $bar = [ \&other ]; | |
1375 | $d = Data::Dumper->new([\&other,$bar],['*other','bar']); | |
1376 | $d->Seen({ '*foo' => \&foo }); | |
1377 | print $d->Dump; | |
1378 | ||
1379 | ||
31a725b3 JH |
1380 | ######## |
1381 | # sorting and filtering hash keys | |
1382 | ######## | |
1383 | ||
1384 | $Data::Dumper::Sortkeys = \&my_filter; | |
1385 | my $foo = { map { (ord, "$_$_$_") } 'I'..'Q' }; | |
1386 | my $bar = { %$foo }; | |
1387 | my $baz = { reverse %$foo }; | |
1388 | print Dumper [ $foo, $bar, $baz ]; | |
1389 | ||
1390 | sub my_filter { | |
1391 | my ($hash) = @_; | |
1392 | # return an array ref containing the hash keys to dump | |
1393 | # in the order that you want them to be dumped | |
1394 | return [ | |
1395 | # Sort the keys of %$foo in reverse numeric order | |
1396 | $hash eq $foo ? (sort {$b <=> $a} keys %$hash) : | |
1397 | # Only dump the odd number keys of %$bar | |
1398 | $hash eq $bar ? (grep {$_ % 2} keys %$hash) : | |
1399 | # Sort keys in default order for all other hashes | |
1400 | (sort keys %$hash) | |
1401 | ]; | |
1402 | } | |
1403 | ||
823edd99 GS |
1404 | =head1 BUGS |
1405 | ||
1406 | Due to limitations of Perl subroutine call semantics, you cannot pass an | |
1407 | array or hash. Prepend it with a C<\> to pass its reference instead. This | |
8e5f9a6e RGS |
1408 | will be remedied in time, now that Perl has subroutine prototypes. |
1409 | For now, you need to use the extended usage form, and prepend the | |
823edd99 GS |
1410 | name with a C<*> to output it as a hash or array. |
1411 | ||
1412 | C<Data::Dumper> cheats with CODE references. If a code reference is | |
8e5f9a6e RGS |
1413 | encountered in the structure being processed (and if you haven't set |
1414 | the C<Deparse> flag), an anonymous subroutine that | |
823edd99 GS |
1415 | contains the string '"DUMMY"' will be inserted in its place, and a warning |
1416 | will be printed if C<Purity> is set. You can C<eval> the result, but bear | |
1417 | in mind that the anonymous sub that gets created is just a placeholder. | |
b5048e7b AC |
1418 | Even using the C<Deparse> flag will in some cases produce results that |
1419 | behave differently after being passed to C<eval>; see the documentation | |
1420 | for L<B::Deparse>. | |
823edd99 GS |
1421 | |
1422 | SCALAR objects have the weirdest looking C<bless> workaround. | |
1423 | ||
504f80c1 JH |
1424 | =head2 NOTE |
1425 | ||
ed904d96 | 1426 | Different runs of Perl will have different |
504f80c1 JH |
1427 | ordering of hash keys. The change was done for greater security, |
1428 | see L<perlsec/"Algorithmic Complexity Attacks">. This means that | |
1429 | different runs of Perl will have different Data::Dumper outputs if | |
1430 | the data contains hashes. If you need to have identical Data::Dumper | |
1431 | outputs from different runs of Perl, use the environment variable | |
1432 | PERL_HASH_SEED, see L<perlrun/PERL_HASH_SEED>. Using this restores | |
1433 | the old (platform-specific) ordering: an even prettier solution might | |
1434 | be to use the C<Sortkeys> filter of Data::Dumper. | |
823edd99 GS |
1435 | |
1436 | =head1 AUTHOR | |
1437 | ||
6e238990 | 1438 | Gurusamy Sarathy gsar@activestate.com |
823edd99 | 1439 | |
6d65cb5d | 1440 | Copyright (c) 1996-2019 Gurusamy Sarathy. All rights reserved. |
823edd99 GS |
1441 | This program is free software; you can redistribute it and/or |
1442 | modify it under the same terms as Perl itself. | |
1443 | ||
823edd99 GS |
1444 | =head1 VERSION |
1445 | ||
805d541f | 1446 | Version 2.180 |
823edd99 GS |
1447 | |
1448 | =head1 SEE ALSO | |
1449 | ||
1450 | perl(1) | |
1451 | ||
1452 | =cut |