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 10package Data::Dumper; 11 12BEGIN { 13 $VERSION = '2.151_01'; # Don't forget to set version and release 14} # date in POD below! 15 16#$| = 1; 17 18use 5.006_001; 19require Exporter; 20require overload; 21 22use Carp; 23 24BEGIN { 25 @ISA = qw(Exporter); 26 @EXPORT = qw(Dumper); 27 @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 eval { 33 require XSLoader; 34 XSLoader::load( 'Data::Dumper' ); 35 1 36 } 37 or $Useperl = 1; 38} 39 40# module vars and their defaults 41$Indent = 2 unless defined $Indent; 42$Purity = 0 unless defined $Purity; 43$Pad = "" unless defined $Pad; 44$Varname = "VAR" unless defined $Varname; 45$Useqq = 0 unless defined $Useqq; 46$Terse = 0 unless defined $Terse; 47$Freezer = "" unless defined $Freezer; 48$Toaster = "" unless defined $Toaster; 49$Deepcopy = 0 unless defined $Deepcopy; 50$Quotekeys = 1 unless defined $Quotekeys; 51$Bless = "bless" unless defined $Bless; 52#$Expdepth = 0 unless defined $Expdepth; 53$Maxdepth = 0 unless defined $Maxdepth; 54$Pair = ' => ' unless defined $Pair; 55$Useperl = 0 unless defined $Useperl; 56$Sortkeys = 0 unless defined $Sortkeys; 57$Deparse = 0 unless defined $Deparse; 58$Sparseseen = 0 unless defined $Sparseseen; 59$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# 67sub 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 117sub init_refaddr_format { 118} 119 120sub format_refaddr { 121 require Scalar::Util; 122 pack "J", Scalar::Util::refaddr(shift); 123}; 124 125if ($] < 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 142EOC 143} 144 145# 146# add-to or query the table of already seen references 147# 148sub 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# 186sub 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# 205sub 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 221sub DESTROY {} 222 223sub 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# 234sub 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) 264sub _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. 272use constant _bad_vsmg => defined &_vstring && (_vstring(~v0)||'') eq "v0"; 273 274# 275# twist, toil and turn; 276# and recurse, of course. 277# sometimes sordidly; 278# and curse if no recourse. 279# 280sub _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# 594sub Dumper { 595 return Data::Dumper->Dump([@_]); 596} 597 598# compat stub 599sub DumperX { 600 return Data::Dumper->Dumpxs([@_], []); 601} 602 603# 604# reset the "seen" cache 605# 606sub Reset { 607 my($s) = shift; 608 $s->{seen} = {}; 609 return $s; 610} 611 612sub 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 631sub Pair { 632 my($s, $v) = @_; 633 defined($v) ? (($s->{pair} = $v), return $s) : $s->{pair}; 634} 635 636sub Pad { 637 my($s, $v) = @_; 638 defined($v) ? (($s->{pad} = $v), return $s) : $s->{pad}; 639} 640 641sub Varname { 642 my($s, $v) = @_; 643 defined($v) ? (($s->{varname} = $v), return $s) : $s->{varname}; 644} 645 646sub Purity { 647 my($s, $v) = @_; 648 defined($v) ? (($s->{purity} = $v), return $s) : $s->{purity}; 649} 650 651sub Useqq { 652 my($s, $v) = @_; 653 defined($v) ? (($s->{useqq} = $v), return $s) : $s->{useqq}; 654} 655 656sub Terse { 657 my($s, $v) = @_; 658 defined($v) ? (($s->{terse} = $v), return $s) : $s->{terse}; 659} 660 661sub Freezer { 662 my($s, $v) = @_; 663 defined($v) ? (($s->{freezer} = $v), return $s) : $s->{freezer}; 664} 665 666sub Toaster { 667 my($s, $v) = @_; 668 defined($v) ? (($s->{toaster} = $v), return $s) : $s->{toaster}; 669} 670 671sub Deepcopy { 672 my($s, $v) = @_; 673 defined($v) ? (($s->{deepcopy} = $v), return $s) : $s->{deepcopy}; 674} 675 676sub Quotekeys { 677 my($s, $v) = @_; 678 defined($v) ? (($s->{quotekeys} = $v), return $s) : $s->{quotekeys}; 679} 680 681sub Bless { 682 my($s, $v) = @_; 683 defined($v) ? (($s->{'bless'} = $v), return $s) : $s->{'bless'}; 684} 685 686sub Maxdepth { 687 my($s, $v) = @_; 688 defined($v) ? (($s->{'maxdepth'} = $v), return $s) : $s->{'maxdepth'}; 689} 690 691sub Maxrecurse { 692 my($s, $v) = @_; 693 defined($v) ? (($s->{'maxrecurse'} = $v), return $s) : $s->{'maxrecurse'}; 694} 695 696sub Useperl { 697 my($s, $v) = @_; 698 defined($v) ? (($s->{'useperl'} = $v), return $s) : $s->{'useperl'}; 699} 700 701sub Sortkeys { 702 my($s, $v) = @_; 703 defined($v) ? (($s->{'sortkeys'} = $v), return $s) : $s->{'sortkeys'}; 704} 705 706sub Deparse { 707 my($s, $v) = @_; 708 defined($v) ? (($s->{'deparse'} = $v), return $s) : $s->{'deparse'}; 709} 710 711sub Sparseseen { 712 my($s, $v) = @_; 713 defined($v) ? (($s->{'noseen'} = $v), return $s) : $s->{'noseen'}; 714} 715 716# used by qquote below 717my %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 728sub qquote { 729 local($_) = shift; 730 s/([\\\"\@\$])/\\$1/g; 731 my $bytes; { use bytes; $bytes = length } 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 768sub _sortkeys { [ sort keys %{$_[0]} ] } 769 770sub _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 795sub _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 8091; 810__END__ 811 812=head1 NAME 813 814Data::Dumper - stringified perl data structures, suitable for both printing and C<eval> 815 816=head1 SYNOPSIS 817 818 use Data::Dumper; 819 820 # simple procedural interface 821 print Dumper($foo, $bar); 822 823 # extended usage with names 824 print Data::Dumper->Dump([$foo, $bar], [qw(foo *ary)]); 825 826 # configuration variables 827 { 828 local $Data::Dumper::Purity = 1; 829 eval Data::Dumper->Dump([$foo, $bar], [qw(foo *ary)]); 830 } 831 832 # OO usage 833 $d = Data::Dumper->new([$foo, $bar], [qw(foo *ary)]); 834 ... 835 print $d->Dump; 836 ... 837 $d->Purity(1)->Terse(1)->Deepcopy(1); 838 eval $d->Dump; 839 840 841=head1 DESCRIPTION 842 843Given a list of scalars or reference variables, writes out their contents in 844perl syntax. The references can also be objects. The content of each 845variable is output in a single Perl statement. Handles self-referential 846structures correctly. 847 848The return value can be C<eval>ed to get back an identical copy of the 849original reference structure. (Please do consider the security implications 850of eval'ing code from untrusted sources!) 851 852Any references that are the same as one of those passed in will be named 853C<$VAR>I<n> (where I<n> is a numeric suffix), and other duplicate references 854to substructures within C<$VAR>I<n> will be appropriately labeled using arrow 855notation. You can specify names for individual values to be dumped if you 856use the C<Dump()> method, or you can change the default C<$VAR> prefix to 857something else. See C<$Data::Dumper::Varname> and C<$Data::Dumper::Terse> 858below. 859 860The default output of self-referential structures can be C<eval>ed, but the 861nested references to C<$VAR>I<n> will be undefined, since a recursive 862structure cannot be constructed using one Perl statement. You should set the 863C<Purity> flag to 1 to get additional statements that will correctly fill in 864these references. Moreover, if C<eval>ed when strictures are in effect, 865you need to ensure that any variables it accesses are previously declared. 866 867In the extended usage form, the references to be dumped can be given 868user-specified names. If a name begins with a C<*>, the output will 869describe the dereferenced type of the supplied reference for hashes and 870arrays, and coderefs. Output of names will be avoided where possible if 871the C<Terse> flag is set. 872 873In many cases, methods that are used to set the internal state of the 874object will return the object itself, so method calls can be conveniently 875chained together. 876 877Several styles of output are possible, all controlled by setting 878the C<Indent> flag. See L<Configuration Variables or Methods> below 879for details. 880 881 882=head2 Methods 883 884=over 4 885 886=item I<PACKAGE>->new(I<ARRAYREF [>, I<ARRAYREF]>) 887 888Returns a newly created C<Data::Dumper> object. The first argument is an 889anonymous array of values to be dumped. The optional second argument is an 890anonymous array of names for the values. The names need not have a leading 891C<$> sign, and must be comprised of alphanumeric characters. You can begin 892a name with a C<*> to specify that the dereferenced type must be dumped 893instead of the reference itself, for ARRAY and HASH references. 894 895The prefix specified by C<$Data::Dumper::Varname> will be used with a 896numeric suffix if the name for a value is undefined. 897 898Data::Dumper will catalog all references encountered while dumping the 899values. Cross-references (in the form of names of substructures in perl 900syntax) will be inserted at all possible points, preserving any structural 901interdependencies in the original set of values. Structure traversal is 902depth-first, and proceeds in order from the first supplied value to 903the last. 904 905=item I<$OBJ>->Dump I<or> I<PACKAGE>->Dump(I<ARRAYREF [>, I<ARRAYREF]>) 906 907Returns the stringified form of the values stored in the object (preserving 908the order in which they were supplied to C<new>), subject to the 909configuration options below. In a list context, it returns a list 910of strings corresponding to the supplied values. 911 912The second form, for convenience, simply calls the C<new> method on its 913arguments before dumping the object immediately. 914 915=item I<$OBJ>->Seen(I<[HASHREF]>) 916 917Queries or adds to the internal table of already encountered references. 918You must use C<Reset> to explicitly clear the table if needed. Such 919references are not dumped; instead, their names are inserted wherever they 920are encountered subsequently. This is useful especially for properly 921dumping subroutine references. 922 923Expects an anonymous hash of name => value pairs. Same rules apply for names 924as in C<new>. If no argument is supplied, will return the "seen" list of 925name => value pairs, in a list context. Otherwise, returns the object 926itself. 927 928=item I<$OBJ>->Values(I<[ARRAYREF]>) 929 930Queries or replaces the internal array of values that will be dumped. When 931called without arguments, returns the values as a list. When called with a 932reference to an array of replacement values, returns the object itself. When 933called with any other type of argument, dies. 934 935=item I<$OBJ>->Names(I<[ARRAYREF]>) 936 937Queries or replaces the internal array of user supplied names for the values 938that will be dumped. When called without arguments, returns the names. When 939called with an array of replacement names, returns the object itself. If the 940number of replacement names exceeds the number of values to be named, the 941excess names will not be used. If the number of replacement names falls short 942of the number of values to be named, the list of replacement names will be 943exhausted and remaining values will not be renamed. When 944called with any other type of argument, dies. 945 946=item I<$OBJ>->Reset 947 948Clears the internal table of "seen" references and returns the object 949itself. 950 951=back 952 953=head2 Functions 954 955=over 4 956 957=item Dumper(I<LIST>) 958 959Returns the stringified form of the values in the list, subject to the 960configuration options below. The values will be named C<$VAR>I<n> in the 961output, where I<n> is a numeric suffix. Will return a list of strings 962in a list context. 963 964=back 965 966=head2 Configuration Variables or Methods 967 968Several configuration variables can be used to control the kind of output 969generated when using the procedural interface. These variables are usually 970C<local>ized in a block so that other parts of the code are not affected by 971the change. 972 973These variables determine the default state of the object created by calling 974the C<new> method, but cannot be used to alter the state of the object 975thereafter. The equivalent method names should be used instead to query 976or set the internal state of the object. 977 978The method forms return the object itself when called with arguments, 979so that they can be chained together nicely. 980 981=over 4 982 983=item * 984 985$Data::Dumper::Indent I<or> I<$OBJ>->Indent(I<[NEWVAL]>) 986 987Controls the style of indentation. It can be set to 0, 1, 2 or 3. Style 0 988spews output without any newlines, indentation, or spaces between list 989items. It is the most compact format possible that can still be called 990valid perl. Style 1 outputs a readable form with newlines but no fancy 991indentation (each level in the structure is simply indented by a fixed 992amount of whitespace). Style 2 (the default) outputs a very readable form 993which takes into account the length of hash keys (so the hash value lines 994up). Style 3 is like style 2, but also annotates the elements of arrays 995with their index (but the comment is on its own line, so array output 996consumes twice the number of lines). Style 2 is the default. 997 998=item * 999 1000$Data::Dumper::Purity I<or> I<$OBJ>->Purity(I<[NEWVAL]>) 1001 1002Controls the degree to which the output can be C<eval>ed to recreate the 1003supplied reference structures. Setting it to 1 will output additional perl 1004statements that will correctly recreate nested references. The default is 10050. 1006 1007=item * 1008 1009$Data::Dumper::Pad I<or> I<$OBJ>->Pad(I<[NEWVAL]>) 1010 1011Specifies the string that will be prefixed to every line of the output. 1012Empty string by default. 1013 1014=item * 1015 1016$Data::Dumper::Varname I<or> I<$OBJ>->Varname(I<[NEWVAL]>) 1017 1018Contains the prefix to use for tagging variable names in the output. The 1019default is "VAR". 1020 1021=item * 1022 1023$Data::Dumper::Useqq I<or> I<$OBJ>->Useqq(I<[NEWVAL]>) 1024 1025When set, enables the use of double quotes for representing string values. 1026Whitespace other than space will be represented as C<[\n\t\r]>, "unsafe" 1027characters will be backslashed, and unprintable characters will be output as 1028quoted octal integers. Since setting this variable imposes a performance 1029penalty, the default is 0. C<Dump()> will run slower if this flag is set, 1030since the fast XSUB implementation doesn't support it yet. 1031 1032=item * 1033 1034$Data::Dumper::Terse I<or> I<$OBJ>->Terse(I<[NEWVAL]>) 1035 1036When set, Data::Dumper will emit single, non-self-referential values as 1037atoms/terms rather than statements. This means that the C<$VAR>I<n> names 1038will be avoided where possible, but be advised that such output may not 1039always be parseable by C<eval>. 1040 1041=item * 1042 1043$Data::Dumper::Freezer I<or> $I<OBJ>->Freezer(I<[NEWVAL]>) 1044 1045Can be set to a method name, or to an empty string to disable the feature. 1046Data::Dumper will invoke that method via the object before attempting to 1047stringify it. This method can alter the contents of the object (if, for 1048instance, it contains data allocated from C), and even rebless it in a 1049different package. The client is responsible for making sure the specified 1050method can be called via the object, and that the object ends up containing 1051only perl data types after the method has been called. Defaults to an empty 1052string. 1053 1054If an object does not support the method specified (determined using 1055UNIVERSAL::can()) then the call will be skipped. If the method dies a 1056warning will be generated. 1057 1058=item * 1059 1060$Data::Dumper::Toaster I<or> $I<OBJ>->Toaster(I<[NEWVAL]>) 1061 1062Can be set to a method name, or to an empty string to disable the feature. 1063Data::Dumper will emit a method call for any objects that are to be dumped 1064using the syntax C<bless(DATA, CLASS)-E<gt>METHOD()>. Note that this means that 1065the method specified will have to perform any modifications required on the 1066object (like creating new state within it, and/or reblessing it in a 1067different package) and then return it. The client is responsible for making 1068sure the method can be called via the object, and that it returns a valid 1069object. Defaults to an empty string. 1070 1071=item * 1072 1073$Data::Dumper::Deepcopy I<or> $I<OBJ>->Deepcopy(I<[NEWVAL]>) 1074 1075Can be set to a boolean value to enable deep copies of structures. 1076Cross-referencing will then only be done when absolutely essential 1077(i.e., to break reference cycles). Default is 0. 1078 1079=item * 1080 1081$Data::Dumper::Quotekeys I<or> $I<OBJ>->Quotekeys(I<[NEWVAL]>) 1082 1083Can be set to a boolean value to control whether hash keys are quoted. 1084A defined false value will avoid quoting hash keys when it looks like a simple 1085string. Default is 1, which will always enclose hash keys in quotes. 1086 1087=item * 1088 1089$Data::Dumper::Bless I<or> $I<OBJ>->Bless(I<[NEWVAL]>) 1090 1091Can be set to a string that specifies an alternative to the C<bless> 1092builtin operator used to create objects. A function with the specified 1093name should exist, and should accept the same arguments as the builtin. 1094Default is C<bless>. 1095 1096=item * 1097 1098$Data::Dumper::Pair I<or> $I<OBJ>->Pair(I<[NEWVAL]>) 1099 1100Can be set to a string that specifies the separator between hash keys 1101and values. To dump nested hash, array and scalar values to JavaScript, 1102use: C<$Data::Dumper::Pair = ' : ';>. Implementing C<bless> in JavaScript 1103is left as an exercise for the reader. 1104A function with the specified name exists, and accepts the same arguments 1105as the builtin. 1106 1107Default is: C< =E<gt> >. 1108 1109=item * 1110 1111$Data::Dumper::Maxdepth I<or> $I<OBJ>->Maxdepth(I<[NEWVAL]>) 1112 1113Can be set to a positive integer that specifies the depth beyond which 1114we don't venture into a structure. Has no effect when 1115C<Data::Dumper::Purity> is set. (Useful in debugger when we often don't 1116want to see more than enough). Default is 0, which means there is 1117no maximum depth. 1118 1119=item * 1120 1121$Data::Dumper::Maxrecurse I<or> $I<OBJ>->Maxrecurse(I<[NEWVAL]>) 1122 1123Can be set to a positive integer that specifies the depth beyond which 1124recursion into a structure will throw an exception. This is intended 1125as a security measure to prevent perl running out of stack space when 1126dumping an excessively deep structure. Can be set to 0 to remove the 1127limit. Default is 1000. 1128 1129=item * 1130 1131$Data::Dumper::Useperl I<or> $I<OBJ>->Useperl(I<[NEWVAL]>) 1132 1133Can be set to a boolean value which controls whether the pure Perl 1134implementation of C<Data::Dumper> is used. The C<Data::Dumper> module is 1135a dual implementation, with almost all functionality written in both 1136pure Perl and also in XS ('C'). Since the XS version is much faster, it 1137will always be used if possible. This option lets you override the 1138default behavior, usually for testing purposes only. Default is 0, which 1139means the XS implementation will be used if possible. 1140 1141=item * 1142 1143$Data::Dumper::Sortkeys I<or> $I<OBJ>->Sortkeys(I<[NEWVAL]>) 1144 1145Can be set to a boolean value to control whether hash keys are dumped in 1146sorted order. A true value will cause the keys of all hashes to be 1147dumped in Perl's default sort order. Can also be set to a subroutine 1148reference which will be called for each hash that is dumped. In this 1149case C<Data::Dumper> will call the subroutine once for each hash, 1150passing it the reference of the hash. The purpose of the subroutine is 1151to return a reference to an array of the keys that will be dumped, in 1152the order that they should be dumped. Using this feature, you can 1153control both the order of the keys, and which keys are actually used. In 1154other words, this subroutine acts as a filter by which you can exclude 1155certain keys from being dumped. Default is 0, which means that hash keys 1156are not sorted. 1157 1158=item * 1159 1160$Data::Dumper::Deparse I<or> $I<OBJ>->Deparse(I<[NEWVAL]>) 1161 1162Can be set to a boolean value to control whether code references are 1163turned into perl source code. If set to a true value, C<B::Deparse> 1164will be used to get the source of the code reference. Using this option 1165will force using the Perl implementation of the dumper, since the fast 1166XSUB implementation doesn't support it. 1167 1168Caution : use this option only if you know that your coderefs will be 1169properly reconstructed by C<B::Deparse>. 1170 1171=item * 1172 1173$Data::Dumper::Sparseseen I<or> $I<OBJ>->Sparseseen(I<[NEWVAL]>) 1174 1175By default, Data::Dumper builds up the "seen" hash of scalars that 1176it has encountered during serialization. This is very expensive. 1177This seen hash is necessary to support and even just detect circular 1178references. It is exposed to the user via the C<Seen()> call both 1179for writing and reading. 1180 1181If you, as a user, do not need explicit access to the "seen" hash, 1182then you can set the C<Sparseseen> option to allow Data::Dumper 1183to eschew building the "seen" hash for scalars that are known not 1184to possess more than one reference. This speeds up serialization 1185considerably if you use the XS implementation. 1186 1187Note: If you turn on C<Sparseseen>, then you must not rely on the 1188content of the seen hash since its contents will be an 1189implementation detail! 1190 1191=back 1192 1193=head2 Exports 1194 1195=over 4 1196 1197=item Dumper 1198 1199=back 1200 1201=head1 EXAMPLES 1202 1203Run these code snippets to get a quick feel for the behavior of this 1204module. When you are through with these examples, you may want to 1205add or change the various configuration variables described above, 1206to see their behavior. (See the testsuite in the Data::Dumper 1207distribution for more examples.) 1208 1209 1210 use Data::Dumper; 1211 1212 package Foo; 1213 sub new {bless {'a' => 1, 'b' => sub { return "foo" }}, $_[0]}; 1214 1215 package Fuz; # a weird REF-REF-SCALAR object 1216 sub new {bless \($_ = \ 'fu\'z'), $_[0]}; 1217 1218 package main; 1219 $foo = Foo->new; 1220 $fuz = Fuz->new; 1221 $boo = [ 1, [], "abcd", \*foo, 1222 {1 => 'a', 023 => 'b', 0x45 => 'c'}, 1223 \\"p\q\'r", $foo, $fuz]; 1224 1225 ######## 1226 # simple usage 1227 ######## 1228 1229 $bar = eval(Dumper($boo)); 1230 print($@) if $@; 1231 print Dumper($boo), Dumper($bar); # pretty print (no array indices) 1232 1233 $Data::Dumper::Terse = 1; # don't output names where feasible 1234 $Data::Dumper::Indent = 0; # turn off all pretty print 1235 print Dumper($boo), "\n"; 1236 1237 $Data::Dumper::Indent = 1; # mild pretty print 1238 print Dumper($boo); 1239 1240 $Data::Dumper::Indent = 3; # pretty print with array indices 1241 print Dumper($boo); 1242 1243 $Data::Dumper::Useqq = 1; # print strings in double quotes 1244 print Dumper($boo); 1245 1246 $Data::Dumper::Pair = " : "; # specify hash key/value separator 1247 print Dumper($boo); 1248 1249 1250 ######## 1251 # recursive structures 1252 ######## 1253 1254 @c = ('c'); 1255 $c = \@c; 1256 $b = {}; 1257 $a = [1, $b, $c]; 1258 $b->{a} = $a; 1259 $b->{b} = $a->[1]; 1260 $b->{c} = $a->[2]; 1261 print Data::Dumper->Dump([$a,$b,$c], [qw(a b c)]); 1262 1263 1264 $Data::Dumper::Purity = 1; # fill in the holes for eval 1265 print Data::Dumper->Dump([$a, $b], [qw(*a b)]); # print as @a 1266 print Data::Dumper->Dump([$b, $a], [qw(*b a)]); # print as %b 1267 1268 1269 $Data::Dumper::Deepcopy = 1; # avoid cross-refs 1270 print Data::Dumper->Dump([$b, $a], [qw(*b a)]); 1271 1272 1273 $Data::Dumper::Purity = 0; # avoid cross-refs 1274 print Data::Dumper->Dump([$b, $a], [qw(*b a)]); 1275 1276 ######## 1277 # deep structures 1278 ######## 1279 1280 $a = "pearl"; 1281 $b = [ $a ]; 1282 $c = { 'b' => $b }; 1283 $d = [ $c ]; 1284 $e = { 'd' => $d }; 1285 $f = { 'e' => $e }; 1286 print Data::Dumper->Dump([$f], [qw(f)]); 1287 1288 $Data::Dumper::Maxdepth = 3; # no deeper than 3 refs down 1289 print Data::Dumper->Dump([$f], [qw(f)]); 1290 1291 1292 ######## 1293 # object-oriented usage 1294 ######## 1295 1296 $d = Data::Dumper->new([$a,$b], [qw(a b)]); 1297 $d->Seen({'*c' => $c}); # stash a ref without printing it 1298 $d->Indent(3); 1299 print $d->Dump; 1300 $d->Reset->Purity(0); # empty the seen cache 1301 print join "----\n", $d->Dump; 1302 1303 1304 ######## 1305 # persistence 1306 ######## 1307 1308 package Foo; 1309 sub new { bless { state => 'awake' }, shift } 1310 sub Freeze { 1311 my $s = shift; 1312 print STDERR "preparing to sleep\n"; 1313 $s->{state} = 'asleep'; 1314 return bless $s, 'Foo::ZZZ'; 1315 } 1316 1317 package Foo::ZZZ; 1318 sub Thaw { 1319 my $s = shift; 1320 print STDERR "waking up\n"; 1321 $s->{state} = 'awake'; 1322 return bless $s, 'Foo'; 1323 } 1324 1325 package main; 1326 use Data::Dumper; 1327 $a = Foo->new; 1328 $b = Data::Dumper->new([$a], ['c']); 1329 $b->Freezer('Freeze'); 1330 $b->Toaster('Thaw'); 1331 $c = $b->Dump; 1332 print $c; 1333 $d = eval $c; 1334 print Data::Dumper->Dump([$d], ['d']); 1335 1336 1337 ######## 1338 # symbol substitution (useful for recreating CODE refs) 1339 ######## 1340 1341 sub foo { print "foo speaking\n" } 1342 *other = \&foo; 1343 $bar = [ \&other ]; 1344 $d = Data::Dumper->new([\&other,$bar],['*other','bar']); 1345 $d->Seen({ '*foo' => \&foo }); 1346 print $d->Dump; 1347 1348 1349 ######## 1350 # sorting and filtering hash keys 1351 ######## 1352 1353 $Data::Dumper::Sortkeys = \&my_filter; 1354 my $foo = { map { (ord, "$_$_$_") } 'I'..'Q' }; 1355 my $bar = { %$foo }; 1356 my $baz = { reverse %$foo }; 1357 print Dumper [ $foo, $bar, $baz ]; 1358 1359 sub my_filter { 1360 my ($hash) = @_; 1361 # return an array ref containing the hash keys to dump 1362 # in the order that you want them to be dumped 1363 return [ 1364 # Sort the keys of %$foo in reverse numeric order 1365 $hash eq $foo ? (sort {$b <=> $a} keys %$hash) : 1366 # Only dump the odd number keys of %$bar 1367 $hash eq $bar ? (grep {$_ % 2} keys %$hash) : 1368 # Sort keys in default order for all other hashes 1369 (sort keys %$hash) 1370 ]; 1371 } 1372 1373=head1 BUGS 1374 1375Due to limitations of Perl subroutine call semantics, you cannot pass an 1376array or hash. Prepend it with a C<\> to pass its reference instead. This 1377will be remedied in time, now that Perl has subroutine prototypes. 1378For now, you need to use the extended usage form, and prepend the 1379name with a C<*> to output it as a hash or array. 1380 1381C<Data::Dumper> cheats with CODE references. If a code reference is 1382encountered in the structure being processed (and if you haven't set 1383the C<Deparse> flag), an anonymous subroutine that 1384contains the string '"DUMMY"' will be inserted in its place, and a warning 1385will be printed if C<Purity> is set. You can C<eval> the result, but bear 1386in mind that the anonymous sub that gets created is just a placeholder. 1387Someday, perl will have a switch to cache-on-demand the string 1388representation of a compiled piece of code, I hope. If you have prior 1389knowledge of all the code refs that your data structures are likely 1390to have, you can use the C<Seen> method to pre-seed the internal reference 1391table and make the dumped output point to them, instead. See L</EXAMPLES> 1392above. 1393 1394The C<Useqq> and C<Deparse> flags makes Dump() run slower, since the 1395XSUB implementation does not support them. 1396 1397SCALAR objects have the weirdest looking C<bless> workaround. 1398 1399Pure Perl version of C<Data::Dumper> escapes UTF-8 strings correctly 1400only in Perl 5.8.0 and later. 1401 1402=head2 NOTE 1403 1404Starting from Perl 5.8.1 different runs of Perl will have different 1405ordering of hash keys. The change was done for greater security, 1406see L<perlsec/"Algorithmic Complexity Attacks">. This means that 1407different runs of Perl will have different Data::Dumper outputs if 1408the data contains hashes. If you need to have identical Data::Dumper 1409outputs from different runs of Perl, use the environment variable 1410PERL_HASH_SEED, see L<perlrun/PERL_HASH_SEED>. Using this restores 1411the old (platform-specific) ordering: an even prettier solution might 1412be to use the C<Sortkeys> filter of Data::Dumper. 1413 1414=head1 AUTHOR 1415 1416Gurusamy Sarathy gsar@activestate.com 1417 1418Copyright (c) 1996-2014 Gurusamy Sarathy. All rights reserved. 1419This program is free software; you can redistribute it and/or 1420modify it under the same terms as Perl itself. 1421 1422=head1 VERSION 1423 1424Version 2.151_01 (January 8 2015) 1425 1426=head1 SEE ALSO 1427 1428perl(1) 1429 1430=cut 1431