Subject: | Easy stringification filter |
Some data structures are not exactly _pretty_ printed with Data::Dump. For example
$ perl -MData::Dump=dd -MDateTime -e 'dd(DateTime->now(time_zone => "Europe/Lisbon"))'
would generate more than 1000 lines of output, with a lot of locale and timezone information which is often not needed, especially if the user is not interested in debugging DateTime itself, and the the datetime is just a field in a larger data structure.
XS modules sometimes store their data into opaque C data structures, or sometimes it looks like this:
$ perl -MData::Dump=dd -MTime::Moment -e 'dd(Time::Moment->now)'
bless(do{\(my $o = pack("H*","f5d705d50e000000f0ac7d0878000000"))}, "Time::Moment")
Or a Math::Big* example:
$ perl -MData::Dump=dd -MMath::BigInt -e 'dd(Math::BigInt->new("1234567890123456789012345678901234567890"))'
bless({
sign => "+",
value => bless([234567890, 345678901, 456789012, 567890123, 1234], "Math::BigInt::Calc"),
}, "Math::BigInt")
Most of these modules have overloaded stringification methods which make these objects readable again. In case of datetime-related modules this is usually the ISO 8601 output, in case of math-related modules the number itself.
Using a generic Data::Dump filter these stringification methods can easily be used:
use Data::Dump qw(dd);
use Data::Dump::Filtered qw(add_dump_filter);
add_dump_filter(
sub {
my($ctx, $object) = @_;
if ($ctx->is_blessed && defined &overload::Method && overload::Method($object,q{""})) {
return { dump => qq{"$object"} };
## alternatively, to also show the type
# return { dump => qq{"$object" [@{[ ref $object ]}]} };
}
return;
}
);
I don't propose to apply this filter by default. But this possibility could at least be documented (for easy copy'n'paste), or a simple function offered to set this filter, or maybe even new functions ppp() and ddd() using this filter could be invented.