Pretty-printing data structures with Perl with Data::Dumper
As Perl allows for complicated data structures by virtue of references to hashes and arrays, it’s often useful to look into what’s going on there. In my case, it was on the output of a large JSON parse.
So to make a long story short, if $tree is a reference to the data structure, go
use Data::Dumper; $Data::Dumper::Terse = 1; $Data::Dumper::Purity = 1; $Data::Dumper::Sortkeys = 1; print Dumper $tree;
Noted how many flags? Data::Dumper isn’t always ideal for pretty-printing (there are a few alternatives) but it wins mainly because it’s part of the commonly installed Perl libraries. One of its drawbacks, which is also its advantage, is that its output is Perl code that reconstructs the data structure. Which means that it fusses with accuracy, in particular if the data structure contains blessed references.
So I definitely miss a Data::Dumper::Hooman or something of that sort.
And even more annoyingly, if it meets a complicated value more than one (e.g. a blessed refs), it puts a reference to the first appearance of the same value in the following times. Which is efficient, maybe, but doesn’t help for reading by a human.
So to the flags.
The Sortkeys flag is recommended for human reading (as well as diffs) for obvious reasons.
The Terse flag makes sure that values are dumped as literals and not referenced. For this to work, the Purity flag was also necessary in my case, or the Terse flag was simply ignored silently. The latter adds a lot of assignments at the end of the dump to fix inaccuracies.
The problem was that one of the fields was blessed as JSON::PP::Boolean, so the dump read
'acInsufInfo' => bless( do{\(my $o = 0)}, 'JSON::PP::Boolean' ),
and then it was referenced over and over again. With the Purity flag, the references appeared at the end of the dump, to correct the inaccurate (non-blessed) assignment before. It seems like without it, Dumper refused to respect the Terse flag for these, because it would break the concept that the dump can be executed to reconstruct the original.
Closing note: It’s quite unusual that mainstream Perl libraries behave in a quirky way that actually needs explanation.