in reply to Re: Making Data::Dumper also displaying (stringified) Ref-ID's?
in thread Making Data::Dumper also displaying (stringified) Ref-ID's?

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

Replies are listed 'Best First'.
Re^3: Making Data::Dumper also displaying (stringified) Ref-ID's?
by ikegami (Patriarch) on Sep 27, 2009 at 00:28 UTC
    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.

      Thx, but what's the difference to the code in the post you're replying to?

      IMHO it's essentially the same code!(?)

      Cheers Rolf

        I didn't notice the call to Data::Dumper. I thought the dump was actually initialisation for the subsequently demonstrated debugger solution.