Preload Data::Printer and avoid polluting your code with debug symbols

smonff

🌌 Sébastien Feugère ☔

Posted on February 8, 2024

Preload Data::Printer and avoid polluting your code with debug symbols

When I work, I tend to have a lot of boilerplate. I am a great user of Data::Printer for debugging. There is no way I wouldn't use it, even on the smallest project. Usually, I have this on all my projects:

use strict;
use warnings;
use Data::Printer;

p $object;
Enter fullscreen mode Exit fullscreen mode

Fellow Peter coworker saw me doing that once and said I liked typing too much: only use DDP was enough.

Later I learned another solution was provided in the DDP documentation.

It's possible to call ::pinstead of p(): ::p doesn't require to use the module first.

use strict;
use warnings;

::p $object;
Enter fullscreen mode Exit fullscreen mode

For getting this to work, it's necessary to preload the module through command line:

perl -MDDP script.pl
Enter fullscreen mode Exit fullscreen mode

The good thing is that if we forget debug symbols in a module that is going to reach deployment pipelines or the CPAN (assuming we don't have other tools configured complaining about that), it will trigger a compile-time failure without the -M option, making it easier to catch.

Note that it also works well for tests with Test2::Harness:

yath -MDDP 01_test.t
Enter fullscreen mode Exit fullscreen mode
💖 💪 🙅 🚩
smonff
🌌 Sébastien Feugère ☔

Posted on February 8, 2024

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

Sign up to receive the latest update from our blog.

Related