LanX has asked for the wisdom of the Perl Monks concerning the following question:

Hi

I have to admit that most parts of Data::Dumper are rather unintuitive for me. Is there an option (or alternative modul) to display the stringified references alongside with the actual output?

So instead of

DB<1> use Data::Dumper DB<2> $hr={a=>{b=>{c=>1}}} DB<3> print Dumper $hr $VAR1 = { 'a' => { 'b' => { 'c' => 1 } } }; DB<4> print $hr,$hr->{a},$hr->{a}{b},$hr->{a}{b}{c} HASH(0x98a2930)HASH(0x97f5060)HASH(0x9809780)1

I'd like to get something like :

$VAR1 = { # HASH(0x98a2930) 'a' => { # HASH(0x97f5060) 'b' => { # HASH(0x9809780) 'c' => 1 } } };

Cheers Rolf

Replies are listed 'Best First'.
Re: Making Data::Dumper also displaying (stringified) Ref-ID's?
by toolic (Bishop) on Sep 26, 2009 at 14:27 UTC
      Thanx toolic, indeed I have to read this thoroughly.

      I was actually looking for a way to display the difference between deep and flat copy to a newbie, and no figured out that the Seen-Mechanism in Data::Dumper already reveals it, without the need to print the Ref-IDs. 8)

      DB<90> $hr2={%$hr1} # shallow copy DB<91> print Data::Dumper->Dump([$hr1,$hr2],[qw/hr1 hr2/]) $hr1 = { 'a' => { 'b' => { 'c' => 1 } } }; $hr2 = { 'a' => $hr1->{'a'} }; DB<92> x $hr1,$hr2 0 HASH(0x8b64b78) 'a' => HASH(0x8b64c18) 'b' => HASH(0x8af9120) 'c' => 1 1 HASH(0x8b156c0) 'a' => HASH(0x8b64c18) -> REUSED_ADDRESS

      Anyway IMHO an option to add the IDs as comments would be welcomed by many people as a valuable feature!

      (update: maybe this can be done with Data::Dump::Streamer?)

      Cheers Rolf

      UDPATE: improved code example

        Dump the original and the copy using one call to Dumper, and the difference between a shallow and a deep copy will be obvious.
        use Storable qw( dclone ); use Data::Dumper qw( Dumper ); my %hash = (a=>{b=>{c=>1}}); my %shallow = %hash; my %deep = %{ dclone(\%hash) }; local $Data::Dumper::Indent = 1; print("Shallow\n"); print("-------\n"); print(Dumper(\%hash, \%shallow)); print("\n\n"); print("Deep\n"); print("----\n"); print(Dumper(\%hash, \%deep));
        Shallow ------- $VAR1 = { 'a' => { 'b' => { 'c' => 1 } } }; $VAR2 = { 'a' => $VAR1->{'a'} }; Deep ---- $VAR1 = { 'a' => { 'b' => { 'c' => 1 } } }; $VAR2 = { 'a' => { 'b' => { 'c' => 1 } } };

        As you can see, the shallow copy clearly references back to the original, whereas the deep copy doesn't.