For interactive inspection of objects it would be useful to be able to fold sections of the object. One way could be to dump the object in some kind of xml format and use an xml editor to close and open sections of the object. In order to create xml, XML::Simple is too simple as keys of hashes make not automatically valid xml tags. I am not aware of a simple xml creator for an arbitrary structure, so I tried to roll my own:
use strict;
use warnings;
my $tab = " ";
my %refs_seen;
my %dispatch =
(
# scalar
'' => sub { my ( $element, $level ) = @_;
print $tab x $level, $element // "UNDEF", "\n"
+;
},
'HASH' => sub { my ( $element, $level ) = @_;
print $tab x $level++, "<HASH ref=\"$element\"
+>\n";
for my $key (keys %{$element}) {
print $tab x $level, "<key_$key>\n";
structure_to_xml( $element->{$key}, $level
++1 );
print $tab x $level, "</key_$key>\n"
}
print $tab x --$level, "</HASH>\n";
},
'ARRAY' => sub { my ( $element, $level ) = @_;
print $tab x $level, "<ARRAY ref=\"$element\">
+\n";
structure_to_xml( $_, $level+1 ) for @{$elemen
+t};
print $tab x $level, "</ARRAY>\n";
},
'default' => sub { my ( $element, $level ) = @_;
print $tab x $level, "<UNKNOWN ref=\"".ref($el
+ement)."\"/>\n"
},
);
# operates on references
sub structure_to_xml {
my ( $element, $level ) = @_;
if( ref $element ) {
if( exists $refs_seen{$element} ) {
print $tab x $level, "<SELFREF ref=\"$element\"/>\n";
return;
} else {
$refs_seen{$element} = 1;
}
}
&{ $dispatch{ ref($_[0]) } // $dispatch{'default'} }(@_);
}
# here comes an example for using it
use XML::Simple;
# enable looking into XML::Simple objects
$dispatch{'XML::Simple'} = $dispatch{HASH}; # comment out if you do no
+t want to see internals
my $xml = XML::Simple->new();
sub somesub { }
my $hashref;
$hashref->{self} = $hashref;
my $var = { "0x55555555" => { "0x55555555" => [ ["0xAAAAAAAA", "0x9"],
+ ],
"0xAAAAAAAA" => [ ["0xAAAAAAAA", "0x8"],
+ ], },
"0xAAAAAAAA" => { "0x55555555" => [ ["0xFFFFFFFF", "0x8"],
+ ],
"0xAAAAAAAA" => [ ["0x55555554", "0x3"],
+ ], },
"self" => $hashref,
"code" => \&somesub,
"xmlparser" => $xml,
};
structure_to_xml( $var, 0 );
The dispatch table gives you a lot of flexibility. Any unknown structures will be processed in the 'default' section and you will not see any details. If you want to see the details you just need to add a rule how to deal with it. The 'HASH' rule should work for blessed references as shown in the XML::Simple example.
What one probably wants to add is a facility to write it into file instead of STDOUT but that should be simple.
It is no good for diffing across runs as it prints the memory locations as well that I expect not to be constant across runs.