1*0Sstevel@tonic-gate# 2*0Sstevel@tonic-gate# Data/Dumper.pm 3*0Sstevel@tonic-gate# 4*0Sstevel@tonic-gate# convert perl data structures into perl syntax suitable for both printing 5*0Sstevel@tonic-gate# and eval 6*0Sstevel@tonic-gate# 7*0Sstevel@tonic-gate# Documentation at the __END__ 8*0Sstevel@tonic-gate# 9*0Sstevel@tonic-gate 10*0Sstevel@tonic-gatepackage Data::Dumper; 11*0Sstevel@tonic-gate 12*0Sstevel@tonic-gate$VERSION = '2.121'; 13*0Sstevel@tonic-gate 14*0Sstevel@tonic-gate#$| = 1; 15*0Sstevel@tonic-gate 16*0Sstevel@tonic-gateuse 5.006_001; 17*0Sstevel@tonic-gaterequire Exporter; 18*0Sstevel@tonic-gateuse XSLoader (); 19*0Sstevel@tonic-gaterequire overload; 20*0Sstevel@tonic-gate 21*0Sstevel@tonic-gateuse Carp; 22*0Sstevel@tonic-gate 23*0Sstevel@tonic-gate@ISA = qw(Exporter); 24*0Sstevel@tonic-gate@EXPORT = qw(Dumper); 25*0Sstevel@tonic-gate@EXPORT_OK = qw(DumperX); 26*0Sstevel@tonic-gate 27*0Sstevel@tonic-gateXSLoader::load 'Data::Dumper'; 28*0Sstevel@tonic-gate 29*0Sstevel@tonic-gate# module vars and their defaults 30*0Sstevel@tonic-gate$Indent = 2 unless defined $Indent; 31*0Sstevel@tonic-gate$Purity = 0 unless defined $Purity; 32*0Sstevel@tonic-gate$Pad = "" unless defined $Pad; 33*0Sstevel@tonic-gate$Varname = "VAR" unless defined $Varname; 34*0Sstevel@tonic-gate$Useqq = 0 unless defined $Useqq; 35*0Sstevel@tonic-gate$Terse = 0 unless defined $Terse; 36*0Sstevel@tonic-gate$Freezer = "" unless defined $Freezer; 37*0Sstevel@tonic-gate$Toaster = "" unless defined $Toaster; 38*0Sstevel@tonic-gate$Deepcopy = 0 unless defined $Deepcopy; 39*0Sstevel@tonic-gate$Quotekeys = 1 unless defined $Quotekeys; 40*0Sstevel@tonic-gate$Bless = "bless" unless defined $Bless; 41*0Sstevel@tonic-gate#$Expdepth = 0 unless defined $Expdepth; 42*0Sstevel@tonic-gate$Maxdepth = 0 unless defined $Maxdepth; 43*0Sstevel@tonic-gate$Pair = ' => ' unless defined $Pair; 44*0Sstevel@tonic-gate$Useperl = 0 unless defined $Useperl; 45*0Sstevel@tonic-gate$Sortkeys = 0 unless defined $Sortkeys; 46*0Sstevel@tonic-gate$Deparse = 0 unless defined $Deparse; 47*0Sstevel@tonic-gate 48*0Sstevel@tonic-gate# 49*0Sstevel@tonic-gate# expects an arrayref of values to be dumped. 50*0Sstevel@tonic-gate# can optionally pass an arrayref of names for the values. 51*0Sstevel@tonic-gate# names must have leading $ sign stripped. begin the name with * 52*0Sstevel@tonic-gate# to cause output of arrays and hashes rather than refs. 53*0Sstevel@tonic-gate# 54*0Sstevel@tonic-gatesub new { 55*0Sstevel@tonic-gate my($c, $v, $n) = @_; 56*0Sstevel@tonic-gate 57*0Sstevel@tonic-gate croak "Usage: PACKAGE->new(ARRAYREF, [ARRAYREF])" 58*0Sstevel@tonic-gate unless (defined($v) && (ref($v) eq 'ARRAY')); 59*0Sstevel@tonic-gate $n = [] unless (defined($n) && (ref($v) eq 'ARRAY')); 60*0Sstevel@tonic-gate 61*0Sstevel@tonic-gate my($s) = { 62*0Sstevel@tonic-gate level => 0, # current recursive depth 63*0Sstevel@tonic-gate indent => $Indent, # various styles of indenting 64*0Sstevel@tonic-gate pad => $Pad, # all lines prefixed by this string 65*0Sstevel@tonic-gate xpad => "", # padding-per-level 66*0Sstevel@tonic-gate apad => "", # added padding for hash keys n such 67*0Sstevel@tonic-gate sep => "", # list separator 68*0Sstevel@tonic-gate pair => $Pair, # hash key/value separator: defaults to ' => ' 69*0Sstevel@tonic-gate seen => {}, # local (nested) refs (id => [name, val]) 70*0Sstevel@tonic-gate todump => $v, # values to dump [] 71*0Sstevel@tonic-gate names => $n, # optional names for values [] 72*0Sstevel@tonic-gate varname => $Varname, # prefix to use for tagging nameless ones 73*0Sstevel@tonic-gate purity => $Purity, # degree to which output is evalable 74*0Sstevel@tonic-gate useqq => $Useqq, # use "" for strings (backslashitis ensues) 75*0Sstevel@tonic-gate terse => $Terse, # avoid name output (where feasible) 76*0Sstevel@tonic-gate freezer => $Freezer, # name of Freezer method for objects 77*0Sstevel@tonic-gate toaster => $Toaster, # name of method to revive objects 78*0Sstevel@tonic-gate deepcopy => $Deepcopy, # dont cross-ref, except to stop recursion 79*0Sstevel@tonic-gate quotekeys => $Quotekeys, # quote hash keys 80*0Sstevel@tonic-gate 'bless' => $Bless, # keyword to use for "bless" 81*0Sstevel@tonic-gate# expdepth => $Expdepth, # cutoff depth for explicit dumping 82*0Sstevel@tonic-gate maxdepth => $Maxdepth, # depth beyond which we give up 83*0Sstevel@tonic-gate useperl => $Useperl, # use the pure Perl implementation 84*0Sstevel@tonic-gate sortkeys => $Sortkeys, # flag or filter for sorting hash keys 85*0Sstevel@tonic-gate deparse => $Deparse, # use B::Deparse for coderefs 86*0Sstevel@tonic-gate }; 87*0Sstevel@tonic-gate 88*0Sstevel@tonic-gate if ($Indent > 0) { 89*0Sstevel@tonic-gate $s->{xpad} = " "; 90*0Sstevel@tonic-gate $s->{sep} = "\n"; 91*0Sstevel@tonic-gate } 92*0Sstevel@tonic-gate return bless($s, $c); 93*0Sstevel@tonic-gate} 94*0Sstevel@tonic-gate 95*0Sstevel@tonic-gate# 96*0Sstevel@tonic-gate# add-to or query the table of already seen references 97*0Sstevel@tonic-gate# 98*0Sstevel@tonic-gatesub Seen { 99*0Sstevel@tonic-gate my($s, $g) = @_; 100*0Sstevel@tonic-gate if (defined($g) && (ref($g) eq 'HASH')) { 101*0Sstevel@tonic-gate my($k, $v, $id); 102*0Sstevel@tonic-gate while (($k, $v) = each %$g) { 103*0Sstevel@tonic-gate if (defined $v and ref $v) { 104*0Sstevel@tonic-gate ($id) = (overload::StrVal($v) =~ /\((.*)\)$/); 105*0Sstevel@tonic-gate if ($k =~ /^[*](.*)$/) { 106*0Sstevel@tonic-gate $k = (ref $v eq 'ARRAY') ? ( "\\\@" . $1 ) : 107*0Sstevel@tonic-gate (ref $v eq 'HASH') ? ( "\\\%" . $1 ) : 108*0Sstevel@tonic-gate (ref $v eq 'CODE') ? ( "\\\&" . $1 ) : 109*0Sstevel@tonic-gate ( "\$" . $1 ) ; 110*0Sstevel@tonic-gate } 111*0Sstevel@tonic-gate elsif ($k !~ /^\$/) { 112*0Sstevel@tonic-gate $k = "\$" . $k; 113*0Sstevel@tonic-gate } 114*0Sstevel@tonic-gate $s->{seen}{$id} = [$k, $v]; 115*0Sstevel@tonic-gate } 116*0Sstevel@tonic-gate else { 117*0Sstevel@tonic-gate carp "Only refs supported, ignoring non-ref item \$$k"; 118*0Sstevel@tonic-gate } 119*0Sstevel@tonic-gate } 120*0Sstevel@tonic-gate return $s; 121*0Sstevel@tonic-gate } 122*0Sstevel@tonic-gate else { 123*0Sstevel@tonic-gate return map { @$_ } values %{$s->{seen}}; 124*0Sstevel@tonic-gate } 125*0Sstevel@tonic-gate} 126*0Sstevel@tonic-gate 127*0Sstevel@tonic-gate# 128*0Sstevel@tonic-gate# set or query the values to be dumped 129*0Sstevel@tonic-gate# 130*0Sstevel@tonic-gatesub Values { 131*0Sstevel@tonic-gate my($s, $v) = @_; 132*0Sstevel@tonic-gate if (defined($v) && (ref($v) eq 'ARRAY')) { 133*0Sstevel@tonic-gate $s->{todump} = [@$v]; # make a copy 134*0Sstevel@tonic-gate return $s; 135*0Sstevel@tonic-gate } 136*0Sstevel@tonic-gate else { 137*0Sstevel@tonic-gate return @{$s->{todump}}; 138*0Sstevel@tonic-gate } 139*0Sstevel@tonic-gate} 140*0Sstevel@tonic-gate 141*0Sstevel@tonic-gate# 142*0Sstevel@tonic-gate# set or query the names of the values to be dumped 143*0Sstevel@tonic-gate# 144*0Sstevel@tonic-gatesub Names { 145*0Sstevel@tonic-gate my($s, $n) = @_; 146*0Sstevel@tonic-gate if (defined($n) && (ref($n) eq 'ARRAY')) { 147*0Sstevel@tonic-gate $s->{names} = [@$n]; # make a copy 148*0Sstevel@tonic-gate return $s; 149*0Sstevel@tonic-gate } 150*0Sstevel@tonic-gate else { 151*0Sstevel@tonic-gate return @{$s->{names}}; 152*0Sstevel@tonic-gate } 153*0Sstevel@tonic-gate} 154*0Sstevel@tonic-gate 155*0Sstevel@tonic-gatesub DESTROY {} 156*0Sstevel@tonic-gate 157*0Sstevel@tonic-gatesub Dump { 158*0Sstevel@tonic-gate return &Dumpxs 159*0Sstevel@tonic-gate unless $Data::Dumper::Useperl || (ref($_[0]) && $_[0]->{useperl}) || 160*0Sstevel@tonic-gate $Data::Dumper::Useqq || (ref($_[0]) && $_[0]->{useqq}) || 161*0Sstevel@tonic-gate $Data::Dumper::Deparse || (ref($_[0]) && $_[0]->{deparse}); 162*0Sstevel@tonic-gate return &Dumpperl; 163*0Sstevel@tonic-gate} 164*0Sstevel@tonic-gate 165*0Sstevel@tonic-gate# 166*0Sstevel@tonic-gate# dump the refs in the current dumper object. 167*0Sstevel@tonic-gate# expects same args as new() if called via package name. 168*0Sstevel@tonic-gate# 169*0Sstevel@tonic-gatesub Dumpperl { 170*0Sstevel@tonic-gate my($s) = shift; 171*0Sstevel@tonic-gate my(@out, $val, $name); 172*0Sstevel@tonic-gate my($i) = 0; 173*0Sstevel@tonic-gate local(@post); 174*0Sstevel@tonic-gate 175*0Sstevel@tonic-gate $s = $s->new(@_) unless ref $s; 176*0Sstevel@tonic-gate 177*0Sstevel@tonic-gate for $val (@{$s->{todump}}) { 178*0Sstevel@tonic-gate my $out = ""; 179*0Sstevel@tonic-gate @post = (); 180*0Sstevel@tonic-gate $name = $s->{names}[$i++]; 181*0Sstevel@tonic-gate if (defined $name) { 182*0Sstevel@tonic-gate if ($name =~ /^[*](.*)$/) { 183*0Sstevel@tonic-gate if (defined $val) { 184*0Sstevel@tonic-gate $name = (ref $val eq 'ARRAY') ? ( "\@" . $1 ) : 185*0Sstevel@tonic-gate (ref $val eq 'HASH') ? ( "\%" . $1 ) : 186*0Sstevel@tonic-gate (ref $val eq 'CODE') ? ( "\*" . $1 ) : 187*0Sstevel@tonic-gate ( "\$" . $1 ) ; 188*0Sstevel@tonic-gate } 189*0Sstevel@tonic-gate else { 190*0Sstevel@tonic-gate $name = "\$" . $1; 191*0Sstevel@tonic-gate } 192*0Sstevel@tonic-gate } 193*0Sstevel@tonic-gate elsif ($name !~ /^\$/) { 194*0Sstevel@tonic-gate $name = "\$" . $name; 195*0Sstevel@tonic-gate } 196*0Sstevel@tonic-gate } 197*0Sstevel@tonic-gate else { 198*0Sstevel@tonic-gate $name = "\$" . $s->{varname} . $i; 199*0Sstevel@tonic-gate } 200*0Sstevel@tonic-gate 201*0Sstevel@tonic-gate my $valstr; 202*0Sstevel@tonic-gate { 203*0Sstevel@tonic-gate local($s->{apad}) = $s->{apad}; 204*0Sstevel@tonic-gate $s->{apad} .= ' ' x (length($name) + 3) if $s->{indent} >= 2; 205*0Sstevel@tonic-gate $valstr = $s->_dump($val, $name); 206*0Sstevel@tonic-gate } 207*0Sstevel@tonic-gate 208*0Sstevel@tonic-gate $valstr = "$name = " . $valstr . ';' if @post or !$s->{terse}; 209*0Sstevel@tonic-gate $out .= $s->{pad} . $valstr . $s->{sep}; 210*0Sstevel@tonic-gate $out .= $s->{pad} . join(';' . $s->{sep} . $s->{pad}, @post) 211*0Sstevel@tonic-gate . ';' . $s->{sep} if @post; 212*0Sstevel@tonic-gate 213*0Sstevel@tonic-gate push @out, $out; 214*0Sstevel@tonic-gate } 215*0Sstevel@tonic-gate return wantarray ? @out : join('', @out); 216*0Sstevel@tonic-gate} 217*0Sstevel@tonic-gate 218*0Sstevel@tonic-gate# 219*0Sstevel@tonic-gate# twist, toil and turn; 220*0Sstevel@tonic-gate# and recurse, of course. 221*0Sstevel@tonic-gate# sometimes sordidly; 222*0Sstevel@tonic-gate# and curse if no recourse. 223*0Sstevel@tonic-gate# 224*0Sstevel@tonic-gatesub _dump { 225*0Sstevel@tonic-gate my($s, $val, $name) = @_; 226*0Sstevel@tonic-gate my($sname); 227*0Sstevel@tonic-gate my($out, $realpack, $realtype, $type, $ipad, $id, $blesspad); 228*0Sstevel@tonic-gate 229*0Sstevel@tonic-gate $type = ref $val; 230*0Sstevel@tonic-gate $out = ""; 231*0Sstevel@tonic-gate 232*0Sstevel@tonic-gate if ($type) { 233*0Sstevel@tonic-gate 234*0Sstevel@tonic-gate # prep it, if it looks like an object 235*0Sstevel@tonic-gate if (my $freezer = $s->{freezer}) { 236*0Sstevel@tonic-gate $val->$freezer() if UNIVERSAL::can($val, $freezer); 237*0Sstevel@tonic-gate } 238*0Sstevel@tonic-gate 239*0Sstevel@tonic-gate ($realpack, $realtype, $id) = 240*0Sstevel@tonic-gate (overload::StrVal($val) =~ /^(?:(.*)\=)?([^=]*)\(([^\(]*)\)$/); 241*0Sstevel@tonic-gate 242*0Sstevel@tonic-gate # if it has a name, we need to either look it up, or keep a tab 243*0Sstevel@tonic-gate # on it so we know when we hit it later 244*0Sstevel@tonic-gate if (defined($name) and length($name)) { 245*0Sstevel@tonic-gate # keep a tab on it so that we dont fall into recursive pit 246*0Sstevel@tonic-gate if (exists $s->{seen}{$id}) { 247*0Sstevel@tonic-gate# if ($s->{expdepth} < $s->{level}) { 248*0Sstevel@tonic-gate if ($s->{purity} and $s->{level} > 0) { 249*0Sstevel@tonic-gate $out = ($realtype eq 'HASH') ? '{}' : 250*0Sstevel@tonic-gate ($realtype eq 'ARRAY') ? '[]' : 251*0Sstevel@tonic-gate 'do{my $o}' ; 252*0Sstevel@tonic-gate push @post, $name . " = " . $s->{seen}{$id}[0]; 253*0Sstevel@tonic-gate } 254*0Sstevel@tonic-gate else { 255*0Sstevel@tonic-gate $out = $s->{seen}{$id}[0]; 256*0Sstevel@tonic-gate if ($name =~ /^([\@\%])/) { 257*0Sstevel@tonic-gate my $start = $1; 258*0Sstevel@tonic-gate if ($out =~ /^\\$start/) { 259*0Sstevel@tonic-gate $out = substr($out, 1); 260*0Sstevel@tonic-gate } 261*0Sstevel@tonic-gate else { 262*0Sstevel@tonic-gate $out = $start . '{' . $out . '}'; 263*0Sstevel@tonic-gate } 264*0Sstevel@tonic-gate } 265*0Sstevel@tonic-gate } 266*0Sstevel@tonic-gate return $out; 267*0Sstevel@tonic-gate# } 268*0Sstevel@tonic-gate } 269*0Sstevel@tonic-gate else { 270*0Sstevel@tonic-gate # store our name 271*0Sstevel@tonic-gate $s->{seen}{$id} = [ (($name =~ /^[@%]/) ? ('\\' . $name ) : 272*0Sstevel@tonic-gate ($realtype eq 'CODE' and 273*0Sstevel@tonic-gate $name =~ /^[*](.*)$/) ? ('\\&' . $1 ) : 274*0Sstevel@tonic-gate $name ), 275*0Sstevel@tonic-gate $val ]; 276*0Sstevel@tonic-gate } 277*0Sstevel@tonic-gate } 278*0Sstevel@tonic-gate 279*0Sstevel@tonic-gate if ($realpack and $realpack eq 'Regexp') { 280*0Sstevel@tonic-gate $out = "$val"; 281*0Sstevel@tonic-gate $out =~ s,/,\\/,g; 282*0Sstevel@tonic-gate return "qr/$out/"; 283*0Sstevel@tonic-gate } 284*0Sstevel@tonic-gate 285*0Sstevel@tonic-gate # If purity is not set and maxdepth is set, then check depth: 286*0Sstevel@tonic-gate # if we have reached maximum depth, return the string 287*0Sstevel@tonic-gate # representation of the thing we are currently examining 288*0Sstevel@tonic-gate # at this depth (i.e., 'Foo=ARRAY(0xdeadbeef)'). 289*0Sstevel@tonic-gate if (!$s->{purity} 290*0Sstevel@tonic-gate and $s->{maxdepth} > 0 291*0Sstevel@tonic-gate and $s->{level} >= $s->{maxdepth}) 292*0Sstevel@tonic-gate { 293*0Sstevel@tonic-gate return qq['$val']; 294*0Sstevel@tonic-gate } 295*0Sstevel@tonic-gate 296*0Sstevel@tonic-gate # we have a blessed ref 297*0Sstevel@tonic-gate if ($realpack) { 298*0Sstevel@tonic-gate $out = $s->{'bless'} . '( '; 299*0Sstevel@tonic-gate $blesspad = $s->{apad}; 300*0Sstevel@tonic-gate $s->{apad} .= ' ' if ($s->{indent} >= 2); 301*0Sstevel@tonic-gate } 302*0Sstevel@tonic-gate 303*0Sstevel@tonic-gate $s->{level}++; 304*0Sstevel@tonic-gate $ipad = $s->{xpad} x $s->{level}; 305*0Sstevel@tonic-gate 306*0Sstevel@tonic-gate if ($realtype eq 'SCALAR' || $realtype eq 'REF') { 307*0Sstevel@tonic-gate if ($realpack) { 308*0Sstevel@tonic-gate $out .= 'do{\\(my $o = ' . $s->_dump($$val, "\${$name}") . ')}'; 309*0Sstevel@tonic-gate } 310*0Sstevel@tonic-gate else { 311*0Sstevel@tonic-gate $out .= '\\' . $s->_dump($$val, "\${$name}"); 312*0Sstevel@tonic-gate } 313*0Sstevel@tonic-gate } 314*0Sstevel@tonic-gate elsif ($realtype eq 'GLOB') { 315*0Sstevel@tonic-gate $out .= '\\' . $s->_dump($$val, "*{$name}"); 316*0Sstevel@tonic-gate } 317*0Sstevel@tonic-gate elsif ($realtype eq 'ARRAY') { 318*0Sstevel@tonic-gate my($v, $pad, $mname); 319*0Sstevel@tonic-gate my($i) = 0; 320*0Sstevel@tonic-gate $out .= ($name =~ /^\@/) ? '(' : '['; 321*0Sstevel@tonic-gate $pad = $s->{sep} . $s->{pad} . $s->{apad}; 322*0Sstevel@tonic-gate ($name =~ /^\@(.*)$/) ? ($mname = "\$" . $1) : 323*0Sstevel@tonic-gate # omit -> if $foo->[0]->{bar}, but not ${$foo->[0]}->{bar} 324*0Sstevel@tonic-gate ($name =~ /^\\?[\%\@\*\$][^{].*[]}]$/) ? ($mname = $name) : 325*0Sstevel@tonic-gate ($mname = $name . '->'); 326*0Sstevel@tonic-gate $mname .= '->' if $mname =~ /^\*.+\{[A-Z]+\}$/; 327*0Sstevel@tonic-gate for $v (@$val) { 328*0Sstevel@tonic-gate $sname = $mname . '[' . $i . ']'; 329*0Sstevel@tonic-gate $out .= $pad . $ipad . '#' . $i if $s->{indent} >= 3; 330*0Sstevel@tonic-gate $out .= $pad . $ipad . $s->_dump($v, $sname); 331*0Sstevel@tonic-gate $out .= "," if $i++ < $#$val; 332*0Sstevel@tonic-gate } 333*0Sstevel@tonic-gate $out .= $pad . ($s->{xpad} x ($s->{level} - 1)) if $i; 334*0Sstevel@tonic-gate $out .= ($name =~ /^\@/) ? ')' : ']'; 335*0Sstevel@tonic-gate } 336*0Sstevel@tonic-gate elsif ($realtype eq 'HASH') { 337*0Sstevel@tonic-gate my($k, $v, $pad, $lpad, $mname, $pair); 338*0Sstevel@tonic-gate $out .= ($name =~ /^\%/) ? '(' : '{'; 339*0Sstevel@tonic-gate $pad = $s->{sep} . $s->{pad} . $s->{apad}; 340*0Sstevel@tonic-gate $lpad = $s->{apad}; 341*0Sstevel@tonic-gate $pair = $s->{pair}; 342*0Sstevel@tonic-gate ($name =~ /^\%(.*)$/) ? ($mname = "\$" . $1) : 343*0Sstevel@tonic-gate # omit -> if $foo->[0]->{bar}, but not ${$foo->[0]}->{bar} 344*0Sstevel@tonic-gate ($name =~ /^\\?[\%\@\*\$][^{].*[]}]$/) ? ($mname = $name) : 345*0Sstevel@tonic-gate ($mname = $name . '->'); 346*0Sstevel@tonic-gate $mname .= '->' if $mname =~ /^\*.+\{[A-Z]+\}$/; 347*0Sstevel@tonic-gate my ($sortkeys, $keys, $key) = ("$s->{sortkeys}"); 348*0Sstevel@tonic-gate if ($sortkeys) { 349*0Sstevel@tonic-gate if (ref($s->{sortkeys}) eq 'CODE') { 350*0Sstevel@tonic-gate $keys = $s->{sortkeys}($val); 351*0Sstevel@tonic-gate unless (ref($keys) eq 'ARRAY') { 352*0Sstevel@tonic-gate carp "Sortkeys subroutine did not return ARRAYREF"; 353*0Sstevel@tonic-gate $keys = []; 354*0Sstevel@tonic-gate } 355*0Sstevel@tonic-gate } 356*0Sstevel@tonic-gate else { 357*0Sstevel@tonic-gate $keys = [ sort keys %$val ]; 358*0Sstevel@tonic-gate } 359*0Sstevel@tonic-gate } 360*0Sstevel@tonic-gate while (($k, $v) = ! $sortkeys ? (each %$val) : 361*0Sstevel@tonic-gate @$keys ? ($key = shift(@$keys), $val->{$key}) : 362*0Sstevel@tonic-gate () ) 363*0Sstevel@tonic-gate { 364*0Sstevel@tonic-gate my $nk = $s->_dump($k, ""); 365*0Sstevel@tonic-gate $nk = $1 if !$s->{quotekeys} and $nk =~ /^[\"\']([A-Za-z_]\w*)[\"\']$/; 366*0Sstevel@tonic-gate $sname = $mname . '{' . $nk . '}'; 367*0Sstevel@tonic-gate $out .= $pad . $ipad . $nk . $pair; 368*0Sstevel@tonic-gate 369*0Sstevel@tonic-gate # temporarily alter apad 370*0Sstevel@tonic-gate $s->{apad} .= (" " x (length($nk) + 4)) if $s->{indent} >= 2; 371*0Sstevel@tonic-gate $out .= $s->_dump($val->{$k}, $sname) . ","; 372*0Sstevel@tonic-gate $s->{apad} = $lpad if $s->{indent} >= 2; 373*0Sstevel@tonic-gate } 374*0Sstevel@tonic-gate if (substr($out, -1) eq ',') { 375*0Sstevel@tonic-gate chop $out; 376*0Sstevel@tonic-gate $out .= $pad . ($s->{xpad} x ($s->{level} - 1)); 377*0Sstevel@tonic-gate } 378*0Sstevel@tonic-gate $out .= ($name =~ /^\%/) ? ')' : '}'; 379*0Sstevel@tonic-gate } 380*0Sstevel@tonic-gate elsif ($realtype eq 'CODE') { 381*0Sstevel@tonic-gate if ($s->{deparse}) { 382*0Sstevel@tonic-gate require B::Deparse; 383*0Sstevel@tonic-gate my $sub = 'sub ' . (B::Deparse->new)->coderef2text($val); 384*0Sstevel@tonic-gate $pad = $s->{sep} . $s->{pad} . $s->{xpad} . $s->{apad} . ' '; 385*0Sstevel@tonic-gate $sub =~ s/\n/$pad/gse; 386*0Sstevel@tonic-gate $out .= $sub; 387*0Sstevel@tonic-gate } else { 388*0Sstevel@tonic-gate $out .= 'sub { "DUMMY" }'; 389*0Sstevel@tonic-gate carp "Encountered CODE ref, using dummy placeholder" if $s->{purity}; 390*0Sstevel@tonic-gate } 391*0Sstevel@tonic-gate } 392*0Sstevel@tonic-gate else { 393*0Sstevel@tonic-gate croak "Can\'t handle $realtype type."; 394*0Sstevel@tonic-gate } 395*0Sstevel@tonic-gate 396*0Sstevel@tonic-gate if ($realpack) { # we have a blessed ref 397*0Sstevel@tonic-gate $out .= ', \'' . $realpack . '\'' . ' )'; 398*0Sstevel@tonic-gate $out .= '->' . $s->{toaster} . '()' if $s->{toaster} ne ''; 399*0Sstevel@tonic-gate $s->{apad} = $blesspad; 400*0Sstevel@tonic-gate } 401*0Sstevel@tonic-gate $s->{level}--; 402*0Sstevel@tonic-gate 403*0Sstevel@tonic-gate } 404*0Sstevel@tonic-gate else { # simple scalar 405*0Sstevel@tonic-gate 406*0Sstevel@tonic-gate my $ref = \$_[1]; 407*0Sstevel@tonic-gate # first, catalog the scalar 408*0Sstevel@tonic-gate if ($name ne '') { 409*0Sstevel@tonic-gate ($id) = ("$ref" =~ /\(([^\(]*)\)$/); 410*0Sstevel@tonic-gate if (exists $s->{seen}{$id}) { 411*0Sstevel@tonic-gate if ($s->{seen}{$id}[2]) { 412*0Sstevel@tonic-gate $out = $s->{seen}{$id}[0]; 413*0Sstevel@tonic-gate #warn "[<$out]\n"; 414*0Sstevel@tonic-gate return "\${$out}"; 415*0Sstevel@tonic-gate } 416*0Sstevel@tonic-gate } 417*0Sstevel@tonic-gate else { 418*0Sstevel@tonic-gate #warn "[>\\$name]\n"; 419*0Sstevel@tonic-gate $s->{seen}{$id} = ["\\$name", $ref]; 420*0Sstevel@tonic-gate } 421*0Sstevel@tonic-gate } 422*0Sstevel@tonic-gate if (ref($ref) eq 'GLOB' or "$ref" =~ /=GLOB\([^()]+\)$/) { # glob 423*0Sstevel@tonic-gate my $name = substr($val, 1); 424*0Sstevel@tonic-gate if ($name =~ /^[A-Za-z_][\w:]*$/) { 425*0Sstevel@tonic-gate $name =~ s/^main::/::/; 426*0Sstevel@tonic-gate $sname = $name; 427*0Sstevel@tonic-gate } 428*0Sstevel@tonic-gate else { 429*0Sstevel@tonic-gate $sname = $s->_dump($name, ""); 430*0Sstevel@tonic-gate $sname = '{' . $sname . '}'; 431*0Sstevel@tonic-gate } 432*0Sstevel@tonic-gate if ($s->{purity}) { 433*0Sstevel@tonic-gate my $k; 434*0Sstevel@tonic-gate local ($s->{level}) = 0; 435*0Sstevel@tonic-gate for $k (qw(SCALAR ARRAY HASH)) { 436*0Sstevel@tonic-gate my $gval = *$val{$k}; 437*0Sstevel@tonic-gate next unless defined $gval; 438*0Sstevel@tonic-gate next if $k eq "SCALAR" && ! defined $$gval; # always there 439*0Sstevel@tonic-gate 440*0Sstevel@tonic-gate # _dump can push into @post, so we hold our place using $postlen 441*0Sstevel@tonic-gate my $postlen = scalar @post; 442*0Sstevel@tonic-gate $post[$postlen] = "\*$sname = "; 443*0Sstevel@tonic-gate local ($s->{apad}) = " " x length($post[$postlen]) if $s->{indent} >= 2; 444*0Sstevel@tonic-gate $post[$postlen] .= $s->_dump($gval, "\*$sname\{$k\}"); 445*0Sstevel@tonic-gate } 446*0Sstevel@tonic-gate } 447*0Sstevel@tonic-gate $out .= '*' . $sname; 448*0Sstevel@tonic-gate } 449*0Sstevel@tonic-gate elsif (!defined($val)) { 450*0Sstevel@tonic-gate $out .= "undef"; 451*0Sstevel@tonic-gate } 452*0Sstevel@tonic-gate elsif ($val =~ /^(?:0|-?[1-9]\d{0,8})\z/) { # safe decimal number 453*0Sstevel@tonic-gate $out .= $val; 454*0Sstevel@tonic-gate } 455*0Sstevel@tonic-gate else { # string 456*0Sstevel@tonic-gate if ($s->{useqq} or $val =~ tr/\0-\377//c) { 457*0Sstevel@tonic-gate # Fall back to qq if there's unicode 458*0Sstevel@tonic-gate $out .= qquote($val, $s->{useqq}); 459*0Sstevel@tonic-gate } 460*0Sstevel@tonic-gate else { 461*0Sstevel@tonic-gate $val =~ s/([\\\'])/\\$1/g; 462*0Sstevel@tonic-gate $out .= '\'' . $val . '\''; 463*0Sstevel@tonic-gate } 464*0Sstevel@tonic-gate } 465*0Sstevel@tonic-gate } 466*0Sstevel@tonic-gate if ($id) { 467*0Sstevel@tonic-gate # if we made it this far, $id was added to seen list at current 468*0Sstevel@tonic-gate # level, so remove it to get deep copies 469*0Sstevel@tonic-gate if ($s->{deepcopy}) { 470*0Sstevel@tonic-gate delete($s->{seen}{$id}); 471*0Sstevel@tonic-gate } 472*0Sstevel@tonic-gate elsif ($name) { 473*0Sstevel@tonic-gate $s->{seen}{$id}[2] = 1; 474*0Sstevel@tonic-gate } 475*0Sstevel@tonic-gate } 476*0Sstevel@tonic-gate return $out; 477*0Sstevel@tonic-gate} 478*0Sstevel@tonic-gate 479*0Sstevel@tonic-gate# 480*0Sstevel@tonic-gate# non-OO style of earlier version 481*0Sstevel@tonic-gate# 482*0Sstevel@tonic-gatesub Dumper { 483*0Sstevel@tonic-gate return Data::Dumper->Dump([@_]); 484*0Sstevel@tonic-gate} 485*0Sstevel@tonic-gate 486*0Sstevel@tonic-gate# compat stub 487*0Sstevel@tonic-gatesub DumperX { 488*0Sstevel@tonic-gate return Data::Dumper->Dumpxs([@_], []); 489*0Sstevel@tonic-gate} 490*0Sstevel@tonic-gate 491*0Sstevel@tonic-gatesub Dumpf { return Data::Dumper->Dump(@_) } 492*0Sstevel@tonic-gate 493*0Sstevel@tonic-gatesub Dumpp { print Data::Dumper->Dump(@_) } 494*0Sstevel@tonic-gate 495*0Sstevel@tonic-gate# 496*0Sstevel@tonic-gate# reset the "seen" cache 497*0Sstevel@tonic-gate# 498*0Sstevel@tonic-gatesub Reset { 499*0Sstevel@tonic-gate my($s) = shift; 500*0Sstevel@tonic-gate $s->{seen} = {}; 501*0Sstevel@tonic-gate return $s; 502*0Sstevel@tonic-gate} 503*0Sstevel@tonic-gate 504*0Sstevel@tonic-gatesub Indent { 505*0Sstevel@tonic-gate my($s, $v) = @_; 506*0Sstevel@tonic-gate if (defined($v)) { 507*0Sstevel@tonic-gate if ($v == 0) { 508*0Sstevel@tonic-gate $s->{xpad} = ""; 509*0Sstevel@tonic-gate $s->{sep} = ""; 510*0Sstevel@tonic-gate } 511*0Sstevel@tonic-gate else { 512*0Sstevel@tonic-gate $s->{xpad} = " "; 513*0Sstevel@tonic-gate $s->{sep} = "\n"; 514*0Sstevel@tonic-gate } 515*0Sstevel@tonic-gate $s->{indent} = $v; 516*0Sstevel@tonic-gate return $s; 517*0Sstevel@tonic-gate } 518*0Sstevel@tonic-gate else { 519*0Sstevel@tonic-gate return $s->{indent}; 520*0Sstevel@tonic-gate } 521*0Sstevel@tonic-gate} 522*0Sstevel@tonic-gate 523*0Sstevel@tonic-gatesub Pair { 524*0Sstevel@tonic-gate my($s, $v) = @_; 525*0Sstevel@tonic-gate defined($v) ? (($s->{pair} = $v), return $s) : $s->{pair}; 526*0Sstevel@tonic-gate} 527*0Sstevel@tonic-gate 528*0Sstevel@tonic-gatesub Pad { 529*0Sstevel@tonic-gate my($s, $v) = @_; 530*0Sstevel@tonic-gate defined($v) ? (($s->{pad} = $v), return $s) : $s->{pad}; 531*0Sstevel@tonic-gate} 532*0Sstevel@tonic-gate 533*0Sstevel@tonic-gatesub Varname { 534*0Sstevel@tonic-gate my($s, $v) = @_; 535*0Sstevel@tonic-gate defined($v) ? (($s->{varname} = $v), return $s) : $s->{varname}; 536*0Sstevel@tonic-gate} 537*0Sstevel@tonic-gate 538*0Sstevel@tonic-gatesub Purity { 539*0Sstevel@tonic-gate my($s, $v) = @_; 540*0Sstevel@tonic-gate defined($v) ? (($s->{purity} = $v), return $s) : $s->{purity}; 541*0Sstevel@tonic-gate} 542*0Sstevel@tonic-gate 543*0Sstevel@tonic-gatesub Useqq { 544*0Sstevel@tonic-gate my($s, $v) = @_; 545*0Sstevel@tonic-gate defined($v) ? (($s->{useqq} = $v), return $s) : $s->{useqq}; 546*0Sstevel@tonic-gate} 547*0Sstevel@tonic-gate 548*0Sstevel@tonic-gatesub Terse { 549*0Sstevel@tonic-gate my($s, $v) = @_; 550*0Sstevel@tonic-gate defined($v) ? (($s->{terse} = $v), return $s) : $s->{terse}; 551*0Sstevel@tonic-gate} 552*0Sstevel@tonic-gate 553*0Sstevel@tonic-gatesub Freezer { 554*0Sstevel@tonic-gate my($s, $v) = @_; 555*0Sstevel@tonic-gate defined($v) ? (($s->{freezer} = $v), return $s) : $s->{freezer}; 556*0Sstevel@tonic-gate} 557*0Sstevel@tonic-gate 558*0Sstevel@tonic-gatesub Toaster { 559*0Sstevel@tonic-gate my($s, $v) = @_; 560*0Sstevel@tonic-gate defined($v) ? (($s->{toaster} = $v), return $s) : $s->{toaster}; 561*0Sstevel@tonic-gate} 562*0Sstevel@tonic-gate 563*0Sstevel@tonic-gatesub Deepcopy { 564*0Sstevel@tonic-gate my($s, $v) = @_; 565*0Sstevel@tonic-gate defined($v) ? (($s->{deepcopy} = $v), return $s) : $s->{deepcopy}; 566*0Sstevel@tonic-gate} 567*0Sstevel@tonic-gate 568*0Sstevel@tonic-gatesub Quotekeys { 569*0Sstevel@tonic-gate my($s, $v) = @_; 570*0Sstevel@tonic-gate defined($v) ? (($s->{quotekeys} = $v), return $s) : $s->{quotekeys}; 571*0Sstevel@tonic-gate} 572*0Sstevel@tonic-gate 573*0Sstevel@tonic-gatesub Bless { 574*0Sstevel@tonic-gate my($s, $v) = @_; 575*0Sstevel@tonic-gate defined($v) ? (($s->{'bless'} = $v), return $s) : $s->{'bless'}; 576*0Sstevel@tonic-gate} 577*0Sstevel@tonic-gate 578*0Sstevel@tonic-gatesub Maxdepth { 579*0Sstevel@tonic-gate my($s, $v) = @_; 580*0Sstevel@tonic-gate defined($v) ? (($s->{'maxdepth'} = $v), return $s) : $s->{'maxdepth'}; 581*0Sstevel@tonic-gate} 582*0Sstevel@tonic-gate 583*0Sstevel@tonic-gatesub Useperl { 584*0Sstevel@tonic-gate my($s, $v) = @_; 585*0Sstevel@tonic-gate defined($v) ? (($s->{'useperl'} = $v), return $s) : $s->{'useperl'}; 586*0Sstevel@tonic-gate} 587*0Sstevel@tonic-gate 588*0Sstevel@tonic-gatesub Sortkeys { 589*0Sstevel@tonic-gate my($s, $v) = @_; 590*0Sstevel@tonic-gate defined($v) ? (($s->{'sortkeys'} = $v), return $s) : $s->{'sortkeys'}; 591*0Sstevel@tonic-gate} 592*0Sstevel@tonic-gate 593*0Sstevel@tonic-gatesub Deparse { 594*0Sstevel@tonic-gate my($s, $v) = @_; 595*0Sstevel@tonic-gate defined($v) ? (($s->{'deparse'} = $v), return $s) : $s->{'deparse'}; 596*0Sstevel@tonic-gate} 597*0Sstevel@tonic-gate 598*0Sstevel@tonic-gate# used by qquote below 599*0Sstevel@tonic-gatemy %esc = ( 600*0Sstevel@tonic-gate "\a" => "\\a", 601*0Sstevel@tonic-gate "\b" => "\\b", 602*0Sstevel@tonic-gate "\t" => "\\t", 603*0Sstevel@tonic-gate "\n" => "\\n", 604*0Sstevel@tonic-gate "\f" => "\\f", 605*0Sstevel@tonic-gate "\r" => "\\r", 606*0Sstevel@tonic-gate "\e" => "\\e", 607*0Sstevel@tonic-gate); 608*0Sstevel@tonic-gate 609*0Sstevel@tonic-gate# put a string value in double quotes 610*0Sstevel@tonic-gatesub qquote { 611*0Sstevel@tonic-gate local($_) = shift; 612*0Sstevel@tonic-gate s/([\\\"\@\$])/\\$1/g; 613*0Sstevel@tonic-gate my $bytes; { use bytes; $bytes = length } 614*0Sstevel@tonic-gate s/([^\x00-\x7f])/'\x{'.sprintf("%x",ord($1)).'}'/ge if $bytes > length; 615*0Sstevel@tonic-gate return qq("$_") unless 616*0Sstevel@tonic-gate /[^ !"\#\$%&'()*+,\-.\/0-9:;<=>?\@A-Z[\\\]^_`a-z{|}~]/; # fast exit 617*0Sstevel@tonic-gate 618*0Sstevel@tonic-gate my $high = shift || ""; 619*0Sstevel@tonic-gate s/([\a\b\t\n\f\r\e])/$esc{$1}/g; 620*0Sstevel@tonic-gate 621*0Sstevel@tonic-gate if (ord('^')==94) { # ascii 622*0Sstevel@tonic-gate # no need for 3 digits in escape for these 623*0Sstevel@tonic-gate s/([\0-\037])(?!\d)/'\\'.sprintf('%o',ord($1))/eg; 624*0Sstevel@tonic-gate s/([\0-\037\177])/'\\'.sprintf('%03o',ord($1))/eg; 625*0Sstevel@tonic-gate # all but last branch below not supported --BEHAVIOR SUBJECT TO CHANGE-- 626*0Sstevel@tonic-gate if ($high eq "iso8859") { 627*0Sstevel@tonic-gate s/([\200-\240])/'\\'.sprintf('%o',ord($1))/eg; 628*0Sstevel@tonic-gate } elsif ($high eq "utf8") { 629*0Sstevel@tonic-gate# use utf8; 630*0Sstevel@tonic-gate# $str =~ s/([^\040-\176])/sprintf "\\x{%04x}", ord($1)/ge; 631*0Sstevel@tonic-gate } elsif ($high eq "8bit") { 632*0Sstevel@tonic-gate # leave it as it is 633*0Sstevel@tonic-gate } else { 634*0Sstevel@tonic-gate s/([\200-\377])/'\\'.sprintf('%03o',ord($1))/eg; 635*0Sstevel@tonic-gate s/([^\040-\176])/sprintf "\\x{%04x}", ord($1)/ge; 636*0Sstevel@tonic-gate } 637*0Sstevel@tonic-gate } 638*0Sstevel@tonic-gate else { # ebcdic 639*0Sstevel@tonic-gate s{([^ !"\#\$%&'()*+,\-.\/0-9:;<=>?\@A-Z[\\\]^_`a-z{|}~])(?!\d)} 640*0Sstevel@tonic-gate {my $v = ord($1); '\\'.sprintf(($v <= 037 ? '%o' : '%03o'), $v)}eg; 641*0Sstevel@tonic-gate s{([^ !"\#\$%&'()*+,\-.\/0-9:;<=>?\@A-Z[\\\]^_`a-z{|}~])} 642*0Sstevel@tonic-gate {'\\'.sprintf('%03o',ord($1))}eg; 643*0Sstevel@tonic-gate } 644*0Sstevel@tonic-gate 645*0Sstevel@tonic-gate return qq("$_"); 646*0Sstevel@tonic-gate} 647*0Sstevel@tonic-gate 648*0Sstevel@tonic-gate# helper sub to sort hash keys in Perl < 5.8.0 where we don't have 649*0Sstevel@tonic-gate# access to sortsv() from XS 650*0Sstevel@tonic-gatesub _sortkeys { [ sort keys %{$_[0]} ] } 651*0Sstevel@tonic-gate 652*0Sstevel@tonic-gate1; 653*0Sstevel@tonic-gate__END__ 654*0Sstevel@tonic-gate 655*0Sstevel@tonic-gate=head1 NAME 656*0Sstevel@tonic-gate 657*0Sstevel@tonic-gateData::Dumper - stringified perl data structures, suitable for both printing and C<eval> 658*0Sstevel@tonic-gate 659*0Sstevel@tonic-gate=head1 SYNOPSIS 660*0Sstevel@tonic-gate 661*0Sstevel@tonic-gate use Data::Dumper; 662*0Sstevel@tonic-gate 663*0Sstevel@tonic-gate # simple procedural interface 664*0Sstevel@tonic-gate print Dumper($foo, $bar); 665*0Sstevel@tonic-gate 666*0Sstevel@tonic-gate # extended usage with names 667*0Sstevel@tonic-gate print Data::Dumper->Dump([$foo, $bar], [qw(foo *ary)]); 668*0Sstevel@tonic-gate 669*0Sstevel@tonic-gate # configuration variables 670*0Sstevel@tonic-gate { 671*0Sstevel@tonic-gate local $Data::Dumper::Purity = 1; 672*0Sstevel@tonic-gate eval Data::Dumper->Dump([$foo, $bar], [qw(foo *ary)]); 673*0Sstevel@tonic-gate } 674*0Sstevel@tonic-gate 675*0Sstevel@tonic-gate # OO usage 676*0Sstevel@tonic-gate $d = Data::Dumper->new([$foo, $bar], [qw(foo *ary)]); 677*0Sstevel@tonic-gate ... 678*0Sstevel@tonic-gate print $d->Dump; 679*0Sstevel@tonic-gate ... 680*0Sstevel@tonic-gate $d->Purity(1)->Terse(1)->Deepcopy(1); 681*0Sstevel@tonic-gate eval $d->Dump; 682*0Sstevel@tonic-gate 683*0Sstevel@tonic-gate 684*0Sstevel@tonic-gate=head1 DESCRIPTION 685*0Sstevel@tonic-gate 686*0Sstevel@tonic-gateGiven a list of scalars or reference variables, writes out their contents in 687*0Sstevel@tonic-gateperl syntax. The references can also be objects. The contents of each 688*0Sstevel@tonic-gatevariable is output in a single Perl statement. Handles self-referential 689*0Sstevel@tonic-gatestructures correctly. 690*0Sstevel@tonic-gate 691*0Sstevel@tonic-gateThe return value can be C<eval>ed to get back an identical copy of the 692*0Sstevel@tonic-gateoriginal reference structure. 693*0Sstevel@tonic-gate 694*0Sstevel@tonic-gateAny references that are the same as one of those passed in will be named 695*0Sstevel@tonic-gateC<$VAR>I<n> (where I<n> is a numeric suffix), and other duplicate references 696*0Sstevel@tonic-gateto substructures within C<$VAR>I<n> will be appropriately labeled using arrow 697*0Sstevel@tonic-gatenotation. You can specify names for individual values to be dumped if you 698*0Sstevel@tonic-gateuse the C<Dump()> method, or you can change the default C<$VAR> prefix to 699*0Sstevel@tonic-gatesomething else. See C<$Data::Dumper::Varname> and C<$Data::Dumper::Terse> 700*0Sstevel@tonic-gatebelow. 701*0Sstevel@tonic-gate 702*0Sstevel@tonic-gateThe default output of self-referential structures can be C<eval>ed, but the 703*0Sstevel@tonic-gatenested references to C<$VAR>I<n> will be undefined, since a recursive 704*0Sstevel@tonic-gatestructure cannot be constructed using one Perl statement. You should set the 705*0Sstevel@tonic-gateC<Purity> flag to 1 to get additional statements that will correctly fill in 706*0Sstevel@tonic-gatethese references. 707*0Sstevel@tonic-gate 708*0Sstevel@tonic-gateIn the extended usage form, the references to be dumped can be given 709*0Sstevel@tonic-gateuser-specified names. If a name begins with a C<*>, the output will 710*0Sstevel@tonic-gatedescribe the dereferenced type of the supplied reference for hashes and 711*0Sstevel@tonic-gatearrays, and coderefs. Output of names will be avoided where possible if 712*0Sstevel@tonic-gatethe C<Terse> flag is set. 713*0Sstevel@tonic-gate 714*0Sstevel@tonic-gateIn many cases, methods that are used to set the internal state of the 715*0Sstevel@tonic-gateobject will return the object itself, so method calls can be conveniently 716*0Sstevel@tonic-gatechained together. 717*0Sstevel@tonic-gate 718*0Sstevel@tonic-gateSeveral styles of output are possible, all controlled by setting 719*0Sstevel@tonic-gatethe C<Indent> flag. See L<Configuration Variables or Methods> below 720*0Sstevel@tonic-gatefor details. 721*0Sstevel@tonic-gate 722*0Sstevel@tonic-gate 723*0Sstevel@tonic-gate=head2 Methods 724*0Sstevel@tonic-gate 725*0Sstevel@tonic-gate=over 4 726*0Sstevel@tonic-gate 727*0Sstevel@tonic-gate=item I<PACKAGE>->new(I<ARRAYREF [>, I<ARRAYREF]>) 728*0Sstevel@tonic-gate 729*0Sstevel@tonic-gateReturns a newly created C<Data::Dumper> object. The first argument is an 730*0Sstevel@tonic-gateanonymous array of values to be dumped. The optional second argument is an 731*0Sstevel@tonic-gateanonymous array of names for the values. The names need not have a leading 732*0Sstevel@tonic-gateC<$> sign, and must be comprised of alphanumeric characters. You can begin 733*0Sstevel@tonic-gatea name with a C<*> to specify that the dereferenced type must be dumped 734*0Sstevel@tonic-gateinstead of the reference itself, for ARRAY and HASH references. 735*0Sstevel@tonic-gate 736*0Sstevel@tonic-gateThe prefix specified by C<$Data::Dumper::Varname> will be used with a 737*0Sstevel@tonic-gatenumeric suffix if the name for a value is undefined. 738*0Sstevel@tonic-gate 739*0Sstevel@tonic-gateData::Dumper will catalog all references encountered while dumping the 740*0Sstevel@tonic-gatevalues. Cross-references (in the form of names of substructures in perl 741*0Sstevel@tonic-gatesyntax) will be inserted at all possible points, preserving any structural 742*0Sstevel@tonic-gateinterdependencies in the original set of values. Structure traversal is 743*0Sstevel@tonic-gatedepth-first, and proceeds in order from the first supplied value to 744*0Sstevel@tonic-gatethe last. 745*0Sstevel@tonic-gate 746*0Sstevel@tonic-gate=item I<$OBJ>->Dump I<or> I<PACKAGE>->Dump(I<ARRAYREF [>, I<ARRAYREF]>) 747*0Sstevel@tonic-gate 748*0Sstevel@tonic-gateReturns the stringified form of the values stored in the object (preserving 749*0Sstevel@tonic-gatethe order in which they were supplied to C<new>), subject to the 750*0Sstevel@tonic-gateconfiguration options below. In a list context, it returns a list 751*0Sstevel@tonic-gateof strings corresponding to the supplied values. 752*0Sstevel@tonic-gate 753*0Sstevel@tonic-gateThe second form, for convenience, simply calls the C<new> method on its 754*0Sstevel@tonic-gatearguments before dumping the object immediately. 755*0Sstevel@tonic-gate 756*0Sstevel@tonic-gate=item I<$OBJ>->Seen(I<[HASHREF]>) 757*0Sstevel@tonic-gate 758*0Sstevel@tonic-gateQueries or adds to the internal table of already encountered references. 759*0Sstevel@tonic-gateYou must use C<Reset> to explicitly clear the table if needed. Such 760*0Sstevel@tonic-gatereferences are not dumped; instead, their names are inserted wherever they 761*0Sstevel@tonic-gateare encountered subsequently. This is useful especially for properly 762*0Sstevel@tonic-gatedumping subroutine references. 763*0Sstevel@tonic-gate 764*0Sstevel@tonic-gateExpects an anonymous hash of name => value pairs. Same rules apply for names 765*0Sstevel@tonic-gateas in C<new>. If no argument is supplied, will return the "seen" list of 766*0Sstevel@tonic-gatename => value pairs, in a list context. Otherwise, returns the object 767*0Sstevel@tonic-gateitself. 768*0Sstevel@tonic-gate 769*0Sstevel@tonic-gate=item I<$OBJ>->Values(I<[ARRAYREF]>) 770*0Sstevel@tonic-gate 771*0Sstevel@tonic-gateQueries or replaces the internal array of values that will be dumped. 772*0Sstevel@tonic-gateWhen called without arguments, returns the values. Otherwise, returns the 773*0Sstevel@tonic-gateobject itself. 774*0Sstevel@tonic-gate 775*0Sstevel@tonic-gate=item I<$OBJ>->Names(I<[ARRAYREF]>) 776*0Sstevel@tonic-gate 777*0Sstevel@tonic-gateQueries or replaces the internal array of user supplied names for the values 778*0Sstevel@tonic-gatethat will be dumped. When called without arguments, returns the names. 779*0Sstevel@tonic-gateOtherwise, returns the object itself. 780*0Sstevel@tonic-gate 781*0Sstevel@tonic-gate=item I<$OBJ>->Reset 782*0Sstevel@tonic-gate 783*0Sstevel@tonic-gateClears the internal table of "seen" references and returns the object 784*0Sstevel@tonic-gateitself. 785*0Sstevel@tonic-gate 786*0Sstevel@tonic-gate=back 787*0Sstevel@tonic-gate 788*0Sstevel@tonic-gate=head2 Functions 789*0Sstevel@tonic-gate 790*0Sstevel@tonic-gate=over 4 791*0Sstevel@tonic-gate 792*0Sstevel@tonic-gate=item Dumper(I<LIST>) 793*0Sstevel@tonic-gate 794*0Sstevel@tonic-gateReturns the stringified form of the values in the list, subject to the 795*0Sstevel@tonic-gateconfiguration options below. The values will be named C<$VAR>I<n> in the 796*0Sstevel@tonic-gateoutput, where I<n> is a numeric suffix. Will return a list of strings 797*0Sstevel@tonic-gatein a list context. 798*0Sstevel@tonic-gate 799*0Sstevel@tonic-gate=back 800*0Sstevel@tonic-gate 801*0Sstevel@tonic-gate=head2 Configuration Variables or Methods 802*0Sstevel@tonic-gate 803*0Sstevel@tonic-gateSeveral configuration variables can be used to control the kind of output 804*0Sstevel@tonic-gategenerated when using the procedural interface. These variables are usually 805*0Sstevel@tonic-gateC<local>ized in a block so that other parts of the code are not affected by 806*0Sstevel@tonic-gatethe change. 807*0Sstevel@tonic-gate 808*0Sstevel@tonic-gateThese variables determine the default state of the object created by calling 809*0Sstevel@tonic-gatethe C<new> method, but cannot be used to alter the state of the object 810*0Sstevel@tonic-gatethereafter. The equivalent method names should be used instead to query 811*0Sstevel@tonic-gateor set the internal state of the object. 812*0Sstevel@tonic-gate 813*0Sstevel@tonic-gateThe method forms return the object itself when called with arguments, 814*0Sstevel@tonic-gateso that they can be chained together nicely. 815*0Sstevel@tonic-gate 816*0Sstevel@tonic-gate=over 4 817*0Sstevel@tonic-gate 818*0Sstevel@tonic-gate=item * 819*0Sstevel@tonic-gate 820*0Sstevel@tonic-gate$Data::Dumper::Indent I<or> I<$OBJ>->Indent(I<[NEWVAL]>) 821*0Sstevel@tonic-gate 822*0Sstevel@tonic-gateControls the style of indentation. It can be set to 0, 1, 2 or 3. Style 0 823*0Sstevel@tonic-gatespews output without any newlines, indentation, or spaces between list 824*0Sstevel@tonic-gateitems. It is the most compact format possible that can still be called 825*0Sstevel@tonic-gatevalid perl. Style 1 outputs a readable form with newlines but no fancy 826*0Sstevel@tonic-gateindentation (each level in the structure is simply indented by a fixed 827*0Sstevel@tonic-gateamount of whitespace). Style 2 (the default) outputs a very readable form 828*0Sstevel@tonic-gatewhich takes into account the length of hash keys (so the hash value lines 829*0Sstevel@tonic-gateup). Style 3 is like style 2, but also annotates the elements of arrays 830*0Sstevel@tonic-gatewith their index (but the comment is on its own line, so array output 831*0Sstevel@tonic-gateconsumes twice the number of lines). Style 2 is the default. 832*0Sstevel@tonic-gate 833*0Sstevel@tonic-gate=item * 834*0Sstevel@tonic-gate 835*0Sstevel@tonic-gate$Data::Dumper::Purity I<or> I<$OBJ>->Purity(I<[NEWVAL]>) 836*0Sstevel@tonic-gate 837*0Sstevel@tonic-gateControls the degree to which the output can be C<eval>ed to recreate the 838*0Sstevel@tonic-gatesupplied reference structures. Setting it to 1 will output additional perl 839*0Sstevel@tonic-gatestatements that will correctly recreate nested references. The default is 840*0Sstevel@tonic-gate0. 841*0Sstevel@tonic-gate 842*0Sstevel@tonic-gate=item * 843*0Sstevel@tonic-gate 844*0Sstevel@tonic-gate$Data::Dumper::Pad I<or> I<$OBJ>->Pad(I<[NEWVAL]>) 845*0Sstevel@tonic-gate 846*0Sstevel@tonic-gateSpecifies the string that will be prefixed to every line of the output. 847*0Sstevel@tonic-gateEmpty string by default. 848*0Sstevel@tonic-gate 849*0Sstevel@tonic-gate=item * 850*0Sstevel@tonic-gate 851*0Sstevel@tonic-gate$Data::Dumper::Varname I<or> I<$OBJ>->Varname(I<[NEWVAL]>) 852*0Sstevel@tonic-gate 853*0Sstevel@tonic-gateContains the prefix to use for tagging variable names in the output. The 854*0Sstevel@tonic-gatedefault is "VAR". 855*0Sstevel@tonic-gate 856*0Sstevel@tonic-gate=item * 857*0Sstevel@tonic-gate 858*0Sstevel@tonic-gate$Data::Dumper::Useqq I<or> I<$OBJ>->Useqq(I<[NEWVAL]>) 859*0Sstevel@tonic-gate 860*0Sstevel@tonic-gateWhen set, enables the use of double quotes for representing string values. 861*0Sstevel@tonic-gateWhitespace other than space will be represented as C<[\n\t\r]>, "unsafe" 862*0Sstevel@tonic-gatecharacters will be backslashed, and unprintable characters will be output as 863*0Sstevel@tonic-gatequoted octal integers. Since setting this variable imposes a performance 864*0Sstevel@tonic-gatepenalty, the default is 0. C<Dump()> will run slower if this flag is set, 865*0Sstevel@tonic-gatesince the fast XSUB implementation doesn't support it yet. 866*0Sstevel@tonic-gate 867*0Sstevel@tonic-gate=item * 868*0Sstevel@tonic-gate 869*0Sstevel@tonic-gate$Data::Dumper::Terse I<or> I<$OBJ>->Terse(I<[NEWVAL]>) 870*0Sstevel@tonic-gate 871*0Sstevel@tonic-gateWhen set, Data::Dumper will emit single, non-self-referential values as 872*0Sstevel@tonic-gateatoms/terms rather than statements. This means that the C<$VAR>I<n> names 873*0Sstevel@tonic-gatewill be avoided where possible, but be advised that such output may not 874*0Sstevel@tonic-gatealways be parseable by C<eval>. 875*0Sstevel@tonic-gate 876*0Sstevel@tonic-gate=item * 877*0Sstevel@tonic-gate 878*0Sstevel@tonic-gate$Data::Dumper::Freezer I<or> $I<OBJ>->Freezer(I<[NEWVAL]>) 879*0Sstevel@tonic-gate 880*0Sstevel@tonic-gateCan be set to a method name, or to an empty string to disable the feature. 881*0Sstevel@tonic-gateData::Dumper will invoke that method via the object before attempting to 882*0Sstevel@tonic-gatestringify it. This method can alter the contents of the object (if, for 883*0Sstevel@tonic-gateinstance, it contains data allocated from C), and even rebless it in a 884*0Sstevel@tonic-gatedifferent package. The client is responsible for making sure the specified 885*0Sstevel@tonic-gatemethod can be called via the object, and that the object ends up containing 886*0Sstevel@tonic-gateonly perl data types after the method has been called. Defaults to an empty 887*0Sstevel@tonic-gatestring. 888*0Sstevel@tonic-gate 889*0Sstevel@tonic-gate=item * 890*0Sstevel@tonic-gate 891*0Sstevel@tonic-gate$Data::Dumper::Toaster I<or> $I<OBJ>->Toaster(I<[NEWVAL]>) 892*0Sstevel@tonic-gate 893*0Sstevel@tonic-gateCan be set to a method name, or to an empty string to disable the feature. 894*0Sstevel@tonic-gateData::Dumper will emit a method call for any objects that are to be dumped 895*0Sstevel@tonic-gateusing the syntax C<bless(DATA, CLASS)-E<gt>METHOD()>. Note that this means that 896*0Sstevel@tonic-gatethe method specified will have to perform any modifications required on the 897*0Sstevel@tonic-gateobject (like creating new state within it, and/or reblessing it in a 898*0Sstevel@tonic-gatedifferent package) and then return it. The client is responsible for making 899*0Sstevel@tonic-gatesure the method can be called via the object, and that it returns a valid 900*0Sstevel@tonic-gateobject. Defaults to an empty string. 901*0Sstevel@tonic-gate 902*0Sstevel@tonic-gate=item * 903*0Sstevel@tonic-gate 904*0Sstevel@tonic-gate$Data::Dumper::Deepcopy I<or> $I<OBJ>->Deepcopy(I<[NEWVAL]>) 905*0Sstevel@tonic-gate 906*0Sstevel@tonic-gateCan be set to a boolean value to enable deep copies of structures. 907*0Sstevel@tonic-gateCross-referencing will then only be done when absolutely essential 908*0Sstevel@tonic-gate(i.e., to break reference cycles). Default is 0. 909*0Sstevel@tonic-gate 910*0Sstevel@tonic-gate=item * 911*0Sstevel@tonic-gate 912*0Sstevel@tonic-gate$Data::Dumper::Quotekeys I<or> $I<OBJ>->Quotekeys(I<[NEWVAL]>) 913*0Sstevel@tonic-gate 914*0Sstevel@tonic-gateCan be set to a boolean value to control whether hash keys are quoted. 915*0Sstevel@tonic-gateA false value will avoid quoting hash keys when it looks like a simple 916*0Sstevel@tonic-gatestring. Default is 1, which will always enclose hash keys in quotes. 917*0Sstevel@tonic-gate 918*0Sstevel@tonic-gate=item * 919*0Sstevel@tonic-gate 920*0Sstevel@tonic-gate$Data::Dumper::Bless I<or> $I<OBJ>->Bless(I<[NEWVAL]>) 921*0Sstevel@tonic-gate 922*0Sstevel@tonic-gateCan be set to a string that specifies an alternative to the C<bless> 923*0Sstevel@tonic-gatebuiltin operator used to create objects. A function with the specified 924*0Sstevel@tonic-gatename should exist, and should accept the same arguments as the builtin. 925*0Sstevel@tonic-gateDefault is C<bless>. 926*0Sstevel@tonic-gate 927*0Sstevel@tonic-gate=item * 928*0Sstevel@tonic-gate 929*0Sstevel@tonic-gate$Data::Dumper::Pair I<or> $I<OBJ>->Pair(I<[NEWVAL]>) 930*0Sstevel@tonic-gate 931*0Sstevel@tonic-gateCan be set to a string that specifies the separator between hash keys 932*0Sstevel@tonic-gateand values. To dump nested hash, array and scalar values to JavaScript, 933*0Sstevel@tonic-gateuse: C<$Data::Dumper::Pair = ' : ';>. Implementing C<bless> in JavaScript 934*0Sstevel@tonic-gateis left as an exercise for the reader. 935*0Sstevel@tonic-gateA function with the specified name exists, and accepts the same arguments 936*0Sstevel@tonic-gateas the builtin. 937*0Sstevel@tonic-gate 938*0Sstevel@tonic-gateDefault is: C< =E<gt> >. 939*0Sstevel@tonic-gate 940*0Sstevel@tonic-gate=item * 941*0Sstevel@tonic-gate 942*0Sstevel@tonic-gate$Data::Dumper::Maxdepth I<or> $I<OBJ>->Maxdepth(I<[NEWVAL]>) 943*0Sstevel@tonic-gate 944*0Sstevel@tonic-gateCan be set to a positive integer that specifies the depth beyond which 945*0Sstevel@tonic-gatewhich we don't venture into a structure. Has no effect when 946*0Sstevel@tonic-gateC<Data::Dumper::Purity> is set. (Useful in debugger when we often don't 947*0Sstevel@tonic-gatewant to see more than enough). Default is 0, which means there is 948*0Sstevel@tonic-gateno maximum depth. 949*0Sstevel@tonic-gate 950*0Sstevel@tonic-gate=item * 951*0Sstevel@tonic-gate 952*0Sstevel@tonic-gate$Data::Dumper::Useperl I<or> $I<OBJ>->Useperl(I<[NEWVAL]>) 953*0Sstevel@tonic-gate 954*0Sstevel@tonic-gateCan be set to a boolean value which controls whether the pure Perl 955*0Sstevel@tonic-gateimplementation of C<Data::Dumper> is used. The C<Data::Dumper> module is 956*0Sstevel@tonic-gatea dual implementation, with almost all functionality written in both 957*0Sstevel@tonic-gatepure Perl and also in XS ('C'). Since the XS version is much faster, it 958*0Sstevel@tonic-gatewill always be used if possible. This option lets you override the 959*0Sstevel@tonic-gatedefault behavior, usually for testing purposes only. Default is 0, which 960*0Sstevel@tonic-gatemeans the XS implementation will be used if possible. 961*0Sstevel@tonic-gate 962*0Sstevel@tonic-gate=item * 963*0Sstevel@tonic-gate 964*0Sstevel@tonic-gate$Data::Dumper::Sortkeys I<or> $I<OBJ>->Sortkeys(I<[NEWVAL]>) 965*0Sstevel@tonic-gate 966*0Sstevel@tonic-gateCan be set to a boolean value to control whether hash keys are dumped in 967*0Sstevel@tonic-gatesorted order. A true value will cause the keys of all hashes to be 968*0Sstevel@tonic-gatedumped in Perl's default sort order. Can also be set to a subroutine 969*0Sstevel@tonic-gatereference which will be called for each hash that is dumped. In this 970*0Sstevel@tonic-gatecase C<Data::Dumper> will call the subroutine once for each hash, 971*0Sstevel@tonic-gatepassing it the reference of the hash. The purpose of the subroutine is 972*0Sstevel@tonic-gateto return a reference to an array of the keys that will be dumped, in 973*0Sstevel@tonic-gatethe order that they should be dumped. Using this feature, you can 974*0Sstevel@tonic-gatecontrol both the order of the keys, and which keys are actually used. In 975*0Sstevel@tonic-gateother words, this subroutine acts as a filter by which you can exclude 976*0Sstevel@tonic-gatecertain keys from being dumped. Default is 0, which means that hash keys 977*0Sstevel@tonic-gateare not sorted. 978*0Sstevel@tonic-gate 979*0Sstevel@tonic-gate=item * 980*0Sstevel@tonic-gate 981*0Sstevel@tonic-gate$Data::Dumper::Deparse I<or> $I<OBJ>->Deparse(I<[NEWVAL]>) 982*0Sstevel@tonic-gate 983*0Sstevel@tonic-gateCan be set to a boolean value to control whether code references are 984*0Sstevel@tonic-gateturned into perl source code. If set to a true value, C<B::Deparse> 985*0Sstevel@tonic-gatewill be used to get the source of the code reference. Using this option 986*0Sstevel@tonic-gatewill force using the Perl implementation of the dumper, since the fast 987*0Sstevel@tonic-gateXSUB implementation doesn't support it. 988*0Sstevel@tonic-gate 989*0Sstevel@tonic-gateCaution : use this option only if you know that your coderefs will be 990*0Sstevel@tonic-gateproperly reconstructed by C<B::Deparse>. 991*0Sstevel@tonic-gate 992*0Sstevel@tonic-gate=back 993*0Sstevel@tonic-gate 994*0Sstevel@tonic-gate=head2 Exports 995*0Sstevel@tonic-gate 996*0Sstevel@tonic-gate=over 4 997*0Sstevel@tonic-gate 998*0Sstevel@tonic-gate=item Dumper 999*0Sstevel@tonic-gate 1000*0Sstevel@tonic-gate=back 1001*0Sstevel@tonic-gate 1002*0Sstevel@tonic-gate=head1 EXAMPLES 1003*0Sstevel@tonic-gate 1004*0Sstevel@tonic-gateRun these code snippets to get a quick feel for the behavior of this 1005*0Sstevel@tonic-gatemodule. When you are through with these examples, you may want to 1006*0Sstevel@tonic-gateadd or change the various configuration variables described above, 1007*0Sstevel@tonic-gateto see their behavior. (See the testsuite in the Data::Dumper 1008*0Sstevel@tonic-gatedistribution for more examples.) 1009*0Sstevel@tonic-gate 1010*0Sstevel@tonic-gate 1011*0Sstevel@tonic-gate use Data::Dumper; 1012*0Sstevel@tonic-gate 1013*0Sstevel@tonic-gate package Foo; 1014*0Sstevel@tonic-gate sub new {bless {'a' => 1, 'b' => sub { return "foo" }}, $_[0]}; 1015*0Sstevel@tonic-gate 1016*0Sstevel@tonic-gate package Fuz; # a weird REF-REF-SCALAR object 1017*0Sstevel@tonic-gate sub new {bless \($_ = \ 'fu\'z'), $_[0]}; 1018*0Sstevel@tonic-gate 1019*0Sstevel@tonic-gate package main; 1020*0Sstevel@tonic-gate $foo = Foo->new; 1021*0Sstevel@tonic-gate $fuz = Fuz->new; 1022*0Sstevel@tonic-gate $boo = [ 1, [], "abcd", \*foo, 1023*0Sstevel@tonic-gate {1 => 'a', 023 => 'b', 0x45 => 'c'}, 1024*0Sstevel@tonic-gate \\"p\q\'r", $foo, $fuz]; 1025*0Sstevel@tonic-gate 1026*0Sstevel@tonic-gate ######## 1027*0Sstevel@tonic-gate # simple usage 1028*0Sstevel@tonic-gate ######## 1029*0Sstevel@tonic-gate 1030*0Sstevel@tonic-gate $bar = eval(Dumper($boo)); 1031*0Sstevel@tonic-gate print($@) if $@; 1032*0Sstevel@tonic-gate print Dumper($boo), Dumper($bar); # pretty print (no array indices) 1033*0Sstevel@tonic-gate 1034*0Sstevel@tonic-gate $Data::Dumper::Terse = 1; # don't output names where feasible 1035*0Sstevel@tonic-gate $Data::Dumper::Indent = 0; # turn off all pretty print 1036*0Sstevel@tonic-gate print Dumper($boo), "\n"; 1037*0Sstevel@tonic-gate 1038*0Sstevel@tonic-gate $Data::Dumper::Indent = 1; # mild pretty print 1039*0Sstevel@tonic-gate print Dumper($boo); 1040*0Sstevel@tonic-gate 1041*0Sstevel@tonic-gate $Data::Dumper::Indent = 3; # pretty print with array indices 1042*0Sstevel@tonic-gate print Dumper($boo); 1043*0Sstevel@tonic-gate 1044*0Sstevel@tonic-gate $Data::Dumper::Useqq = 1; # print strings in double quotes 1045*0Sstevel@tonic-gate print Dumper($boo); 1046*0Sstevel@tonic-gate 1047*0Sstevel@tonic-gate $Data::Dumper::Pair = " : "; # specify hash key/value separator 1048*0Sstevel@tonic-gate print Dumper($boo); 1049*0Sstevel@tonic-gate 1050*0Sstevel@tonic-gate 1051*0Sstevel@tonic-gate ######## 1052*0Sstevel@tonic-gate # recursive structures 1053*0Sstevel@tonic-gate ######## 1054*0Sstevel@tonic-gate 1055*0Sstevel@tonic-gate @c = ('c'); 1056*0Sstevel@tonic-gate $c = \@c; 1057*0Sstevel@tonic-gate $b = {}; 1058*0Sstevel@tonic-gate $a = [1, $b, $c]; 1059*0Sstevel@tonic-gate $b->{a} = $a; 1060*0Sstevel@tonic-gate $b->{b} = $a->[1]; 1061*0Sstevel@tonic-gate $b->{c} = $a->[2]; 1062*0Sstevel@tonic-gate print Data::Dumper->Dump([$a,$b,$c], [qw(a b c)]); 1063*0Sstevel@tonic-gate 1064*0Sstevel@tonic-gate 1065*0Sstevel@tonic-gate $Data::Dumper::Purity = 1; # fill in the holes for eval 1066*0Sstevel@tonic-gate print Data::Dumper->Dump([$a, $b], [qw(*a b)]); # print as @a 1067*0Sstevel@tonic-gate print Data::Dumper->Dump([$b, $a], [qw(*b a)]); # print as %b 1068*0Sstevel@tonic-gate 1069*0Sstevel@tonic-gate 1070*0Sstevel@tonic-gate $Data::Dumper::Deepcopy = 1; # avoid cross-refs 1071*0Sstevel@tonic-gate print Data::Dumper->Dump([$b, $a], [qw(*b a)]); 1072*0Sstevel@tonic-gate 1073*0Sstevel@tonic-gate 1074*0Sstevel@tonic-gate $Data::Dumper::Purity = 0; # avoid cross-refs 1075*0Sstevel@tonic-gate print Data::Dumper->Dump([$b, $a], [qw(*b a)]); 1076*0Sstevel@tonic-gate 1077*0Sstevel@tonic-gate ######## 1078*0Sstevel@tonic-gate # deep structures 1079*0Sstevel@tonic-gate ######## 1080*0Sstevel@tonic-gate 1081*0Sstevel@tonic-gate $a = "pearl"; 1082*0Sstevel@tonic-gate $b = [ $a ]; 1083*0Sstevel@tonic-gate $c = { 'b' => $b }; 1084*0Sstevel@tonic-gate $d = [ $c ]; 1085*0Sstevel@tonic-gate $e = { 'd' => $d }; 1086*0Sstevel@tonic-gate $f = { 'e' => $e }; 1087*0Sstevel@tonic-gate print Data::Dumper->Dump([$f], [qw(f)]); 1088*0Sstevel@tonic-gate 1089*0Sstevel@tonic-gate $Data::Dumper::Maxdepth = 3; # no deeper than 3 refs down 1090*0Sstevel@tonic-gate print Data::Dumper->Dump([$f], [qw(f)]); 1091*0Sstevel@tonic-gate 1092*0Sstevel@tonic-gate 1093*0Sstevel@tonic-gate ######## 1094*0Sstevel@tonic-gate # object-oriented usage 1095*0Sstevel@tonic-gate ######## 1096*0Sstevel@tonic-gate 1097*0Sstevel@tonic-gate $d = Data::Dumper->new([$a,$b], [qw(a b)]); 1098*0Sstevel@tonic-gate $d->Seen({'*c' => $c}); # stash a ref without printing it 1099*0Sstevel@tonic-gate $d->Indent(3); 1100*0Sstevel@tonic-gate print $d->Dump; 1101*0Sstevel@tonic-gate $d->Reset->Purity(0); # empty the seen cache 1102*0Sstevel@tonic-gate print join "----\n", $d->Dump; 1103*0Sstevel@tonic-gate 1104*0Sstevel@tonic-gate 1105*0Sstevel@tonic-gate ######## 1106*0Sstevel@tonic-gate # persistence 1107*0Sstevel@tonic-gate ######## 1108*0Sstevel@tonic-gate 1109*0Sstevel@tonic-gate package Foo; 1110*0Sstevel@tonic-gate sub new { bless { state => 'awake' }, shift } 1111*0Sstevel@tonic-gate sub Freeze { 1112*0Sstevel@tonic-gate my $s = shift; 1113*0Sstevel@tonic-gate print STDERR "preparing to sleep\n"; 1114*0Sstevel@tonic-gate $s->{state} = 'asleep'; 1115*0Sstevel@tonic-gate return bless $s, 'Foo::ZZZ'; 1116*0Sstevel@tonic-gate } 1117*0Sstevel@tonic-gate 1118*0Sstevel@tonic-gate package Foo::ZZZ; 1119*0Sstevel@tonic-gate sub Thaw { 1120*0Sstevel@tonic-gate my $s = shift; 1121*0Sstevel@tonic-gate print STDERR "waking up\n"; 1122*0Sstevel@tonic-gate $s->{state} = 'awake'; 1123*0Sstevel@tonic-gate return bless $s, 'Foo'; 1124*0Sstevel@tonic-gate } 1125*0Sstevel@tonic-gate 1126*0Sstevel@tonic-gate package Foo; 1127*0Sstevel@tonic-gate use Data::Dumper; 1128*0Sstevel@tonic-gate $a = Foo->new; 1129*0Sstevel@tonic-gate $b = Data::Dumper->new([$a], ['c']); 1130*0Sstevel@tonic-gate $b->Freezer('Freeze'); 1131*0Sstevel@tonic-gate $b->Toaster('Thaw'); 1132*0Sstevel@tonic-gate $c = $b->Dump; 1133*0Sstevel@tonic-gate print $c; 1134*0Sstevel@tonic-gate $d = eval $c; 1135*0Sstevel@tonic-gate print Data::Dumper->Dump([$d], ['d']); 1136*0Sstevel@tonic-gate 1137*0Sstevel@tonic-gate 1138*0Sstevel@tonic-gate ######## 1139*0Sstevel@tonic-gate # symbol substitution (useful for recreating CODE refs) 1140*0Sstevel@tonic-gate ######## 1141*0Sstevel@tonic-gate 1142*0Sstevel@tonic-gate sub foo { print "foo speaking\n" } 1143*0Sstevel@tonic-gate *other = \&foo; 1144*0Sstevel@tonic-gate $bar = [ \&other ]; 1145*0Sstevel@tonic-gate $d = Data::Dumper->new([\&other,$bar],['*other','bar']); 1146*0Sstevel@tonic-gate $d->Seen({ '*foo' => \&foo }); 1147*0Sstevel@tonic-gate print $d->Dump; 1148*0Sstevel@tonic-gate 1149*0Sstevel@tonic-gate 1150*0Sstevel@tonic-gate ######## 1151*0Sstevel@tonic-gate # sorting and filtering hash keys 1152*0Sstevel@tonic-gate ######## 1153*0Sstevel@tonic-gate 1154*0Sstevel@tonic-gate $Data::Dumper::Sortkeys = \&my_filter; 1155*0Sstevel@tonic-gate my $foo = { map { (ord, "$_$_$_") } 'I'..'Q' }; 1156*0Sstevel@tonic-gate my $bar = { %$foo }; 1157*0Sstevel@tonic-gate my $baz = { reverse %$foo }; 1158*0Sstevel@tonic-gate print Dumper [ $foo, $bar, $baz ]; 1159*0Sstevel@tonic-gate 1160*0Sstevel@tonic-gate sub my_filter { 1161*0Sstevel@tonic-gate my ($hash) = @_; 1162*0Sstevel@tonic-gate # return an array ref containing the hash keys to dump 1163*0Sstevel@tonic-gate # in the order that you want them to be dumped 1164*0Sstevel@tonic-gate return [ 1165*0Sstevel@tonic-gate # Sort the keys of %$foo in reverse numeric order 1166*0Sstevel@tonic-gate $hash eq $foo ? (sort {$b <=> $a} keys %$hash) : 1167*0Sstevel@tonic-gate # Only dump the odd number keys of %$bar 1168*0Sstevel@tonic-gate $hash eq $bar ? (grep {$_ % 2} keys %$hash) : 1169*0Sstevel@tonic-gate # Sort keys in default order for all other hashes 1170*0Sstevel@tonic-gate (sort keys %$hash) 1171*0Sstevel@tonic-gate ]; 1172*0Sstevel@tonic-gate } 1173*0Sstevel@tonic-gate 1174*0Sstevel@tonic-gate=head1 BUGS 1175*0Sstevel@tonic-gate 1176*0Sstevel@tonic-gateDue to limitations of Perl subroutine call semantics, you cannot pass an 1177*0Sstevel@tonic-gatearray or hash. Prepend it with a C<\> to pass its reference instead. This 1178*0Sstevel@tonic-gatewill be remedied in time, now that Perl has subroutine prototypes. 1179*0Sstevel@tonic-gateFor now, you need to use the extended usage form, and prepend the 1180*0Sstevel@tonic-gatename with a C<*> to output it as a hash or array. 1181*0Sstevel@tonic-gate 1182*0Sstevel@tonic-gateC<Data::Dumper> cheats with CODE references. If a code reference is 1183*0Sstevel@tonic-gateencountered in the structure being processed (and if you haven't set 1184*0Sstevel@tonic-gatethe C<Deparse> flag), an anonymous subroutine that 1185*0Sstevel@tonic-gatecontains the string '"DUMMY"' will be inserted in its place, and a warning 1186*0Sstevel@tonic-gatewill be printed if C<Purity> is set. You can C<eval> the result, but bear 1187*0Sstevel@tonic-gatein mind that the anonymous sub that gets created is just a placeholder. 1188*0Sstevel@tonic-gateSomeday, perl will have a switch to cache-on-demand the string 1189*0Sstevel@tonic-gaterepresentation of a compiled piece of code, I hope. If you have prior 1190*0Sstevel@tonic-gateknowledge of all the code refs that your data structures are likely 1191*0Sstevel@tonic-gateto have, you can use the C<Seen> method to pre-seed the internal reference 1192*0Sstevel@tonic-gatetable and make the dumped output point to them, instead. See L<EXAMPLES> 1193*0Sstevel@tonic-gateabove. 1194*0Sstevel@tonic-gate 1195*0Sstevel@tonic-gateThe C<Useqq> and C<Deparse> flags makes Dump() run slower, since the 1196*0Sstevel@tonic-gateXSUB implementation does not support them. 1197*0Sstevel@tonic-gate 1198*0Sstevel@tonic-gateSCALAR objects have the weirdest looking C<bless> workaround. 1199*0Sstevel@tonic-gate 1200*0Sstevel@tonic-gatePure Perl version of C<Data::Dumper> escapes UTF-8 strings correctly 1201*0Sstevel@tonic-gateonly in Perl 5.8.0 and later. 1202*0Sstevel@tonic-gate 1203*0Sstevel@tonic-gate=head2 NOTE 1204*0Sstevel@tonic-gate 1205*0Sstevel@tonic-gateStarting from Perl 5.8.1 different runs of Perl will have different 1206*0Sstevel@tonic-gateordering of hash keys. The change was done for greater security, 1207*0Sstevel@tonic-gatesee L<perlsec/"Algorithmic Complexity Attacks">. This means that 1208*0Sstevel@tonic-gatedifferent runs of Perl will have different Data::Dumper outputs if 1209*0Sstevel@tonic-gatethe data contains hashes. If you need to have identical Data::Dumper 1210*0Sstevel@tonic-gateoutputs from different runs of Perl, use the environment variable 1211*0Sstevel@tonic-gatePERL_HASH_SEED, see L<perlrun/PERL_HASH_SEED>. Using this restores 1212*0Sstevel@tonic-gatethe old (platform-specific) ordering: an even prettier solution might 1213*0Sstevel@tonic-gatebe to use the C<Sortkeys> filter of Data::Dumper. 1214*0Sstevel@tonic-gate 1215*0Sstevel@tonic-gate=head1 AUTHOR 1216*0Sstevel@tonic-gate 1217*0Sstevel@tonic-gateGurusamy Sarathy gsar@activestate.com 1218*0Sstevel@tonic-gate 1219*0Sstevel@tonic-gateCopyright (c) 1996-98 Gurusamy Sarathy. All rights reserved. 1220*0Sstevel@tonic-gateThis program is free software; you can redistribute it and/or 1221*0Sstevel@tonic-gatemodify it under the same terms as Perl itself. 1222*0Sstevel@tonic-gate 1223*0Sstevel@tonic-gate=head1 VERSION 1224*0Sstevel@tonic-gate 1225*0Sstevel@tonic-gateVersion 2.121 (Aug 24 2003) 1226*0Sstevel@tonic-gate 1227*0Sstevel@tonic-gate=head1 SEE ALSO 1228*0Sstevel@tonic-gate 1229*0Sstevel@tonic-gateperl(1) 1230*0Sstevel@tonic-gate 1231*0Sstevel@tonic-gate=cut 1232