hv has asked for the wisdom of the Perl Monks concerning the following question:
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.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Dumping opaque objects
by haukex (Archbishop) on Jan 24, 2022 at 17:43 UTC | |
by Fletch (Bishop) on Jan 24, 2022 at 20:24 UTC | |
by hv (Prior) on Jan 24, 2022 at 19:02 UTC | |
|
Re: Dumping opaque objects
by Anonymous Monk on Jan 23, 2022 at 23:44 UTC | |
by hv (Prior) on Jan 24, 2022 at 00:42 UTC | |
by syphilis (Archbishop) on Jan 24, 2022 at 10:58 UTC | |
by hv (Prior) on Jan 24, 2022 at 16:04 UTC | |
by syphilis (Archbishop) on Jan 25, 2022 at 10:47 UTC | |
|