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.


In reply to Re: Object Browser for Perl? by hdb
in thread Object Browser for Perl? by QM

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.