Just another Perl shrine | |
PerlMonks |
Dumping opaque objectsby hv (Prior) |
on Jan 23, 2022 at 22:52 UTC ( [id://11140754]=perlquestion: print w/replies, xml ) | Need Help?? |
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:
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:
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.
Back to
Seekers of Perl Wisdom
|
|