A bit too often I write some code, have a problem, stick in a temporary Data::Dumper call, and see something like this:

sub munge_data { my($data) = @_; # temp hack use Data::Dumper; warn Dumper($data); ... # do munging } ... $VAR1 = [ bless( do{\(my $o = 63036880)}, 'Math::GMP' ), bless( do{\(my $o = 63024960)}, 'Math::GMP' ), bless( do{\(my $o = 63021408)}, 'Math::GMP' ) ],

Now Data::Dumper has the Freezer interface, which provides a way to modify the object before dumping - useful if the object caches a bunch of verbose but uninteresting stuff which will automatically be recreated when needed.

What Data::Dumper doesn't seem to provide is a way to say "call this method/function to get the _actual string_ this object should dump as". For this case it would be ideal to represent a GMP object $z as something like "z$z", taking advantage of its existing stringification overload - despite Data::Dumper's stated purpose, I hardly ever care about evalling the output to recreate the data structure.

Does anyone know of a recommended (or remotely usable) way to achieve such a thing? Ideal would be a per-class override, but even a global one would nearly always be as useful to me.

Update: I'm perfectly happy to use a module other than Data::Dumper, clearly that wasn't clear.

Update: I've managed to make some progress with Data::Printer after a few false starts. I'm going to spend some time seeing if I can learn to live with its output format (which has lots of options, but not necessarily the ones I'd want), but I like that I can throw almost everything in a config file so there's minimal boilerplate when I use it:

% cat ~/.dataprinter use_prototypes = 0 index = 0 align_hash = 0 hash_separator = ' => ' colored = 0 begin filter Math::GMP $ddp->unsee($obj); "z_$obj"; end filter % perl -MDDP -MMath::GMP -we ' $val = 1; $zval = Math::GMP->new(1); p [$val, $val, $zval, $zval]; ' [ 1, 1, z_1, z_1 ] %

The hardest bit was avoiding its handling of duplicate references, which was making the last line of output show var[2] instead of z_1. That's what the unsee() call is fixing for me.


In reply to Dumping opaque objects by hv

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.