Search code examples
perldata-dumperdata-dump

Making more of Perl Data::Dumper output


I have several nested data structures that refer to one another's elements. I would like to be able to check those references, so I am searching for something that will print the memory address of nested structures. An option for Data::Dumper would be fine.

Here are some examples of what I mean:

my @a = ( [1,2,3], [4,5,6] );
print \@a;

Will give you something like:

ARRAY(0x20071dc8)

When you run the same code through the debugger and examine the array with x \@a it will print this:

0  ARRAY(0x20070668)
   0  ARRAY(0x2006c0d8)
      0  1
      1  2
      2  3
   1  ARRAY(0x2006c1b0)
      0  4
      1  5
      2  6

But using Data::Dumper

print Dumper \@a;

It looks like this

$VAR1 = [
          [
            1,
            2,
            3
          ],
          [
            4,
            5,
            6
          ]
        ];

What I really want is a mixture of the Data::Dumper output and the details that the debugger provides. Perhaps this

$VAR1 = [ ARRAY(0x20070668)
          [ ARRAY(0x2006c0d8)
            1,
            2,
            3
          ],
          [ ARRAY(0x2006c1b0)
            4,
            5,
            6
          ]
        ];

Edit

Consider this code. The output doesn't explain that $b[1] is the same reference as in $a[0]

use Data::Dumper;

my @a = ( [1,2,3], [4,5,6] );
my @b = ( ["a","b","c"], $a[0] );

print Dumper \@b

print $b[1], "\n";
print $a[0], "\n";

output

$VAR1 = [
          [
            'a',
            'b',
            'c'
          ],
          [
            1,
            2,
            3
          ]
        ];


ARRAY(0x2002bcc0)
ARRAY(0x2002bcc0)

Also, is this approach, when one structure references the contents of another, considered to be good programming practice? Maybe this is too general question and heavily depends on particular code but I would like to know your opinion.


Solution

  • Data::Dumper will already tell if a reference is reused.

    In the following example, the 2nd and 3rd elements of the AoA are identical. This is represented in the Dumper output:

    use strict;
    use warnings;
    
    my @array1 = (1..3);
    my @array2 = (4..6);
    
    my @AoA = (\@array1, \@array2, \@array2);
    
    use Data::Dumper;
    print Dumper \@AoA;
    

    Outputs:

    $VAR1 = [
              [
                1,
                2,
                3
              ],
              [
                4,
                5,
                6
              ],
              $VAR1->[1]
            ];
    

    Responding to your edit

    If you want to find the relation between two different data structures, just make a single call to Dumper with both data structures.

    You can do this by passing them as a list, or as values in another anonymous data structure, like a hash or array:

    use strict;
    use warnings;
    
    my @a = ([1,2,3], [4,5,6]);
    my @b = (["a","b","c"], $a[0]);
    
    use Data::Dumper;
    
    print Dumper(\@a, \@b);
    

    Outputs:

    $VAR1 = [
              [
                1,
                2,
                3
              ],
              [
                4,
                5,
                6
              ]
            ];
    $VAR2 = [
              [
                'a',
                'b',
                'c'
              ],
              $VAR1->[0]
            ];