Blame Dumper.pm

Packit dfacfc
#
Packit dfacfc
# Data/Dumper.pm
Packit dfacfc
#
Packit dfacfc
# convert perl data structures into perl syntax suitable for both printing
Packit dfacfc
# and eval
Packit dfacfc
#
Packit dfacfc
# Documentation at the __END__
Packit dfacfc
#
Packit dfacfc
Packit dfacfc
package Data::Dumper;
Packit dfacfc
Packit dfacfc
BEGIN {
Packit dfacfc
    $VERSION = '2.161'; # Don't forget to set version and release
Packit dfacfc
}               # date in POD below!
Packit dfacfc
Packit dfacfc
#$| = 1;
Packit dfacfc
Packit dfacfc
use 5.006_001;
Packit dfacfc
require Exporter;
Packit dfacfc
require overload;
Packit dfacfc
Packit dfacfc
use Carp;
Packit dfacfc
Packit dfacfc
BEGIN {
Packit dfacfc
    @ISA = qw(Exporter);
Packit dfacfc
    @EXPORT = qw(Dumper);
Packit dfacfc
    @EXPORT_OK = qw(DumperX);
Packit dfacfc
Packit dfacfc
    # if run under miniperl, or otherwise lacking dynamic loading,
Packit dfacfc
    # XSLoader should be attempted to load, or the pure perl flag
Packit dfacfc
    # toggled on load failure.
Packit dfacfc
    eval {
Packit dfacfc
        require XSLoader;
Packit dfacfc
        XSLoader::load( 'Data::Dumper' );
Packit dfacfc
        1
Packit dfacfc
    }
Packit dfacfc
    or $Useperl = 1;
Packit dfacfc
}
Packit dfacfc
Packit dfacfc
my $IS_ASCII  = ord 'A' ==  65;
Packit dfacfc
Packit dfacfc
# module vars and their defaults
Packit dfacfc
$Indent     = 2         unless defined $Indent;
Packit dfacfc
$Trailingcomma = 0      unless defined $Trailingcomma;
Packit dfacfc
$Purity     = 0         unless defined $Purity;
Packit dfacfc
$Pad        = ""        unless defined $Pad;
Packit dfacfc
$Varname    = "VAR"     unless defined $Varname;
Packit dfacfc
$Useqq      = 0         unless defined $Useqq;
Packit dfacfc
$Terse      = 0         unless defined $Terse;
Packit dfacfc
$Freezer    = ""        unless defined $Freezer;
Packit dfacfc
$Toaster    = ""        unless defined $Toaster;
Packit dfacfc
$Deepcopy   = 0         unless defined $Deepcopy;
Packit dfacfc
$Quotekeys  = 1         unless defined $Quotekeys;
Packit dfacfc
$Bless      = "bless"   unless defined $Bless;
Packit dfacfc
#$Expdepth   = 0         unless defined $Expdepth;
Packit dfacfc
$Maxdepth   = 0         unless defined $Maxdepth;
Packit dfacfc
$Pair       = ' => '    unless defined $Pair;
Packit dfacfc
$Useperl    = 0         unless defined $Useperl;
Packit dfacfc
$Sortkeys   = 0         unless defined $Sortkeys;
Packit dfacfc
$Deparse    = 0         unless defined $Deparse;
Packit dfacfc
$Sparseseen = 0         unless defined $Sparseseen;
Packit dfacfc
$Maxrecurse = 1000      unless defined $Maxrecurse;
Packit dfacfc
Packit dfacfc
#
Packit dfacfc
# expects an arrayref of values to be dumped.
Packit dfacfc
# can optionally pass an arrayref of names for the values.
Packit dfacfc
# names must have leading $ sign stripped. begin the name with *
Packit dfacfc
# to cause output of arrays and hashes rather than refs.
Packit dfacfc
#
Packit dfacfc
sub new {
Packit dfacfc
  my($c, $v, $n) = @_;
Packit dfacfc
Packit dfacfc
  croak "Usage:  PACKAGE->new(ARRAYREF, [ARRAYREF])"
Packit dfacfc
    unless (defined($v) && (ref($v) eq 'ARRAY'));
Packit dfacfc
  $n = [] unless (defined($n) && (ref($n) eq 'ARRAY'));
Packit dfacfc
Packit dfacfc
  my($s) = {
Packit dfacfc
        level      => 0,           # current recursive depth
Packit dfacfc
        indent     => $Indent,     # various styles of indenting
Packit dfacfc
        trailingcomma => $Trailingcomma, # whether to add comma after last elem
Packit dfacfc
        pad        => $Pad,        # all lines prefixed by this string
Packit dfacfc
        xpad       => "",          # padding-per-level
Packit dfacfc
        apad       => "",          # added padding for hash keys n such
Packit dfacfc
        sep        => "",          # list separator
Packit dfacfc
        pair       => $Pair,    # hash key/value separator: defaults to ' => '
Packit dfacfc
        seen       => {},          # local (nested) refs (id => [name, val])
Packit dfacfc
        todump     => $v,          # values to dump []
Packit dfacfc
        names      => $n,          # optional names for values []
Packit dfacfc
        varname    => $Varname,    # prefix to use for tagging nameless ones
Packit dfacfc
        purity     => $Purity,     # degree to which output is evalable
Packit dfacfc
        useqq      => $Useqq,      # use "" for strings (backslashitis ensues)
Packit dfacfc
        terse      => $Terse,      # avoid name output (where feasible)
Packit dfacfc
        freezer    => $Freezer,    # name of Freezer method for objects
Packit dfacfc
        toaster    => $Toaster,    # name of method to revive objects
Packit dfacfc
        deepcopy   => $Deepcopy,   # do not cross-ref, except to stop recursion
Packit dfacfc
        quotekeys  => $Quotekeys,  # quote hash keys
Packit dfacfc
        'bless'    => $Bless,    # keyword to use for "bless"
Packit dfacfc
#        expdepth   => $Expdepth,   # cutoff depth for explicit dumping
Packit dfacfc
        maxdepth   => $Maxdepth,   # depth beyond which we give up
Packit dfacfc
	maxrecurse => $Maxrecurse, # depth beyond which we abort
Packit dfacfc
        useperl    => $Useperl,    # use the pure Perl implementation
Packit dfacfc
        sortkeys   => $Sortkeys,   # flag or filter for sorting hash keys
Packit dfacfc
        deparse    => $Deparse,    # use B::Deparse for coderefs
Packit dfacfc
        noseen     => $Sparseseen, # do not populate the seen hash unless necessary
Packit dfacfc
       };
Packit dfacfc
Packit dfacfc
  if ($Indent > 0) {
Packit dfacfc
    $s->{xpad} = "  ";
Packit dfacfc
    $s->{sep} = "\n";
Packit dfacfc
  }
Packit dfacfc
  return bless($s, $c);
Packit dfacfc
}
Packit dfacfc
Packit dfacfc
# Packed numeric addresses take less memory. Plus pack is faster than sprintf
Packit dfacfc
Packit dfacfc
# Most users of current versions of Data::Dumper will be 5.008 or later.
Packit dfacfc
# Anyone on 5.6.1 and 5.6.2 upgrading will be rare (particularly judging by
Packit dfacfc
# the bug reports from users on those platforms), so for the common case avoid
Packit dfacfc
# complexity, and avoid even compiling the unneeded code.
Packit dfacfc
Packit dfacfc
sub init_refaddr_format {
Packit dfacfc
}
Packit dfacfc
Packit dfacfc
sub format_refaddr {
Packit dfacfc
    require Scalar::Util;
Packit dfacfc
    pack "J", Scalar::Util::refaddr(shift);
Packit dfacfc
};
Packit dfacfc
Packit dfacfc
if ($] < 5.008) {
Packit dfacfc
    eval <<'EOC' or die;
Packit dfacfc
    no warnings 'redefine';
Packit dfacfc
    my $refaddr_format;
Packit dfacfc
    sub init_refaddr_format {
Packit dfacfc
        require Config;
Packit dfacfc
        my $f = $Config::Config{uvxformat};
Packit dfacfc
        $f =~ tr/"//d;
Packit dfacfc
        $refaddr_format = "0x%" . $f;
Packit dfacfc
    }
Packit dfacfc
Packit dfacfc
    sub format_refaddr {
Packit dfacfc
        require Scalar::Util;
Packit dfacfc
        sprintf $refaddr_format, Scalar::Util::refaddr(shift);
Packit dfacfc
    }
Packit dfacfc
Packit dfacfc
    1
Packit dfacfc
EOC
Packit dfacfc
}
Packit dfacfc
Packit dfacfc
#
Packit dfacfc
# add-to or query the table of already seen references
Packit dfacfc
#
Packit dfacfc
sub Seen {
Packit dfacfc
  my($s, $g) = @_;
Packit dfacfc
  if (defined($g) && (ref($g) eq 'HASH'))  {
Packit dfacfc
    init_refaddr_format();
Packit dfacfc
    my($k, $v, $id);
Packit dfacfc
    while (($k, $v) = each %$g) {
Packit dfacfc
      if (defined $v) {
Packit dfacfc
        if (ref $v) {
Packit dfacfc
          $id = format_refaddr($v);
Packit dfacfc
          if ($k =~ /^[*](.*)$/) {
Packit dfacfc
            $k = (ref $v eq 'ARRAY') ? ( "\\\@" . $1 ) :
Packit dfacfc
                 (ref $v eq 'HASH')  ? ( "\\\%" . $1 ) :
Packit dfacfc
                 (ref $v eq 'CODE')  ? ( "\\\&" . $1 ) :
Packit dfacfc
                 (   "\$" . $1 ) ;
Packit dfacfc
          }
Packit dfacfc
          elsif ($k !~ /^\$/) {
Packit dfacfc
            $k = "\$" . $k;
Packit dfacfc
          }
Packit dfacfc
          $s->{seen}{$id} = [$k, $v];
Packit dfacfc
        }
Packit dfacfc
        else {
Packit dfacfc
          carp "Only refs supported, ignoring non-ref item \$$k";
Packit dfacfc
        }
Packit dfacfc
      }
Packit dfacfc
      else {
Packit dfacfc
        carp "Value of ref must be defined; ignoring undefined item \$$k";
Packit dfacfc
      }
Packit dfacfc
    }
Packit dfacfc
    return $s;
Packit dfacfc
  }
Packit dfacfc
  else {
Packit dfacfc
    return map { @$_ } values %{$s->{seen}};
Packit dfacfc
  }
Packit dfacfc
}
Packit dfacfc
Packit dfacfc
#
Packit dfacfc
# set or query the values to be dumped
Packit dfacfc
#
Packit dfacfc
sub Values {
Packit dfacfc
  my($s, $v) = @_;
Packit dfacfc
  if (defined($v)) {
Packit dfacfc
    if (ref($v) eq 'ARRAY')  {
Packit dfacfc
      $s->{todump} = [@$v];        # make a copy
Packit dfacfc
      return $s;
Packit dfacfc
    }
Packit dfacfc
    else {
Packit dfacfc
      croak "Argument to Values, if provided, must be array ref";
Packit dfacfc
    }
Packit dfacfc
  }
Packit dfacfc
  else {
Packit dfacfc
    return @{$s->{todump}};
Packit dfacfc
  }
Packit dfacfc
}
Packit dfacfc
Packit dfacfc
#
Packit dfacfc
# set or query the names of the values to be dumped
Packit dfacfc
#
Packit dfacfc
sub Names {
Packit dfacfc
  my($s, $n) = @_;
Packit dfacfc
  if (defined($n)) {
Packit dfacfc
    if (ref($n) eq 'ARRAY') {
Packit dfacfc
      $s->{names} = [@$n];         # make a copy
Packit dfacfc
      return $s;
Packit dfacfc
    }
Packit dfacfc
    else {
Packit dfacfc
      croak "Argument to Names, if provided, must be array ref";
Packit dfacfc
    }
Packit dfacfc
  }
Packit dfacfc
  else {
Packit dfacfc
    return @{$s->{names}};
Packit dfacfc
  }
Packit dfacfc
}
Packit dfacfc
Packit dfacfc
sub DESTROY {}
Packit dfacfc
Packit dfacfc
sub Dump {
Packit dfacfc
    return &Dumpxs
Packit dfacfc
    unless $Data::Dumper::Useperl || (ref($_[0]) && $_[0]->{useperl})
Packit dfacfc
        || $Data::Dumper::Deparse || (ref($_[0]) && $_[0]->{deparse})
Packit dfacfc
Packit dfacfc
            # Use pure perl version on earlier releases on EBCDIC platforms
Packit dfacfc
        || (! $IS_ASCII && $] lt 5.021_010);
Packit dfacfc
    return &Dumpperl;
Packit dfacfc
}
Packit dfacfc
Packit dfacfc
#
Packit dfacfc
# dump the refs in the current dumper object.
Packit dfacfc
# expects same args as new() if called via package name.
Packit dfacfc
#
Packit dfacfc
sub Dumpperl {
Packit dfacfc
  my($s) = shift;
Packit dfacfc
  my(@out, $val, $name);
Packit dfacfc
  my($i) = 0;
Packit dfacfc
  local(@post);
Packit dfacfc
  init_refaddr_format();
Packit dfacfc
Packit dfacfc
  $s = $s->new(@_) unless ref $s;
Packit dfacfc
Packit dfacfc
  for $val (@{$s->{todump}}) {
Packit dfacfc
    @post = ();
Packit dfacfc
    $name = $s->{names}[$i++];
Packit dfacfc
    $name = $s->_refine_name($name, $val, $i);
Packit dfacfc
Packit dfacfc
    my $valstr;
Packit dfacfc
    {
Packit dfacfc
      local($s->{apad}) = $s->{apad};
Packit dfacfc
      $s->{apad} .= ' ' x (length($name) + 3) if $s->{indent} >= 2 and !$s->{terse};
Packit dfacfc
      $valstr = $s->_dump($val, $name);
Packit dfacfc
    }
Packit dfacfc
Packit dfacfc
    $valstr = "$name = " . $valstr . ';' if @post or !$s->{terse};
Packit dfacfc
    my $out = $s->_compose_out($valstr, \@post);
Packit dfacfc
Packit dfacfc
    push @out, $out;
Packit dfacfc
  }
Packit dfacfc
  return wantarray ? @out : join('', @out);
Packit dfacfc
}
Packit dfacfc
Packit dfacfc
# wrap string in single quotes (escaping if needed)
Packit dfacfc
sub _quote {
Packit dfacfc
    my $val = shift;
Packit dfacfc
    $val =~ s/([\\\'])/\\$1/g;
Packit dfacfc
    return  "'" . $val .  "'";
Packit dfacfc
}
Packit dfacfc
Packit dfacfc
# Old Perls (5.14-) have trouble resetting vstring magic when it is no
Packit dfacfc
# longer valid.
Packit dfacfc
use constant _bad_vsmg => defined &_vstring && (_vstring(~v0)||'') eq "v0";
Packit dfacfc
Packit dfacfc
#
Packit dfacfc
# twist, toil and turn;
Packit dfacfc
# and recurse, of course.
Packit dfacfc
# sometimes sordidly;
Packit dfacfc
# and curse if no recourse.
Packit dfacfc
#
Packit dfacfc
sub _dump {
Packit dfacfc
  my($s, $val, $name) = @_;
Packit dfacfc
  my($out, $type, $id, $sname);
Packit dfacfc
Packit dfacfc
  $type = ref $val;
Packit dfacfc
  $out = "";
Packit dfacfc
Packit dfacfc
  if ($type) {
Packit dfacfc
Packit dfacfc
    # Call the freezer method if it's specified and the object has the
Packit dfacfc
    # method.  Trap errors and warn() instead of die()ing, like the XS
Packit dfacfc
    # implementation.
Packit dfacfc
    my $freezer = $s->{freezer};
Packit dfacfc
    if ($freezer and UNIVERSAL::can($val, $freezer)) {
Packit dfacfc
      eval { $val->$freezer() };
Packit dfacfc
      warn "WARNING(Freezer method call failed): $@" if $@;
Packit dfacfc
    }
Packit dfacfc
Packit dfacfc
    require Scalar::Util;
Packit dfacfc
    my $realpack = Scalar::Util::blessed($val);
Packit dfacfc
    my $realtype = $realpack ? Scalar::Util::reftype($val) : ref $val;
Packit dfacfc
    $id = format_refaddr($val);
Packit dfacfc
Packit dfacfc
    # Note: By this point $name is always defined and of non-zero length.
Packit dfacfc
    # Keep a tab on it so that we do not fall into recursive pit.
Packit dfacfc
    if (exists $s->{seen}{$id}) {
Packit dfacfc
      if ($s->{purity} and $s->{level} > 0) {
Packit dfacfc
        $out = ($realtype eq 'HASH')  ? '{}' :
Packit dfacfc
               ($realtype eq 'ARRAY') ? '[]' :
Packit dfacfc
               'do{my $o}' ;
Packit dfacfc
        push @post, $name . " = " . $s->{seen}{$id}[0];
Packit dfacfc
      }
Packit dfacfc
      else {
Packit dfacfc
        $out = $s->{seen}{$id}[0];
Packit dfacfc
        if ($name =~ /^([\@\%])/) {
Packit dfacfc
          my $start = $1;
Packit dfacfc
          if ($out =~ /^\\$start/) {
Packit dfacfc
            $out = substr($out, 1);
Packit dfacfc
          }
Packit dfacfc
          else {
Packit dfacfc
            $out = $start . '{' . $out . '}';
Packit dfacfc
          }
Packit dfacfc
        }
Packit dfacfc
      }
Packit dfacfc
      return $out;
Packit dfacfc
    }
Packit dfacfc
    else {
Packit dfacfc
      # store our name
Packit dfacfc
      $s->{seen}{$id} = [ (
Packit dfacfc
          ($name =~ /^[@%]/)
Packit dfacfc
            ? ('\\' . $name )
Packit dfacfc
            : ($realtype eq 'CODE' and $name =~ /^[*](.*)$/)
Packit dfacfc
              ? ('\\&' . $1 )
Packit dfacfc
              : $name
Packit dfacfc
        ), $val ];
Packit dfacfc
    }
Packit dfacfc
    my $no_bless = 0;
Packit dfacfc
    my $is_regex = 0;
Packit dfacfc
    if ( $realpack and ($] >= 5.009005 ? re::is_regexp($val) : $realpack eq 'Regexp') ) {
Packit dfacfc
        $is_regex = 1;
Packit dfacfc
        $no_bless = $realpack eq 'Regexp';
Packit dfacfc
    }
Packit dfacfc
Packit dfacfc
    # If purity is not set and maxdepth is set, then check depth:
Packit dfacfc
    # if we have reached maximum depth, return the string
Packit dfacfc
    # representation of the thing we are currently examining
Packit dfacfc
    # at this depth (i.e., 'Foo=ARRAY(0xdeadbeef)').
Packit dfacfc
    if (!$s->{purity}
Packit dfacfc
      and defined($s->{maxdepth})
Packit dfacfc
      and $s->{maxdepth} > 0
Packit dfacfc
      and $s->{level} >= $s->{maxdepth})
Packit dfacfc
    {
Packit dfacfc
      return qq['$val'];
Packit dfacfc
    }
Packit dfacfc
Packit dfacfc
    # avoid recursing infinitely [perl #122111]
Packit dfacfc
    if ($s->{maxrecurse} > 0
Packit dfacfc
        and $s->{level} >= $s->{maxrecurse}) {
Packit dfacfc
        die "Recursion limit of $s->{maxrecurse} exceeded";
Packit dfacfc
    }
Packit dfacfc
Packit dfacfc
    # we have a blessed ref
Packit dfacfc
    my ($blesspad);
Packit dfacfc
    if ($realpack and !$no_bless) {
Packit dfacfc
      $out = $s->{'bless'} . '( ';
Packit dfacfc
      $blesspad = $s->{apad};
Packit dfacfc
      $s->{apad} .= '       ' if ($s->{indent} >= 2);
Packit dfacfc
    }
Packit dfacfc
Packit dfacfc
    $s->{level}++;
Packit dfacfc
    my $ipad = $s->{xpad} x $s->{level};
Packit dfacfc
Packit dfacfc
    if ($is_regex) {
Packit dfacfc
        my $pat;
Packit dfacfc
        my $flags = "";
Packit dfacfc
        if (defined(*re::regexp_pattern{CODE})) {
Packit dfacfc
          ($pat, $flags) = re::regexp_pattern($val);
Packit dfacfc
        }
Packit dfacfc
        else {
Packit dfacfc
          $pat = "$val";
Packit dfacfc
        }
Packit dfacfc
        $pat =~ s <(\\.)|/> { $1 || '\\/' }ge;
Packit dfacfc
        $out .= "qr/$pat/$flags";
Packit dfacfc
    }
Packit dfacfc
    elsif ($realtype eq 'SCALAR' || $realtype eq 'REF'
Packit dfacfc
    || $realtype eq 'VSTRING') {
Packit dfacfc
      if ($realpack) {
Packit dfacfc
        $out .= 'do{\\(my $o = ' . $s->_dump($$val, "\${$name}") . ')}';
Packit dfacfc
      }
Packit dfacfc
      else {
Packit dfacfc
        $out .= '\\' . $s->_dump($$val, "\${$name}");
Packit dfacfc
      }
Packit dfacfc
    }
Packit dfacfc
    elsif ($realtype eq 'GLOB') {
Packit dfacfc
      $out .= '\\' . $s->_dump($$val, "*{$name}");
Packit dfacfc
    }
Packit dfacfc
    elsif ($realtype eq 'ARRAY') {
Packit dfacfc
      my($pad, $mname);
Packit dfacfc
      my($i) = 0;
Packit dfacfc
      $out .= ($name =~ /^\@/) ? '(' : '[';
Packit dfacfc
      $pad = $s->{sep} . $s->{pad} . $s->{apad};
Packit dfacfc
      ($name =~ /^\@(.*)$/) ? ($mname = "\$" . $1) :
Packit dfacfc
    # omit -> if $foo->[0]->{bar}, but not ${$foo->[0]}->{bar}
Packit dfacfc
        ($name =~ /^\\?[\%\@\*\$][^{].*[]}]$/) ? ($mname = $name) :
Packit dfacfc
        ($mname = $name . '->');
Packit dfacfc
      $mname .= '->' if $mname =~ /^\*.+\{[A-Z]+\}$/;
Packit dfacfc
      for my $v (@$val) {
Packit dfacfc
        $sname = $mname . '[' . $i . ']';
Packit dfacfc
        $out .= $pad . $ipad . '#' . $i
Packit dfacfc
          if $s->{indent} >= 3;
Packit dfacfc
        $out .= $pad . $ipad . $s->_dump($v, $sname);
Packit dfacfc
        $out .= ","
Packit dfacfc
            if $i++ < $#$val
Packit dfacfc
            || ($s->{trailingcomma} && $s->{indent} >= 1);
Packit dfacfc
      }
Packit dfacfc
      $out .= $pad . ($s->{xpad} x ($s->{level} - 1)) if $i;
Packit dfacfc
      $out .= ($name =~ /^\@/) ? ')' : ']';
Packit dfacfc
    }
Packit dfacfc
    elsif ($realtype eq 'HASH') {
Packit dfacfc
      my ($k, $v, $pad, $lpad, $mname, $pair);
Packit dfacfc
      $out .= ($name =~ /^\%/) ? '(' : '{';
Packit dfacfc
      $pad = $s->{sep} . $s->{pad} . $s->{apad};
Packit dfacfc
      $lpad = $s->{apad};
Packit dfacfc
      $pair = $s->{pair};
Packit dfacfc
      ($name =~ /^\%(.*)$/) ? ($mname = "\$" . $1) :
Packit dfacfc
    # omit -> if $foo->[0]->{bar}, but not ${$foo->[0]}->{bar}
Packit dfacfc
        ($name =~ /^\\?[\%\@\*\$][^{].*[]}]$/) ? ($mname = $name) :
Packit dfacfc
        ($mname = $name . '->');
Packit dfacfc
      $mname .= '->' if $mname =~ /^\*.+\{[A-Z]+\}$/;
Packit dfacfc
      my $sortkeys = defined($s->{sortkeys}) ? $s->{sortkeys} : '';
Packit dfacfc
      my $keys = [];
Packit dfacfc
      if ($sortkeys) {
Packit dfacfc
        if (ref($s->{sortkeys}) eq 'CODE') {
Packit dfacfc
          $keys = $s->{sortkeys}($val);
Packit dfacfc
          unless (ref($keys) eq 'ARRAY') {
Packit dfacfc
            carp "Sortkeys subroutine did not return ARRAYREF";
Packit dfacfc
            $keys = [];
Packit dfacfc
          }
Packit dfacfc
        }
Packit dfacfc
        else {
Packit dfacfc
          $keys = [ sort keys %$val ];
Packit dfacfc
        }
Packit dfacfc
      }
Packit dfacfc
Packit dfacfc
      # Ensure hash iterator is reset
Packit dfacfc
      keys(%$val);
Packit dfacfc
Packit dfacfc
      my $key;
Packit dfacfc
      while (($k, $v) = ! $sortkeys ? (each %$val) :
Packit dfacfc
         @$keys ? ($key = shift(@$keys), $val->{$key}) :
Packit dfacfc
         () )
Packit dfacfc
      {
Packit dfacfc
        my $nk = $s->_dump($k, "");
Packit dfacfc
Packit dfacfc
        # _dump doesn't quote numbers of this form
Packit dfacfc
        if ($s->{quotekeys} && $nk =~ /^(?:0|-?[1-9][0-9]{0,8})\z/) {
Packit dfacfc
          $nk = $s->{useqq} ? qq("$nk") : qq('$nk');
Packit dfacfc
        }
Packit dfacfc
        elsif (!$s->{quotekeys} and $nk =~ /^[\"\']([A-Za-z_]\w*)[\"\']$/) {
Packit dfacfc
          $nk = $1
Packit dfacfc
        }
Packit dfacfc
Packit dfacfc
        $sname = $mname . '{' . $nk . '}';
Packit dfacfc
        $out .= $pad . $ipad . $nk . $pair;
Packit dfacfc
Packit dfacfc
        # temporarily alter apad
Packit dfacfc
        $s->{apad} .= (" " x (length($nk) + 4))
Packit dfacfc
          if $s->{indent} >= 2;
Packit dfacfc
        $out .= $s->_dump($val->{$k}, $sname) . ",";
Packit dfacfc
        $s->{apad} = $lpad
Packit dfacfc
          if $s->{indent} >= 2;
Packit dfacfc
      }
Packit dfacfc
      if (substr($out, -1) eq ',') {
Packit dfacfc
        chop $out if !$s->{trailingcomma} || !$s->{indent};
Packit dfacfc
        $out .= $pad . ($s->{xpad} x ($s->{level} - 1));
Packit dfacfc
      }
Packit dfacfc
      $out .= ($name =~ /^\%/) ? ')' : '}';
Packit dfacfc
    }
Packit dfacfc
    elsif ($realtype eq 'CODE') {
Packit dfacfc
      if ($s->{deparse}) {
Packit dfacfc
        require B::Deparse;
Packit dfacfc
        my $sub =  'sub ' . (B::Deparse->new)->coderef2text($val);
Packit dfacfc
        $pad    =  $s->{sep} . $s->{pad} . $s->{apad} . $s->{xpad} x ($s->{level} - 1);
Packit dfacfc
        $sub    =~ s/\n/$pad/gse;
Packit dfacfc
        $out   .=  $sub;
Packit dfacfc
      }
Packit dfacfc
      else {
Packit dfacfc
        $out .= 'sub { "DUMMY" }';
Packit dfacfc
        carp "Encountered CODE ref, using dummy placeholder" if $s->{purity};
Packit dfacfc
      }
Packit dfacfc
    }
Packit dfacfc
    else {
Packit dfacfc
      croak "Can't handle '$realtype' type";
Packit dfacfc
    }
Packit dfacfc
Packit dfacfc
    if ($realpack and !$no_bless) { # we have a blessed ref
Packit dfacfc
      $out .= ', ' . _quote($realpack) . ' )';
Packit dfacfc
      $out .= '->' . $s->{toaster} . '()'
Packit dfacfc
        if $s->{toaster} ne '';
Packit dfacfc
      $s->{apad} = $blesspad;
Packit dfacfc
    }
Packit dfacfc
    $s->{level}--;
Packit dfacfc
  }
Packit dfacfc
  else {                                 # simple scalar
Packit dfacfc
Packit dfacfc
    my $ref = \$_[1];
Packit dfacfc
    my $v;
Packit dfacfc
    # first, catalog the scalar
Packit dfacfc
    if ($name ne '') {
Packit dfacfc
      $id = format_refaddr($ref);
Packit dfacfc
      if (exists $s->{seen}{$id}) {
Packit dfacfc
        if ($s->{seen}{$id}[2]) {
Packit dfacfc
          $out = $s->{seen}{$id}[0];
Packit dfacfc
          #warn "[<$out]\n";
Packit dfacfc
          return "\${$out}";
Packit dfacfc
        }
Packit dfacfc
      }
Packit dfacfc
      else {
Packit dfacfc
        #warn "[>\\$name]\n";
Packit dfacfc
        $s->{seen}{$id} = ["\\$name", $ref];
Packit dfacfc
      }
Packit dfacfc
    }
Packit dfacfc
    $ref = \$val;
Packit dfacfc
    if (ref($ref) eq 'GLOB') {  # glob
Packit dfacfc
      my $name = substr($val, 1);
Packit dfacfc
      if ($name =~ /^[A-Za-z_][\w:]*$/ && $name ne 'main::') {
Packit dfacfc
        $name =~ s/^main::/::/;
Packit dfacfc
        $sname = $name;
Packit dfacfc
      }
Packit dfacfc
      else {
Packit dfacfc
        $sname = $s->_dump(
Packit dfacfc
          $name eq 'main::' || $] < 5.007 && $name eq "main::\0"
Packit dfacfc
            ? ''
Packit dfacfc
            : $name,
Packit dfacfc
          "",
Packit dfacfc
        );
Packit dfacfc
        $sname = '{' . $sname . '}';
Packit dfacfc
      }
Packit dfacfc
      if ($s->{purity}) {
Packit dfacfc
        my $k;
Packit dfacfc
        local ($s->{level}) = 0;
Packit dfacfc
        for $k (qw(SCALAR ARRAY HASH)) {
Packit dfacfc
          my $gval = *$val{$k};
Packit dfacfc
          next unless defined $gval;
Packit dfacfc
          next if $k eq "SCALAR" && ! defined $$gval;  # always there
Packit dfacfc
Packit dfacfc
          # _dump can push into @post, so we hold our place using $postlen
Packit dfacfc
          my $postlen = scalar @post;
Packit dfacfc
          $post[$postlen] = "\*$sname = ";
Packit dfacfc
          local ($s->{apad}) = " " x length($post[$postlen]) if $s->{indent} >= 2;
Packit dfacfc
          $post[$postlen] .= $s->_dump($gval, "\*$sname\{$k\}");
Packit dfacfc
        }
Packit dfacfc
      }
Packit dfacfc
      $out .= '*' . $sname;
Packit dfacfc
    }
Packit dfacfc
    elsif (!defined($val)) {
Packit dfacfc
      $out .= "undef";
Packit dfacfc
    }
Packit dfacfc
    elsif (defined &_vstring and $v = _vstring($val)
Packit dfacfc
      and !_bad_vsmg || eval $v eq $val) {
Packit dfacfc
      $out .= $v;
Packit dfacfc
    }
Packit dfacfc
    elsif (!defined &_vstring
Packit dfacfc
       and ref $ref eq 'VSTRING' || eval{Scalar::Util::isvstring($val)}) {
Packit dfacfc
      $out .= sprintf "%vd", $val;
Packit dfacfc
    }
Packit dfacfc
    # \d here would treat "1\x{660}" as a safe decimal number
Packit dfacfc
    elsif ($val =~ /^(?:0|-?[1-9][0-9]{0,8})\z/) { # safe decimal number
Packit dfacfc
      $out .= $val;
Packit dfacfc
    }
Packit dfacfc
    else {                 # string
Packit dfacfc
      if ($s->{useqq} or $val =~ tr/\0-\377//c) {
Packit dfacfc
        # Fall back to qq if there's Unicode
Packit dfacfc
        $out .= qquote($val, $s->{useqq});
Packit dfacfc
      }
Packit dfacfc
      else {
Packit dfacfc
        $out .= _quote($val);
Packit dfacfc
      }
Packit dfacfc
    }
Packit dfacfc
  }
Packit dfacfc
  if ($id) {
Packit dfacfc
    # if we made it this far, $id was added to seen list at current
Packit dfacfc
    # level, so remove it to get deep copies
Packit dfacfc
    if ($s->{deepcopy}) {
Packit dfacfc
      delete($s->{seen}{$id});
Packit dfacfc
    }
Packit dfacfc
    elsif ($name) {
Packit dfacfc
      $s->{seen}{$id}[2] = 1;
Packit dfacfc
    }
Packit dfacfc
  }
Packit dfacfc
  return $out;
Packit dfacfc
}
Packit dfacfc
Packit dfacfc
#
Packit dfacfc
# non-OO style of earlier version
Packit dfacfc
#
Packit dfacfc
sub Dumper {
Packit dfacfc
  return Data::Dumper->Dump([@_]);
Packit dfacfc
}
Packit dfacfc
Packit dfacfc
# compat stub
Packit dfacfc
sub DumperX {
Packit dfacfc
  return Data::Dumper->Dumpxs([@_], []);
Packit dfacfc
}
Packit dfacfc
Packit dfacfc
#
Packit dfacfc
# reset the "seen" cache
Packit dfacfc
#
Packit dfacfc
sub Reset {
Packit dfacfc
  my($s) = shift;
Packit dfacfc
  $s->{seen} = {};
Packit dfacfc
  return $s;
Packit dfacfc
}
Packit dfacfc
Packit dfacfc
sub Indent {
Packit dfacfc
  my($s, $v) = @_;
Packit dfacfc
  if (defined($v)) {
Packit dfacfc
    if ($v == 0) {
Packit dfacfc
      $s->{xpad} = "";
Packit dfacfc
      $s->{sep} = "";
Packit dfacfc
    }
Packit dfacfc
    else {
Packit dfacfc
      $s->{xpad} = "  ";
Packit dfacfc
      $s->{sep} = "\n";
Packit dfacfc
    }
Packit dfacfc
    $s->{indent} = $v;
Packit dfacfc
    return $s;
Packit dfacfc
  }
Packit dfacfc
  else {
Packit dfacfc
    return $s->{indent};
Packit dfacfc
  }
Packit dfacfc
}
Packit dfacfc
Packit dfacfc
sub Trailingcomma {
Packit dfacfc
  my($s, $v) = @_;
Packit dfacfc
  defined($v) ? (($s->{trailingcomma} = $v), return $s) : $s->{trailingcomma};
Packit dfacfc
}
Packit dfacfc
Packit dfacfc
sub Pair {
Packit dfacfc
    my($s, $v) = @_;
Packit dfacfc
    defined($v) ? (($s->{pair} = $v), return $s) : $s->{pair};
Packit dfacfc
}
Packit dfacfc
Packit dfacfc
sub Pad {
Packit dfacfc
  my($s, $v) = @_;
Packit dfacfc
  defined($v) ? (($s->{pad} = $v), return $s) : $s->{pad};
Packit dfacfc
}
Packit dfacfc
Packit dfacfc
sub Varname {
Packit dfacfc
  my($s, $v) = @_;
Packit dfacfc
  defined($v) ? (($s->{varname} = $v), return $s) : $s->{varname};
Packit dfacfc
}
Packit dfacfc
Packit dfacfc
sub Purity {
Packit dfacfc
  my($s, $v) = @_;
Packit dfacfc
  defined($v) ? (($s->{purity} = $v), return $s) : $s->{purity};
Packit dfacfc
}
Packit dfacfc
Packit dfacfc
sub Useqq {
Packit dfacfc
  my($s, $v) = @_;
Packit dfacfc
  defined($v) ? (($s->{useqq} = $v), return $s) : $s->{useqq};
Packit dfacfc
}
Packit dfacfc
Packit dfacfc
sub Terse {
Packit dfacfc
  my($s, $v) = @_;
Packit dfacfc
  defined($v) ? (($s->{terse} = $v), return $s) : $s->{terse};
Packit dfacfc
}
Packit dfacfc
Packit dfacfc
sub Freezer {
Packit dfacfc
  my($s, $v) = @_;
Packit dfacfc
  defined($v) ? (($s->{freezer} = $v), return $s) : $s->{freezer};
Packit dfacfc
}
Packit dfacfc
Packit dfacfc
sub Toaster {
Packit dfacfc
  my($s, $v) = @_;
Packit dfacfc
  defined($v) ? (($s->{toaster} = $v), return $s) : $s->{toaster};
Packit dfacfc
}
Packit dfacfc
Packit dfacfc
sub Deepcopy {
Packit dfacfc
  my($s, $v) = @_;
Packit dfacfc
  defined($v) ? (($s->{deepcopy} = $v), return $s) : $s->{deepcopy};
Packit dfacfc
}
Packit dfacfc
Packit dfacfc
sub Quotekeys {
Packit dfacfc
  my($s, $v) = @_;
Packit dfacfc
  defined($v) ? (($s->{quotekeys} = $v), return $s) : $s->{quotekeys};
Packit dfacfc
}
Packit dfacfc
Packit dfacfc
sub Bless {
Packit dfacfc
  my($s, $v) = @_;
Packit dfacfc
  defined($v) ? (($s->{'bless'} = $v), return $s) : $s->{'bless'};
Packit dfacfc
}
Packit dfacfc
Packit dfacfc
sub Maxdepth {
Packit dfacfc
  my($s, $v) = @_;
Packit dfacfc
  defined($v) ? (($s->{'maxdepth'} = $v), return $s) : $s->{'maxdepth'};
Packit dfacfc
}
Packit dfacfc
Packit dfacfc
sub Maxrecurse {
Packit dfacfc
  my($s, $v) = @_;
Packit dfacfc
  defined($v) ? (($s->{'maxrecurse'} = $v), return $s) : $s->{'maxrecurse'};
Packit dfacfc
}
Packit dfacfc
Packit dfacfc
sub Useperl {
Packit dfacfc
  my($s, $v) = @_;
Packit dfacfc
  defined($v) ? (($s->{'useperl'} = $v), return $s) : $s->{'useperl'};
Packit dfacfc
}
Packit dfacfc
Packit dfacfc
sub Sortkeys {
Packit dfacfc
  my($s, $v) = @_;
Packit dfacfc
  defined($v) ? (($s->{'sortkeys'} = $v), return $s) : $s->{'sortkeys'};
Packit dfacfc
}
Packit dfacfc
Packit dfacfc
sub Deparse {
Packit dfacfc
  my($s, $v) = @_;
Packit dfacfc
  defined($v) ? (($s->{'deparse'} = $v), return $s) : $s->{'deparse'};
Packit dfacfc
}
Packit dfacfc
Packit dfacfc
sub Sparseseen {
Packit dfacfc
  my($s, $v) = @_;
Packit dfacfc
  defined($v) ? (($s->{'noseen'} = $v), return $s) : $s->{'noseen'};
Packit dfacfc
}
Packit dfacfc
Packit dfacfc
# used by qquote below
Packit dfacfc
my %esc = (
Packit dfacfc
    "\a" => "\\a",
Packit dfacfc
    "\b" => "\\b",
Packit dfacfc
    "\t" => "\\t",
Packit dfacfc
    "\n" => "\\n",
Packit dfacfc
    "\f" => "\\f",
Packit dfacfc
    "\r" => "\\r",
Packit dfacfc
    "\e" => "\\e",
Packit dfacfc
);
Packit dfacfc
Packit dfacfc
my $low_controls = ($IS_ASCII)
Packit dfacfc
Packit dfacfc
                   # This includes \177, because traditionally it has been
Packit dfacfc
                   # output as octal, even though it isn't really a "low"
Packit dfacfc
                   # control
Packit dfacfc
                   ? qr/[\0-\x1f\177]/
Packit dfacfc
Packit dfacfc
                     # EBCDIC low controls.
Packit dfacfc
                   : qr/[\0-\x3f]/;
Packit dfacfc
Packit dfacfc
# put a string value in double quotes
Packit dfacfc
sub qquote {
Packit dfacfc
  local($_) = shift;
Packit dfacfc
  s/([\\\"\@\$])/\\$1/g;
Packit dfacfc
Packit dfacfc
  # This efficiently changes the high ordinal characters to \x{} if the utf8
Packit dfacfc
  # flag is on.  On ASCII platforms, the high ordinals are all the
Packit dfacfc
  # non-ASCII's.  On EBCDIC platforms, we don't include in these the non-ASCII
Packit dfacfc
  # controls whose ordinals are less than SPACE, excluded below by the range
Packit dfacfc
  # \0-\x3f.  On ASCII platforms this range just compiles as part of :ascii:.
Packit dfacfc
  # On EBCDIC platforms, there is just one outlier high ordinal control, and
Packit dfacfc
  # it gets output as \x{}.
Packit dfacfc
  my $bytes; { use bytes; $bytes = length }
Packit dfacfc
  s/([^[:ascii:]\0-\x3f])/sprintf("\\x{%x}",ord($1))/ge
Packit dfacfc
    if $bytes > length
Packit dfacfc
Packit dfacfc
       # The above doesn't get the EBCDIC outlier high ordinal control when
Packit dfacfc
       # the string is UTF-8 but there are no UTF-8 variant characters in it.
Packit dfacfc
       # We want that to come out as \x{} anyway.  We need is_utf8() to do
Packit dfacfc
       # this.
Packit dfacfc
       || (! $IS_ASCII && $] ge 5.008_001 && utf8::is_utf8($_));
Packit dfacfc
Packit dfacfc
  return qq("$_") unless /[[:^print:]]/;  # fast exit if only printables
Packit dfacfc
Packit dfacfc
  # Here, there is at least one non-printable to output.  First, translate the
Packit dfacfc
  # escapes.
Packit dfacfc
  s/([\a\b\t\n\f\r\e])/$esc{$1}/g;
Packit dfacfc
Packit dfacfc
  # no need for 3 digits in escape for octals not followed by a digit.
Packit dfacfc
  s/($low_controls)(?!\d)/'\\'.sprintf('%o',ord($1))/eg;
Packit dfacfc
Packit dfacfc
  # But otherwise use 3 digits
Packit dfacfc
  s/($low_controls)/'\\'.sprintf('%03o',ord($1))/eg;
Packit dfacfc
Packit dfacfc
    # all but last branch below not supported --BEHAVIOR SUBJECT TO CHANGE--
Packit dfacfc
  my $high = shift || "";
Packit dfacfc
    if ($high eq "iso8859") {   # Doesn't escape the Latin1 printables
Packit dfacfc
      if ($IS_ASCII) {
Packit dfacfc
        s/([\200-\240])/'\\'.sprintf('%o',ord($1))/eg;
Packit dfacfc
      }
Packit dfacfc
      elsif ($] ge 5.007_003) {
Packit dfacfc
        my $high_control = utf8::unicode_to_native(0x9F);
Packit dfacfc
        s/$high_control/sprintf('\\%o',ord($1))/eg;
Packit dfacfc
      }
Packit dfacfc
    } elsif ($high eq "utf8") {
Packit dfacfc
#     Some discussion of what to do here is in
Packit dfacfc
#       https://rt.perl.org/Ticket/Display.html?id=113088
Packit dfacfc
#     use utf8;
Packit dfacfc
#     $str =~ s/([^\040-\176])/sprintf "\\x{%04x}", ord($1)/ge;
Packit dfacfc
    } elsif ($high eq "8bit") {
Packit dfacfc
        # leave it as it is
Packit dfacfc
    } else {
Packit dfacfc
      s/([[:^ascii:]])/'\\'.sprintf('%03o',ord($1))/eg;
Packit dfacfc
      #s/([^\040-\176])/sprintf "\\x{%04x}", ord($1)/ge;
Packit dfacfc
    }
Packit dfacfc
Packit dfacfc
  return qq("$_");
Packit dfacfc
}
Packit dfacfc
Packit dfacfc
# helper sub to sort hash keys in Perl < 5.8.0 where we don't have
Packit dfacfc
# access to sortsv() from XS
Packit dfacfc
sub _sortkeys { [ sort keys %{$_[0]} ] }
Packit dfacfc
Packit dfacfc
sub _refine_name {
Packit dfacfc
    my $s = shift;
Packit dfacfc
    my ($name, $val, $i) = @_;
Packit dfacfc
    if (defined $name) {
Packit dfacfc
      if ($name =~ /^[*](.*)$/) {
Packit dfacfc
        if (defined $val) {
Packit dfacfc
            $name = (ref $val eq 'ARRAY') ? ( "\@" . $1 ) :
Packit dfacfc
              (ref $val eq 'HASH')  ? ( "\%" . $1 ) :
Packit dfacfc
              (ref $val eq 'CODE')  ? ( "\*" . $1 ) :
Packit dfacfc
              ( "\$" . $1 ) ;
Packit dfacfc
        }
Packit dfacfc
        else {
Packit dfacfc
          $name = "\$" . $1;
Packit dfacfc
        }
Packit dfacfc
      }
Packit dfacfc
      elsif ($name !~ /^\$/) {
Packit dfacfc
        $name = "\$" . $name;
Packit dfacfc
      }
Packit dfacfc
    }
Packit dfacfc
    else { # no names provided
Packit dfacfc
      $name = "\$" . $s->{varname} . $i;
Packit dfacfc
    }
Packit dfacfc
    return $name;
Packit dfacfc
}
Packit dfacfc
Packit dfacfc
sub _compose_out {
Packit dfacfc
    my $s = shift;
Packit dfacfc
    my ($valstr, $postref) = @_;
Packit dfacfc
    my $out = "";
Packit dfacfc
    $out .= $s->{pad} . $valstr . $s->{sep};
Packit dfacfc
    if (@{$postref}) {
Packit dfacfc
        $out .= $s->{pad} .
Packit dfacfc
            join(';' . $s->{sep} . $s->{pad}, @{$postref}) .
Packit dfacfc
            ';' .
Packit dfacfc
            $s->{sep};
Packit dfacfc
    }
Packit dfacfc
    return $out;
Packit dfacfc
}
Packit dfacfc
Packit dfacfc
1;
Packit dfacfc
__END__
Packit dfacfc
Packit dfacfc
=head1 NAME
Packit dfacfc
Packit dfacfc
Data::Dumper - stringified perl data structures, suitable for both printing and C<eval>
Packit dfacfc
Packit dfacfc
=head1 SYNOPSIS
Packit dfacfc
Packit dfacfc
    use Data::Dumper;
Packit dfacfc
Packit dfacfc
    # simple procedural interface
Packit dfacfc
    print Dumper($foo, $bar);
Packit dfacfc
Packit dfacfc
    # extended usage with names
Packit dfacfc
    print Data::Dumper->Dump([$foo, $bar], [qw(foo *ary)]);
Packit dfacfc
Packit dfacfc
    # configuration variables
Packit dfacfc
    {
Packit dfacfc
      local $Data::Dumper::Purity = 1;
Packit dfacfc
      eval Data::Dumper->Dump([$foo, $bar], [qw(foo *ary)]);
Packit dfacfc
    }
Packit dfacfc
Packit dfacfc
    # OO usage
Packit dfacfc
    $d = Data::Dumper->new([$foo, $bar], [qw(foo *ary)]);
Packit dfacfc
       ...
Packit dfacfc
    print $d->Dump;
Packit dfacfc
       ...
Packit dfacfc
    $d->Purity(1)->Terse(1)->Deepcopy(1);
Packit dfacfc
    eval $d->Dump;
Packit dfacfc
Packit dfacfc
Packit dfacfc
=head1 DESCRIPTION
Packit dfacfc
Packit dfacfc
Given a list of scalars or reference variables, writes out their contents in
Packit dfacfc
perl syntax. The references can also be objects.  The content of each
Packit dfacfc
variable is output in a single Perl statement.  Handles self-referential
Packit dfacfc
structures correctly.
Packit dfacfc
Packit dfacfc
The return value can be C<eval>ed to get back an identical copy of the
Packit dfacfc
original reference structure.  (Please do consider the security implications
Packit dfacfc
of eval'ing code from untrusted sources!)
Packit dfacfc
Packit dfacfc
Any references that are the same as one of those passed in will be named
Packit dfacfc
C<$VAR>I<n> (where I<n> is a numeric suffix), and other duplicate references
Packit dfacfc
to substructures within C<$VAR>I<n> will be appropriately labeled using arrow
Packit dfacfc
notation.  You can specify names for individual values to be dumped if you
Packit dfacfc
use the C<Dump()> method, or you can change the default C<$VAR> prefix to
Packit dfacfc
something else.  See C<$Data::Dumper::Varname> and C<$Data::Dumper::Terse>
Packit dfacfc
below.
Packit dfacfc
Packit dfacfc
The default output of self-referential structures can be C<eval>ed, but the
Packit dfacfc
nested references to C<$VAR>I<n> will be undefined, since a recursive
Packit dfacfc
structure cannot be constructed using one Perl statement.  You should set the
Packit dfacfc
C<Purity> flag to 1 to get additional statements that will correctly fill in
Packit dfacfc
these references.  Moreover, if C<eval>ed when strictures are in effect,
Packit dfacfc
you need to ensure that any variables it accesses are previously declared.
Packit dfacfc
Packit dfacfc
In the extended usage form, the references to be dumped can be given
Packit dfacfc
user-specified names.  If a name begins with a C<*>, the output will
Packit dfacfc
describe the dereferenced type of the supplied reference for hashes and
Packit dfacfc
arrays, and coderefs.  Output of names will be avoided where possible if
Packit dfacfc
the C<Terse> flag is set.
Packit dfacfc
Packit dfacfc
In many cases, methods that are used to set the internal state of the
Packit dfacfc
object will return the object itself, so method calls can be conveniently
Packit dfacfc
chained together.
Packit dfacfc
Packit dfacfc
Several styles of output are possible, all controlled by setting
Packit dfacfc
the C<Indent> flag.  See L<Configuration Variables or Methods> below
Packit dfacfc
for details.
Packit dfacfc
Packit dfacfc
Packit dfacfc
=head2 Methods
Packit dfacfc
Packit dfacfc
=over 4
Packit dfacfc
Packit dfacfc
=item I<PACKAGE>->new(I<ARRAYREF [>, I<ARRAYREF]>)
Packit dfacfc
Packit dfacfc
Returns a newly created C<Data::Dumper> object.  The first argument is an
Packit dfacfc
anonymous array of values to be dumped.  The optional second argument is an
Packit dfacfc
anonymous array of names for the values.  The names need not have a leading
Packit dfacfc
C<$> sign, and must be comprised of alphanumeric characters.  You can begin
Packit dfacfc
a name with a C<*> to specify that the dereferenced type must be dumped
Packit dfacfc
instead of the reference itself, for ARRAY and HASH references.
Packit dfacfc
Packit dfacfc
The prefix specified by C<$Data::Dumper::Varname> will be used with a
Packit dfacfc
numeric suffix if the name for a value is undefined.
Packit dfacfc
Packit dfacfc
Data::Dumper will catalog all references encountered while dumping the
Packit dfacfc
values. Cross-references (in the form of names of substructures in perl
Packit dfacfc
syntax) will be inserted at all possible points, preserving any structural
Packit dfacfc
interdependencies in the original set of values.  Structure traversal is
Packit dfacfc
depth-first,  and proceeds in order from the first supplied value to
Packit dfacfc
the last.
Packit dfacfc
Packit dfacfc
=item I<$OBJ>->Dump  I<or>  I<PACKAGE>->Dump(I<ARRAYREF [>, I<ARRAYREF]>)
Packit dfacfc
Packit dfacfc
Returns the stringified form of the values stored in the object (preserving
Packit dfacfc
the order in which they were supplied to C<new>), subject to the
Packit dfacfc
configuration options below.  In a list context, it returns a list
Packit dfacfc
of strings corresponding to the supplied values.
Packit dfacfc
Packit dfacfc
The second form, for convenience, simply calls the C<new> method on its
Packit dfacfc
arguments before dumping the object immediately.
Packit dfacfc
Packit dfacfc
=item I<$OBJ>->Seen(I<[HASHREF]>)
Packit dfacfc
Packit dfacfc
Queries or adds to the internal table of already encountered references.
Packit dfacfc
You must use C<Reset> to explicitly clear the table if needed.  Such
Packit dfacfc
references are not dumped; instead, their names are inserted wherever they
Packit dfacfc
are encountered subsequently.  This is useful especially for properly
Packit dfacfc
dumping subroutine references.
Packit dfacfc
Packit dfacfc
Expects an anonymous hash of name => value pairs.  Same rules apply for names
Packit dfacfc
as in C<new>.  If no argument is supplied, will return the "seen" list of
Packit dfacfc
name => value pairs, in a list context.  Otherwise, returns the object
Packit dfacfc
itself.
Packit dfacfc
Packit dfacfc
=item I<$OBJ>->Values(I<[ARRAYREF]>)
Packit dfacfc
Packit dfacfc
Queries or replaces the internal array of values that will be dumped.  When
Packit dfacfc
called without arguments, returns the values as a list.  When called with a
Packit dfacfc
reference to an array of replacement values, returns the object itself.  When
Packit dfacfc
called with any other type of argument, dies.
Packit dfacfc
Packit dfacfc
=item I<$OBJ>->Names(I<[ARRAYREF]>)
Packit dfacfc
Packit dfacfc
Queries or replaces the internal array of user supplied names for the values
Packit dfacfc
that will be dumped.  When called without arguments, returns the names.  When
Packit dfacfc
called with an array of replacement names, returns the object itself.  If the
Packit dfacfc
number of replacement names exceeds the number of values to be named, the
Packit dfacfc
excess names will not be used.  If the number of replacement names falls short
Packit dfacfc
of the number of values to be named, the list of replacement names will be
Packit dfacfc
exhausted and remaining values will not be renamed.  When
Packit dfacfc
called with any other type of argument, dies.
Packit dfacfc
Packit dfacfc
=item I<$OBJ>->Reset
Packit dfacfc
Packit dfacfc
Clears the internal table of "seen" references and returns the object
Packit dfacfc
itself.
Packit dfacfc
Packit dfacfc
=back
Packit dfacfc
Packit dfacfc
=head2 Functions
Packit dfacfc
Packit dfacfc
=over 4
Packit dfacfc
Packit dfacfc
=item Dumper(I<LIST>)
Packit dfacfc
Packit dfacfc
Returns the stringified form of the values in the list, subject to the
Packit dfacfc
configuration options below.  The values will be named C<$VAR>I<n> in the
Packit dfacfc
output, where I<n> is a numeric suffix.  Will return a list of strings
Packit dfacfc
in a list context.
Packit dfacfc
Packit dfacfc
=back
Packit dfacfc
Packit dfacfc
=head2 Configuration Variables or Methods
Packit dfacfc
Packit dfacfc
Several configuration variables can be used to control the kind of output
Packit dfacfc
generated when using the procedural interface.  These variables are usually
Packit dfacfc
C<local>ized in a block so that other parts of the code are not affected by
Packit dfacfc
the change.
Packit dfacfc
Packit dfacfc
These variables determine the default state of the object created by calling
Packit dfacfc
the C<new> method, but cannot be used to alter the state of the object
Packit dfacfc
thereafter.  The equivalent method names should be used instead to query
Packit dfacfc
or set the internal state of the object.
Packit dfacfc
Packit dfacfc
The method forms return the object itself when called with arguments,
Packit dfacfc
so that they can be chained together nicely.
Packit dfacfc
Packit dfacfc
=over 4
Packit dfacfc
Packit dfacfc
=item *
Packit dfacfc
Packit dfacfc
$Data::Dumper::Indent  I<or>  I<$OBJ>->Indent(I<[NEWVAL]>)
Packit dfacfc
Packit dfacfc
Controls the style of indentation.  It can be set to 0, 1, 2 or 3.  Style 0
Packit dfacfc
spews output without any newlines, indentation, or spaces between list
Packit dfacfc
items.  It is the most compact format possible that can still be called
Packit dfacfc
valid perl.  Style 1 outputs a readable form with newlines but no fancy
Packit dfacfc
indentation (each level in the structure is simply indented by a fixed
Packit dfacfc
amount of whitespace).  Style 2 (the default) outputs a very readable form
Packit dfacfc
which takes into account the length of hash keys (so the hash value lines
Packit dfacfc
up).  Style 3 is like style 2, but also annotates the elements of arrays
Packit dfacfc
with their index (but the comment is on its own line, so array output
Packit dfacfc
consumes twice the number of lines).  Style 2 is the default.
Packit dfacfc
Packit dfacfc
=item *
Packit dfacfc
Packit dfacfc
$Data::Dumper::Trailingcomma  I<or>  I<$OBJ>->Trailingcomma(I<[NEWVAL]>)
Packit dfacfc
Packit dfacfc
Controls whether a comma is added after the last element of an array or
Packit dfacfc
hash. Even when true, no comma is added between the last element of an array
Packit dfacfc
or hash and a closing bracket when they appear on the same line. The default
Packit dfacfc
is false.
Packit dfacfc
Packit dfacfc
=item *
Packit dfacfc
Packit dfacfc
$Data::Dumper::Purity  I<or>  I<$OBJ>->Purity(I<[NEWVAL]>)
Packit dfacfc
Packit dfacfc
Controls the degree to which the output can be C<eval>ed to recreate the
Packit dfacfc
supplied reference structures.  Setting it to 1 will output additional perl
Packit dfacfc
statements that will correctly recreate nested references.  The default is
Packit dfacfc
0.
Packit dfacfc
Packit dfacfc
=item *
Packit dfacfc
Packit dfacfc
$Data::Dumper::Pad  I<or>  I<$OBJ>->Pad(I<[NEWVAL]>)
Packit dfacfc
Packit dfacfc
Specifies the string that will be prefixed to every line of the output.
Packit dfacfc
Empty string by default.
Packit dfacfc
Packit dfacfc
=item *
Packit dfacfc
Packit dfacfc
$Data::Dumper::Varname  I<or>  I<$OBJ>->Varname(I<[NEWVAL]>)
Packit dfacfc
Packit dfacfc
Contains the prefix to use for tagging variable names in the output. The
Packit dfacfc
default is "VAR".
Packit dfacfc
Packit dfacfc
=item *
Packit dfacfc
Packit dfacfc
$Data::Dumper::Useqq  I<or>  I<$OBJ>->Useqq(I<[NEWVAL]>)
Packit dfacfc
Packit dfacfc
When set, enables the use of double quotes for representing string values.
Packit dfacfc
Whitespace other than space will be represented as C<[\n\t\r]>, "unsafe"
Packit dfacfc
characters will be backslashed, and unprintable characters will be output as
Packit dfacfc
quoted octal integers.  The default is 0.
Packit dfacfc
Packit dfacfc
=item *
Packit dfacfc
Packit dfacfc
$Data::Dumper::Terse  I<or>  I<$OBJ>->Terse(I<[NEWVAL]>)
Packit dfacfc
Packit dfacfc
When set, Data::Dumper will emit single, non-self-referential values as
Packit dfacfc
atoms/terms rather than statements.  This means that the C<$VAR>I<n> names
Packit dfacfc
will be avoided where possible, but be advised that such output may not
Packit dfacfc
always be parseable by C<eval>.
Packit dfacfc
Packit dfacfc
=item *
Packit dfacfc
Packit dfacfc
$Data::Dumper::Freezer  I<or>  $I<OBJ>->Freezer(I<[NEWVAL]>)
Packit dfacfc
Packit dfacfc
Can be set to a method name, or to an empty string to disable the feature.
Packit dfacfc
Data::Dumper will invoke that method via the object before attempting to
Packit dfacfc
stringify it.  This method can alter the contents of the object (if, for
Packit dfacfc
instance, it contains data allocated from C), and even rebless it in a
Packit dfacfc
different package.  The client is responsible for making sure the specified
Packit dfacfc
method can be called via the object, and that the object ends up containing
Packit dfacfc
only perl data types after the method has been called.  Defaults to an empty
Packit dfacfc
string.
Packit dfacfc
Packit dfacfc
If an object does not support the method specified (determined using
Packit dfacfc
UNIVERSAL::can()) then the call will be skipped.  If the method dies a
Packit dfacfc
warning will be generated.
Packit dfacfc
Packit dfacfc
=item *
Packit dfacfc
Packit dfacfc
$Data::Dumper::Toaster  I<or>  $I<OBJ>->Toaster(I<[NEWVAL]>)
Packit dfacfc
Packit dfacfc
Can be set to a method name, or to an empty string to disable the feature.
Packit dfacfc
Data::Dumper will emit a method call for any objects that are to be dumped
Packit dfacfc
using the syntax C<bless(DATA, CLASS)-E<gt>METHOD()>.  Note that this means that
Packit dfacfc
the method specified will have to perform any modifications required on the
Packit dfacfc
object (like creating new state within it, and/or reblessing it in a
Packit dfacfc
different package) and then return it.  The client is responsible for making
Packit dfacfc
sure the method can be called via the object, and that it returns a valid
Packit dfacfc
object.  Defaults to an empty string.
Packit dfacfc
Packit dfacfc
=item *
Packit dfacfc
Packit dfacfc
$Data::Dumper::Deepcopy  I<or>  $I<OBJ>->Deepcopy(I<[NEWVAL]>)
Packit dfacfc
Packit dfacfc
Can be set to a boolean value to enable deep copies of structures.
Packit dfacfc
Cross-referencing will then only be done when absolutely essential
Packit dfacfc
(i.e., to break reference cycles).  Default is 0.
Packit dfacfc
Packit dfacfc
=item *
Packit dfacfc
Packit dfacfc
$Data::Dumper::Quotekeys  I<or>  $I<OBJ>->Quotekeys(I<[NEWVAL]>)
Packit dfacfc
Packit dfacfc
Can be set to a boolean value to control whether hash keys are quoted.
Packit dfacfc
A defined false value will avoid quoting hash keys when it looks like a simple
Packit dfacfc
string.  Default is 1, which will always enclose hash keys in quotes.
Packit dfacfc
Packit dfacfc
=item *
Packit dfacfc
Packit dfacfc
$Data::Dumper::Bless  I<or>  $I<OBJ>->Bless(I<[NEWVAL]>)
Packit dfacfc
Packit dfacfc
Can be set to a string that specifies an alternative to the C<bless>
Packit dfacfc
builtin operator used to create objects.  A function with the specified
Packit dfacfc
name should exist, and should accept the same arguments as the builtin.
Packit dfacfc
Default is C<bless>.
Packit dfacfc
Packit dfacfc
=item *
Packit dfacfc
Packit dfacfc
$Data::Dumper::Pair  I<or>  $I<OBJ>->Pair(I<[NEWVAL]>)
Packit dfacfc
Packit dfacfc
Can be set to a string that specifies the separator between hash keys
Packit dfacfc
and values. To dump nested hash, array and scalar values to JavaScript,
Packit dfacfc
use: C<$Data::Dumper::Pair = ' : ';>. Implementing C<bless> in JavaScript
Packit dfacfc
is left as an exercise for the reader.
Packit dfacfc
A function with the specified name exists, and accepts the same arguments
Packit dfacfc
as the builtin.
Packit dfacfc
Packit dfacfc
Default is: C< =E<gt> >.
Packit dfacfc
Packit dfacfc
=item *
Packit dfacfc
Packit dfacfc
$Data::Dumper::Maxdepth  I<or>  $I<OBJ>->Maxdepth(I<[NEWVAL]>)
Packit dfacfc
Packit dfacfc
Can be set to a positive integer that specifies the depth beyond which
Packit dfacfc
we don't venture into a structure.  Has no effect when
Packit dfacfc
C<Data::Dumper::Purity> is set.  (Useful in debugger when we often don't
Packit dfacfc
want to see more than enough).  Default is 0, which means there is
Packit dfacfc
no maximum depth.
Packit dfacfc
Packit dfacfc
=item *
Packit dfacfc
Packit dfacfc
$Data::Dumper::Maxrecurse  I<or>  $I<OBJ>->Maxrecurse(I<[NEWVAL]>)
Packit dfacfc
Packit dfacfc
Can be set to a positive integer that specifies the depth beyond which
Packit dfacfc
recursion into a structure will throw an exception.  This is intended
Packit dfacfc
as a security measure to prevent perl running out of stack space when
Packit dfacfc
dumping an excessively deep structure.  Can be set to 0 to remove the
Packit dfacfc
limit.  Default is 1000.
Packit dfacfc
Packit dfacfc
=item *
Packit dfacfc
Packit dfacfc
$Data::Dumper::Useperl  I<or>  $I<OBJ>->Useperl(I<[NEWVAL]>)
Packit dfacfc
Packit dfacfc
Can be set to a boolean value which controls whether the pure Perl
Packit dfacfc
implementation of C<Data::Dumper> is used. The C<Data::Dumper> module is
Packit dfacfc
a dual implementation, with almost all functionality written in both
Packit dfacfc
pure Perl and also in XS ('C'). Since the XS version is much faster, it
Packit dfacfc
will always be used if possible. This option lets you override the
Packit dfacfc
default behavior, usually for testing purposes only. Default is 0, which
Packit dfacfc
means the XS implementation will be used if possible.
Packit dfacfc
Packit dfacfc
=item *
Packit dfacfc
Packit dfacfc
$Data::Dumper::Sortkeys  I<or>  $I<OBJ>->Sortkeys(I<[NEWVAL]>)
Packit dfacfc
Packit dfacfc
Can be set to a boolean value to control whether hash keys are dumped in
Packit dfacfc
sorted order. A true value will cause the keys of all hashes to be
Packit dfacfc
dumped in Perl's default sort order. Can also be set to a subroutine
Packit dfacfc
reference which will be called for each hash that is dumped. In this
Packit dfacfc
case C<Data::Dumper> will call the subroutine once for each hash,
Packit dfacfc
passing it the reference of the hash. The purpose of the subroutine is
Packit dfacfc
to return a reference to an array of the keys that will be dumped, in
Packit dfacfc
the order that they should be dumped. Using this feature, you can
Packit dfacfc
control both the order of the keys, and which keys are actually used. In
Packit dfacfc
other words, this subroutine acts as a filter by which you can exclude
Packit dfacfc
certain keys from being dumped. Default is 0, which means that hash keys
Packit dfacfc
are not sorted.
Packit dfacfc
Packit dfacfc
=item *
Packit dfacfc
Packit dfacfc
$Data::Dumper::Deparse  I<or>  $I<OBJ>->Deparse(I<[NEWVAL]>)
Packit dfacfc
Packit dfacfc
Can be set to a boolean value to control whether code references are
Packit dfacfc
turned into perl source code. If set to a true value, C<B::Deparse>
Packit dfacfc
will be used to get the source of the code reference. Using this option
Packit dfacfc
will force using the Perl implementation of the dumper, since the fast
Packit dfacfc
XSUB implementation doesn't support it.
Packit dfacfc
Packit dfacfc
Caution : use this option only if you know that your coderefs will be
Packit dfacfc
properly reconstructed by C<B::Deparse>.
Packit dfacfc
Packit dfacfc
=item *
Packit dfacfc
Packit dfacfc
$Data::Dumper::Sparseseen I<or>  $I<OBJ>->Sparseseen(I<[NEWVAL]>)
Packit dfacfc
Packit dfacfc
By default, Data::Dumper builds up the "seen" hash of scalars that
Packit dfacfc
it has encountered during serialization. This is very expensive.
Packit dfacfc
This seen hash is necessary to support and even just detect circular
Packit dfacfc
references. It is exposed to the user via the C<Seen()> call both
Packit dfacfc
for writing and reading.
Packit dfacfc
Packit dfacfc
If you, as a user, do not need explicit access to the "seen" hash,
Packit dfacfc
then you can set the C<Sparseseen> option to allow Data::Dumper
Packit dfacfc
to eschew building the "seen" hash for scalars that are known not
Packit dfacfc
to possess more than one reference. This speeds up serialization
Packit dfacfc
considerably if you use the XS implementation.
Packit dfacfc
Packit dfacfc
Note: If you turn on C<Sparseseen>, then you must not rely on the
Packit dfacfc
content of the seen hash since its contents will be an
Packit dfacfc
implementation detail!
Packit dfacfc
Packit dfacfc
=back
Packit dfacfc
Packit dfacfc
=head2 Exports
Packit dfacfc
Packit dfacfc
=over 4
Packit dfacfc
Packit dfacfc
=item Dumper
Packit dfacfc
Packit dfacfc
=back
Packit dfacfc
Packit dfacfc
=head1 EXAMPLES
Packit dfacfc
Packit dfacfc
Run these code snippets to get a quick feel for the behavior of this
Packit dfacfc
module.  When you are through with these examples, you may want to
Packit dfacfc
add or change the various configuration variables described above,
Packit dfacfc
to see their behavior.  (See the testsuite in the Data::Dumper
Packit dfacfc
distribution for more examples.)
Packit dfacfc
Packit dfacfc
Packit dfacfc
    use Data::Dumper;
Packit dfacfc
Packit dfacfc
    package Foo;
Packit dfacfc
    sub new {bless {'a' => 1, 'b' => sub { return "foo" }}, $_[0]};
Packit dfacfc
Packit dfacfc
    package Fuz;                       # a weird REF-REF-SCALAR object
Packit dfacfc
    sub new {bless \($_ = \ 'fu\'z'), $_[0]};
Packit dfacfc
Packit dfacfc
    package main;
Packit dfacfc
    $foo = Foo->new;
Packit dfacfc
    $fuz = Fuz->new;
Packit dfacfc
    $boo = [ 1, [], "abcd", \*foo,
Packit dfacfc
             {1 => 'a', 023 => 'b', 0x45 => 'c'},
Packit dfacfc
             \\"p\q\'r", $foo, $fuz];
Packit dfacfc
Packit dfacfc
    ########
Packit dfacfc
    # simple usage
Packit dfacfc
    ########
Packit dfacfc
Packit dfacfc
    $bar = eval(Dumper($boo));
Packit dfacfc
    print($@) if $@;
Packit dfacfc
    print Dumper($boo), Dumper($bar);  # pretty print (no array indices)
Packit dfacfc
Packit dfacfc
    $Data::Dumper::Terse = 1;        # don't output names where feasible
Packit dfacfc
    $Data::Dumper::Indent = 0;       # turn off all pretty print
Packit dfacfc
    print Dumper($boo), "\n";
Packit dfacfc
Packit dfacfc
    $Data::Dumper::Indent = 1;       # mild pretty print
Packit dfacfc
    print Dumper($boo);
Packit dfacfc
Packit dfacfc
    $Data::Dumper::Indent = 3;       # pretty print with array indices
Packit dfacfc
    print Dumper($boo);
Packit dfacfc
Packit dfacfc
    $Data::Dumper::Useqq = 1;        # print strings in double quotes
Packit dfacfc
    print Dumper($boo);
Packit dfacfc
Packit dfacfc
    $Data::Dumper::Pair = " : ";     # specify hash key/value separator
Packit dfacfc
    print Dumper($boo);
Packit dfacfc
Packit dfacfc
Packit dfacfc
    ########
Packit dfacfc
    # recursive structures
Packit dfacfc
    ########
Packit dfacfc
Packit dfacfc
    @c = ('c');
Packit dfacfc
    $c = \@c;
Packit dfacfc
    $b = {};
Packit dfacfc
    $a = [1, $b, $c];
Packit dfacfc
    $b->{a} = $a;
Packit dfacfc
    $b->{b} = $a->[1];
Packit dfacfc
    $b->{c} = $a->[2];
Packit dfacfc
    print Data::Dumper->Dump([$a,$b,$c], [qw(a b c)]);
Packit dfacfc
Packit dfacfc
Packit dfacfc
    $Data::Dumper::Purity = 1;         # fill in the holes for eval
Packit dfacfc
    print Data::Dumper->Dump([$a, $b], [qw(*a b)]); # print as @a
Packit dfacfc
    print Data::Dumper->Dump([$b, $a], [qw(*b a)]); # print as %b
Packit dfacfc
Packit dfacfc
Packit dfacfc
    $Data::Dumper::Deepcopy = 1;       # avoid cross-refs
Packit dfacfc
    print Data::Dumper->Dump([$b, $a], [qw(*b a)]);
Packit dfacfc
Packit dfacfc
Packit dfacfc
    $Data::Dumper::Purity = 0;         # avoid cross-refs
Packit dfacfc
    print Data::Dumper->Dump([$b, $a], [qw(*b a)]);
Packit dfacfc
Packit dfacfc
    ########
Packit dfacfc
    # deep structures
Packit dfacfc
    ########
Packit dfacfc
Packit dfacfc
    $a = "pearl";
Packit dfacfc
    $b = [ $a ];
Packit dfacfc
    $c = { 'b' => $b };
Packit dfacfc
    $d = [ $c ];
Packit dfacfc
    $e = { 'd' => $d };
Packit dfacfc
    $f = { 'e' => $e };
Packit dfacfc
    print Data::Dumper->Dump([$f], [qw(f)]);
Packit dfacfc
Packit dfacfc
    $Data::Dumper::Maxdepth = 3;       # no deeper than 3 refs down
Packit dfacfc
    print Data::Dumper->Dump([$f], [qw(f)]);
Packit dfacfc
Packit dfacfc
Packit dfacfc
    ########
Packit dfacfc
    # object-oriented usage
Packit dfacfc
    ########
Packit dfacfc
Packit dfacfc
    $d = Data::Dumper->new([$a,$b], [qw(a b)]);
Packit dfacfc
    $d->Seen({'*c' => $c});            # stash a ref without printing it
Packit dfacfc
    $d->Indent(3);
Packit dfacfc
    print $d->Dump;
Packit dfacfc
    $d->Reset->Purity(0);              # empty the seen cache
Packit dfacfc
    print join "----\n", $d->Dump;
Packit dfacfc
Packit dfacfc
Packit dfacfc
    ########
Packit dfacfc
    # persistence
Packit dfacfc
    ########
Packit dfacfc
Packit dfacfc
    package Foo;
Packit dfacfc
    sub new { bless { state => 'awake' }, shift }
Packit dfacfc
    sub Freeze {
Packit dfacfc
        my $s = shift;
Packit dfacfc
        print STDERR "preparing to sleep\n";
Packit dfacfc
        $s->{state} = 'asleep';
Packit dfacfc
        return bless $s, 'Foo::ZZZ';
Packit dfacfc
    }
Packit dfacfc
Packit dfacfc
    package Foo::ZZZ;
Packit dfacfc
    sub Thaw {
Packit dfacfc
        my $s = shift;
Packit dfacfc
        print STDERR "waking up\n";
Packit dfacfc
        $s->{state} = 'awake';
Packit dfacfc
        return bless $s, 'Foo';
Packit dfacfc
    }
Packit dfacfc
Packit dfacfc
    package main;
Packit dfacfc
    use Data::Dumper;
Packit dfacfc
    $a = Foo->new;
Packit dfacfc
    $b = Data::Dumper->new([$a], ['c']);
Packit dfacfc
    $b->Freezer('Freeze');
Packit dfacfc
    $b->Toaster('Thaw');
Packit dfacfc
    $c = $b->Dump;
Packit dfacfc
    print $c;
Packit dfacfc
    $d = eval $c;
Packit dfacfc
    print Data::Dumper->Dump([$d], ['d']);
Packit dfacfc
Packit dfacfc
Packit dfacfc
    ########
Packit dfacfc
    # symbol substitution (useful for recreating CODE refs)
Packit dfacfc
    ########
Packit dfacfc
Packit dfacfc
    sub foo { print "foo speaking\n" }
Packit dfacfc
    *other = \&foo;
Packit dfacfc
    $bar = [ \&other ];
Packit dfacfc
    $d = Data::Dumper->new([\&other,$bar],['*other','bar']);
Packit dfacfc
    $d->Seen({ '*foo' => \&foo });
Packit dfacfc
    print $d->Dump;
Packit dfacfc
Packit dfacfc
Packit dfacfc
    ########
Packit dfacfc
    # sorting and filtering hash keys
Packit dfacfc
    ########
Packit dfacfc
Packit dfacfc
    $Data::Dumper::Sortkeys = \&my_filter;
Packit dfacfc
    my $foo = { map { (ord, "$_$_$_") } 'I'..'Q' };
Packit dfacfc
    my $bar = { %$foo };
Packit dfacfc
    my $baz = { reverse %$foo };
Packit dfacfc
    print Dumper [ $foo, $bar, $baz ];
Packit dfacfc
Packit dfacfc
    sub my_filter {
Packit dfacfc
        my ($hash) = @_;
Packit dfacfc
        # return an array ref containing the hash keys to dump
Packit dfacfc
        # in the order that you want them to be dumped
Packit dfacfc
        return [
Packit dfacfc
          # Sort the keys of %$foo in reverse numeric order
Packit dfacfc
            $hash eq $foo ? (sort {$b <=> $a} keys %$hash) :
Packit dfacfc
          # Only dump the odd number keys of %$bar
Packit dfacfc
            $hash eq $bar ? (grep {$_ % 2} keys %$hash) :
Packit dfacfc
          # Sort keys in default order for all other hashes
Packit dfacfc
            (sort keys %$hash)
Packit dfacfc
        ];
Packit dfacfc
    }
Packit dfacfc
Packit dfacfc
=head1 BUGS
Packit dfacfc
Packit dfacfc
Due to limitations of Perl subroutine call semantics, you cannot pass an
Packit dfacfc
array or hash.  Prepend it with a C<\> to pass its reference instead.  This
Packit dfacfc
will be remedied in time, now that Perl has subroutine prototypes.
Packit dfacfc
For now, you need to use the extended usage form, and prepend the
Packit dfacfc
name with a C<*> to output it as a hash or array.
Packit dfacfc
Packit dfacfc
C<Data::Dumper> cheats with CODE references.  If a code reference is
Packit dfacfc
encountered in the structure being processed (and if you haven't set
Packit dfacfc
the C<Deparse> flag), an anonymous subroutine that
Packit dfacfc
contains the string '"DUMMY"' will be inserted in its place, and a warning
Packit dfacfc
will be printed if C<Purity> is set.  You can C<eval> the result, but bear
Packit dfacfc
in mind that the anonymous sub that gets created is just a placeholder.
Packit dfacfc
Someday, perl will have a switch to cache-on-demand the string
Packit dfacfc
representation of a compiled piece of code, I hope.  If you have prior
Packit dfacfc
knowledge of all the code refs that your data structures are likely
Packit dfacfc
to have, you can use the C<Seen> method to pre-seed the internal reference
Packit dfacfc
table and make the dumped output point to them, instead.  See L</EXAMPLES>
Packit dfacfc
above.
Packit dfacfc
Packit dfacfc
The C<Deparse> flag makes Dump() run slower, since the XSUB
Packit dfacfc
implementation does not support it.
Packit dfacfc
Packit dfacfc
SCALAR objects have the weirdest looking C<bless> workaround.
Packit dfacfc
Packit dfacfc
Pure Perl version of C<Data::Dumper> escapes UTF-8 strings correctly
Packit dfacfc
only in Perl 5.8.0 and later.
Packit dfacfc
Packit dfacfc
=head2 NOTE
Packit dfacfc
Packit dfacfc
Starting from Perl 5.8.1 different runs of Perl will have different
Packit dfacfc
ordering of hash keys.  The change was done for greater security,
Packit dfacfc
see L<perlsec/"Algorithmic Complexity Attacks">.  This means that
Packit dfacfc
different runs of Perl will have different Data::Dumper outputs if
Packit dfacfc
the data contains hashes.  If you need to have identical Data::Dumper
Packit dfacfc
outputs from different runs of Perl, use the environment variable
Packit dfacfc
PERL_HASH_SEED, see L<perlrun/PERL_HASH_SEED>.  Using this restores
Packit dfacfc
the old (platform-specific) ordering: an even prettier solution might
Packit dfacfc
be to use the C<Sortkeys> filter of Data::Dumper.
Packit dfacfc
Packit dfacfc
=head1 AUTHOR
Packit dfacfc
Packit dfacfc
Gurusamy Sarathy        gsar@activestate.com
Packit dfacfc
Packit dfacfc
Copyright (c) 1996-2016 Gurusamy Sarathy. All rights reserved.
Packit dfacfc
This program is free software; you can redistribute it and/or
Packit dfacfc
modify it under the same terms as Perl itself.
Packit dfacfc
Packit dfacfc
=head1 VERSION
Packit dfacfc
Packit dfacfc
Version 2.161  (July 11 2016)
Packit dfacfc
Packit dfacfc
=head1 SEE ALSO
Packit dfacfc
Packit dfacfc
perl(1)
Packit dfacfc
Packit dfacfc
=cut