Demand Data::Printer to dump politely if it doesn't want to
🌌 Sébastien Feugère ☔
Posted on February 7, 2024
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 };
But honestly, that's too much typing verbosity. I prefer, to have a ~/.dataprinter
file containing the following:
class.stringify = 0
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 Dockerfile
s :
RUN echo 'class.stringify = 0' > ~/.dataprinter
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 callp()
withclass => { stringify => 0 }
.
Posted on February 7, 2024
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.