Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (3)
As of 2024-04-24 01:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found