<?xml version="1.0" encoding="windows-1252"?>
<node id="481746" title="Answer: How can I visualize my complex data structure?" created="2005-08-07 18:24:02" updated="2005-08-11 12:08:54">
<type id="1888">
categorized answer</type>
<author id="11732">
QandAEditors</author>
<data>
<keywords>
<keyword rating="">
data_structure</keyword>
</keywords>
<field name="doctext">
&lt;p&gt;Yes. In the spirit of TMTOWTDI, there are several ways to
represent your data structure. Which you choose is largely up to
you. The information provided below should serve as an overview
only; always consult a module's complete documentation for usage,
bugs, and caveats.&lt;/p&gt;
&lt;blockquote&gt;
&lt;ol&gt;
&lt;li&gt;[cpan://Dumpvalue]&lt;/li&gt;
&lt;/ol&gt;
&lt;blockquote&gt;
&lt;p&gt;More of a [jargon://prettyprint]er than a serializer, this
method provides the view of your data structure that is most like
the output from Perl's debugger. The output format may be modified
by setting numerous options. [cpan://Dumpvalue] can even dump the
symbol tables of whole packages.&lt;/p&gt;
&lt;code&gt;
use strict;
use warnings;

use Dumpvalue;

my $ref = {
    a =&gt; [ 1, 2, 3 ],
    b =&gt; [
        { X =&gt; 1, Y=&gt; 2 },
        {
            X =&gt; [ 1, 2, 3 ],
            Y =&gt; [ 4, 5, 6 ],
            Z =&gt; [ 7, 8, 9 ]
        },
    ],
};

print "Dumpvalue:\n";
Dumpvalue-&gt;new-&gt;dumpValue( $ref );

&lt;/code&gt;
&lt;p&gt;Output:&lt;/p&gt;
&lt;code&gt;
Dumpvalue:
'a' =&gt; ARRAY(0x460ee8)
   0  1
   1  2
   2  3
'b' =&gt; ARRAY(0x4e3e50)
   0  HASH(0x4d964c)
      'X' =&gt; 1
      'Y' =&gt; 2
   1  HASH(0x4e3e14)
      'X' =&gt; ARRAY(0x4d967c)
         0  1
         1  2
         2  3
      'Y' =&gt; ARRAY(0x4e3d9c)
         0  4
         1  5
         2  6
      'Z' =&gt; ARRAY(0x4e3dd8)
         0  7
         1  8
         2  9

&lt;/code&gt;&lt;/blockquote&gt;
&lt;ol start="2"&gt;
&lt;li&gt;[cpan://Data::Dumper]&lt;/li&gt;
&lt;/ol&gt;
&lt;blockquote&gt;
&lt;p&gt;Possibly the most popular choice, [cpan://Data::Dumper] outputs
actual Perl code. As such, it is both a [jargon://prettyprint]er
&lt;i&gt;and&lt;/i&gt; a serializer. [cpan://Data::Dumper] can handle
self-referential data structures, and &lt;code&gt;eval&lt;/code&gt;ing
[cpan://Data::Dumper]'s output reconstitutes an exact copy of the
original data structure. This module is useful both for saving data
structures to disk and DBM files and for passing them to other
processes. As with [cpan://Dumpvalue], [cpan://Data::Dumper]'s
output is configurable.&lt;/p&gt;
&lt;code&gt;
use strict;
use warnings;

use Data::Dumper;

my $ref = {
    a =&gt; [ 1, 2, 3 ],
    b =&gt; [
        { X =&gt; 1, Y=&gt; 2 },
        {
            X =&gt; [ 1, 2, 3 ],
            Y =&gt; [ 4, 5, 6 ],
            Z =&gt; [ 7, 8, 9 ]
        },
    ],
};

print "Data::Dumper:\n";
print Dumper( $ref );

&lt;/code&gt;
&lt;p&gt;Output:&lt;/p&gt;
&lt;code&gt;
Data::Dumper:
$VAR1 = {
          'a' =&gt; [
                   1,
                   2,
                   3
                 ],
          'b' =&gt; [
                   {
                     'X' =&gt; 1,
                     'Y' =&gt; 2
                   },
                   {
                     'Z' =&gt; [
                              7,
                              8,
                              9
                            ],
                     'X' =&gt; [
                              1,
                              2,
                              3
                            ],
                     'Y' =&gt; [
                              4,
                              5,
                              6
                            ]
                   }
                 ]
        };

&lt;/code&gt;&lt;/blockquote&gt;
&lt;ol start="3"&gt;
&lt;li&gt;[cpan://YAML]'s Dump&lt;/li&gt;
&lt;/ol&gt;
&lt;blockquote&gt;
&lt;p&gt;Another [jargon://prettyprint]er/serializer combo.
[cpan://YAML]'s output format is also controllable and hash keys
are sorted by default. Since [cpan://YAML] does not &lt;code&gt;eval&lt;/code&gt; to de-serialize, it is safer than
[cpan://Data::Dumper] when you're not sure who else might have
access to your files. Data structures serialized from Perl using
[cpan://YAML] can be accurately read and processed using
[cpan://YAML] in Java, Javascript, PHP, Python, Ruby, and Tcl (and
of course, Perl). Likewise, data structures may be serialized in
any of these languages and be usable by Perl.&lt;/p&gt;
&lt;code&gt;
use strict;
use warnings;

use YAML;

my $ref = {
    a =&gt; [ 1, 2, 3 ],
    b =&gt; [
        { X =&gt; 1, Y=&gt; 2 },
        {
            X =&gt; [ 1, 2, 3 ],
            Y =&gt; [ 4, 5, 6 ],
            Z =&gt; [ 7, 8, 9 ]
        },
    ],
};

print "YAML::Dump:\n";
print Dump( $ref );

&lt;/code&gt;
&lt;p&gt;Output:&lt;/p&gt;
&lt;code&gt;
YAML::Dump:
---
a:
  - 1
  - 2
  - 3
b:
  - X: 1
    Y: 2
  - X:
      - 1
      - 2
      - 3
    Y:
      - 4
      - 5
      - 6
    Z:
      - 7
      - 8
      - 9
      
&lt;/code&gt;&lt;/blockquote&gt;
&lt;ol start="4"&gt;
&lt;li&gt;[cpan://Data::Dump::Streamer], by [demerphq]&lt;/li&gt;
&lt;/ol&gt;
&lt;blockquote&gt;
&lt;p&gt;If it's by our very own [demerphq], it &lt;i&gt;must&lt;/i&gt; be good.&lt;/p&gt;
&lt;p&gt;[cpan://Data::Dump::Streamer] offers more accurate and more
readable output, and uses less memory, than does
[cpan://Data::Dumper]. Hash keys are sorted and the sort order is
configurable. [cpan://Data::Dump::Streamer]'s output, like that of
[cpan://Data::Dumper], may be &lt;code&gt;eval&lt;/code&gt;ed back to
its original form.&lt;/p&gt;
&lt;code&gt;
use strict;
use warnings;

use Data::Dump::Streamer;

my $ref = {
    a =&gt; [ 1, 2, 3 ],
    b =&gt; [
        { X =&gt; 1, Y=&gt; 2 },
        {
            X =&gt; [ 1, 2, 3 ],
            Y =&gt; [ 4, 5, 6 ],
            Z =&gt; [ 7, 8, 9 ]
        },
    ],
};

print "Data::Dump::Streamer:\n";
print Dump( $ref );

&lt;/code&gt;
&lt;p&gt;Output:&lt;/p&gt;
&lt;code&gt;
Data::Dump::Streamer:
$HASH1 = {
           a =&gt; [
                  1,
                  2,
                  3
                ],
           b =&gt; [
                  {
                    X =&gt; 1,
                    Y =&gt; 2
                  },
                  {
                    X =&gt; [
                           1,
                           2,
                           3
                         ],
                    Y =&gt; [
                           4,
                           5,
                           6
                         ],
                    Z =&gt; [
                           7,
                           8,
                           9
                         ]
                  }
                ]
         };
         
&lt;/code&gt;
See also: [cpan://Test::Struct]&lt;/blockquote&gt;
&lt;ol start="5"&gt;
&lt;li&gt;[cpan://GraphViz::Data::Structure] is one of two modules I know
of that literally draw you a picture of your data structure. Great
for the visually-oriented, but none of the de-serializing or
multi-lingual features of the above alternatives.&lt;/li&gt;
&lt;/ol&gt;
&lt;blockquote&gt;
&lt;code&gt;
use strict;
use warnings;

use GraphViz::Data::Structure;

my $data_structure = {
    a =&gt; [ 1, 2, 3 ],
    b =&gt; [
        { X =&gt; 1, Y =&gt; 2 },
        {
            X =&gt; [ 1, 2, 3 ],
            Y =&gt; [ 4, 5, 6 ],
            Z =&gt; [ 7, 8, 9 ]
        },
    ],
};

my $gvds =
  GraphViz::Data::Structure-&gt;new( $data_structure, Orientation =&gt; 'vertical' );
print $gvds-&gt;graph()-&gt;as_png("gvds.png");

&lt;/code&gt;
&lt;p&gt;&lt;a href="http://planetscape.perlmonk.org/gvds.png"&gt;Output&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;ol start="6"&gt;
&lt;li&gt;[cpan://GraphViz::Data::Grapher] is the other of the two, and
happens to be my personal favorite. No reconstituting original
structures or sharing them with other languages such as Python or
Ruby; but this does create very nice illustrations of complex data
structures for documentation or perhaps teaching purposes.&lt;/li&gt;
&lt;/ol&gt;
&lt;blockquote&gt;
&lt;code&gt;
use strict;
use warnings;

use GraphViz::Data::Grapher;

my $structure = {
    a =&gt; [ 1, 2, 3 ],
    b =&gt; [
        { X =&gt; 1, Y =&gt; 2 },
        {
            X =&gt; [ 1, 2, 3 ],
            Y =&gt; [ 4, 5, 6 ],
            Z =&gt; [ 7, 8, 9 ]
        },
    ],
};

my $graph = GraphViz::Data::Grapher-&gt;new($structure);
print $graph-&gt;as_png("gvdg.png");

&lt;/code&gt;
&lt;p&gt;&lt;a href="http://planetscape.perlmonk.org/gvdg.png"&gt;Output&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;&lt;i&gt;For more on visualizing hash structure, please see: [id://471590]&lt;/i&gt;&lt;/b&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;References&lt;/b&gt; &lt;font size="2"&gt;(incomplete and in no
particular order)&lt;/font&gt;&lt;b&gt;:&lt;/b&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;[id://30275]&lt;/li&gt;
&lt;li&gt;[id://390153]&lt;/li&gt;
&lt;li&gt;[id://433537]&lt;/li&gt;
&lt;li&gt;[id://439530]&lt;/li&gt;
&lt;li&gt;[id://472337]&lt;/li&gt;
&lt;li&gt;[id://474783]&lt;/li&gt;
&lt;li&gt;[id://476435]&lt;/li&gt;
&lt;li&gt;[id://476806]&lt;/li&gt;
&lt;li&gt;[id://478513]&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;
</field>
<field name="parent_node">
481745</field>
</data>
</node>
