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

smonff

🌌 Sébastien Feugère ☔

Posted on February 7, 2024

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

References

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 }.

💖 💪 🙅 🚩
smonff
🌌 Sébastien Feugère ☔

Posted on February 7, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related