Switching to Data::Dump from Data::Dumper for some projects

Like many people I've used the Perl module Data::Dumper for years and been reasonably happy with the output. Generally I want the output rolled up into the smallest (line-wise) output and Data::Dumper allows this. However, I use Math::FixedPrecision quite a bit in this project and as it is a blessed object the fixed precision numbers of 2 come out ridiculously verbose and almost unreadable.

Recently I saw a blog or reference to Data::Dump. I decided to try it and find the output better although a) the output is over multiple lines when I'd prefer the smallest amount of lines (achievable with Data::Dumper's Indent setting) and b) it suffered with the same issue with Math::FixedPrecision. However, Data::Dump also has Data::Dump::Filtered and this allows me to add a sub which filters the output and hence I can turn the rather verbose object output for Math::FixedPrecision with precision 2 into a simple string "11.01" with a simple filter like:

use Data::Dump qw(pp dd);
use Data::Dump::Filtered;
.
.

Data::Dump::Filtered::add_dump_filter(
    sub {
        if ($_[0]->class eq "Math::FixedPrecision") {
            return {dump => "$_[1]"};
        }
        return undef;
    }
);

I've added filters for other objects too in some of my code to avoid printing the entire object like this:

        if ($_[0]->class eq "LWP::UserAgent") {
            return {dump => $_[0]->class}; # return just class name
        } elsif ($_[0]->container_class eq "LWP::UserAgent") {
            return ""; # do nothing

Now, if only I could get it to condense the output more to reduce the number of lines.

Comments

How about Data::Printer?

Hi there! I was wondering if you considered switching to Data::Printer? It has several customization options and inline filters. With it you can do something like:
  use Data::Printer filter => {
      'Math::FixedPrecision' => sub { return "$_[0]" },
  };
And then, anywhere in your code, just write:
   p $object;
If you really like to see as little and as condensed debug information as possible, you can try the following setup:
  use Data::Printer
      multiline => 0,
      class => { expand => 0 },
      filter => {
            'Math::FixedPrecision' => sub { return "$_[0]" },
  };
Which won't even do line breaks, only display class names and, for Math::FixedPrecision, return the stringified version. Another thing is that you can just set a .dataprinter file in your home directory with:
{
      multiline => 0,
      class => { expand => 0 },
      filter => {
            'Math::FixedPrecision' => sub { return "$_[0]" },
      },
}
and never worry about it ever again, just "use Data::Printer;" in your code and it will load your preferences automatically. Well, that's it. In case you did consider it but preferred how Data::Dump does things, I'd really appreciate any input so I can make Data::Printer even better :) Thanks!

I'll try it out but omg, that is a long list of dependencies

I installed Data::Printer this morning and it took around 20 minutes to install and test all the dependencies including Moose. Compare Data::Printer dependencies and Data::Dump dependencies and you'll see what I mean.

I want something small and light and Data::Printer does not look like that but I'll give a try.

See also Data::Printer

I've also started with Data::Dumper, and a year ago moved over to Data::Dump. But last week I moved over from Data::Dump to Data::Printer. It gives a more informative output on lists, and it also supports the concept of Filters like Data::Dump::Filtered. Give it a try.

Data::Printer

Have you looked at Data::Printer?

You should check out

You should check out Data::Printer, which aims to provide pretty-printing for human consumption.

Data::Printer

I'm a long time Data::Dumper user who's recently switched away from it. However, I switched to Data::Printer. In the words of the author, the rationale is: Data::Dumper is a fantastic tool, meant to stringify data structures in a way they are suitable for being eval'ed back in. The thing is, a lot of people keep using it (and similar ones, like Data::Dump) to print data structures and objects on screen for inspection and debugging, and while you can use those modules for that, it doesn't mean mean you should. This is where Data::Printer comes in. It is meant to do one thing and one thing only: display Perl variables and objects on screen, properly formatted (to be inspected by a human) If you want to serialize/store/restore Perl data structures, this module will NOT help you. Try Storable, Data::Dumper, JSON, or whatever. CPAN is full of such solutions! I've only started using it, but I know it does support filters. Might be another option worth looking into.