DEV Community

Demand Data::Printer to dump politely if it doesn't want to

If your project got objects that use the following methods names:

  • to_string(),
  • as_string(),
  • stringify(),

... Data::Printer might not behave as expected... It will dump the "stringified" object (as defined in your own code, possibly $self->name or something slightly more complicated).

It's possible to disable that feature on p() call:

p $fancy_object, class => { stringify => 0 };
Enter fullscreen mode Exit fullscreen mode

But honestly, that's too much typing verbosity. I prefer, to have a ~/.dataprinter file containing the following:

class.stringify = 0
Enter fullscreen mode Exit fullscreen mode

It's very convenient, for example, to automate the creation of this file in development environment (e.g. Docker containers). I usually add the following to my Dockerfiles :

RUN echo 'class.stringify = 0' > ~/.dataprinter
Enter fullscreen mode Exit fullscreen mode


From Data::Printer perldoc:

Finally, if your object implements string overload or provides a method called "to_string", "as_string" or "stringify", Data::Printer will use it. To disable this behaviour, set class.stringify = 0 on your .dataprinter file, or call p() with class => { stringify => 0 }.

Top comments (0)