Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Data::Dumper too noisy

by Anonymous Monk
on Dec 02, 2008 at 17:42 UTC ( [id://727468]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks!

I'm developing a web app, using Data::Dumper to write a kind of crash dump into the error log file. Unfortunately, the output becomes quite large, as one of the dumped variables contains lots of other objects. The internals of several of those objects (mostly not my code) are not interesting for the crash dump. Instead, I would like to see those objects stringified, while the other object (my stuff) should be dumped normally.

Some of the classes are:

Math::BigInt
I want to see the stringified number ("27"), not the internals 'bless({ "sign" => "+", "value" => [ 27 ]},"Math::BigInt")'
DateTime
like above, I want to see some date (iso8601, the default stringification, is ok), not a 35 line dump of DateTime internals
DateTime::TimeZone
like above, I don't need a dump of the entire TZ DB.
Classes inheriting from Rose::DB::Object::Metadata
All I need to see here is something like "My::App::DB::Foo::Bar::MetaData=HASH(0x12345678)"

It seems I would need a hook into Data::Dumper that is called when Data::Dumper attempts to dump a blessed reference. It would decide if the reference is dumped as usual or if the reference is stringified.

Something like

print Data::Dumper->new([$mystuff],['mystuff'])->objectFilter(sub { my $obj=shift; return "$obj" if (ref($obj) eq 'Math::BigInt') or $obj->isa('Rose::D +B::Object::Metadata'); return $obj; })->Dump();

Any hints how to realize this?

Thanks,
Tux2000

Replies are listed 'Best First'.
Re: Data::Dumper too noisy
by kyle (Abbot) on Dec 02, 2008 at 17:58 UTC

    I'm inclined to preprocess the data structure that you pass to Data::Dumper. If you need to use the data again afterward, start by making a copy of everything (see deep copy of nested data structure). After that, use something like Data::Visitor::Callback to roam through it and clobber everything you want not to show up.

    The following stringifies every DateTime object.

    use Data::Dumper; use DateTime; use Data::Visitor::Callback; my $glob = { dt => DateTime->now(), h => { a => 1, b => 2, dtx => DateTime->now() }, }; my $v = Data::Visitor::Callback->new( 'DateTime' => sub { $_ = "$_" }, 'ignore_return_values' => 1, ); $v->visit( $glob ); print Dumper $glob; __END__ $VAR1 = { 'h' => { 'dtx' => '2008-12-02T17:56:41', 'a' => 1, 'b' => 2 }, 'dt' => '2008-12-02T17:56:41' };
Re: Data::Dumper too noisy
by samwyse (Scribe) on Dec 03, 2008 at 03:14 UTC
    How about monkey-patching Data::Dumper?
    use Data::Dumper; use DateTime; use DateTime::TimeZone; use Math::BigInt; $var = { number => 1, bigint => Math::BigInt->new(), datetime => DateTime->now(), tz => DateTime::TimeZone->new( name => 'America/Chicago' ), }; $Data::Dumper::Useperl = 1; *Data::Dumper::_dump_orig = \&Data::Dumper::_dump; *Data::Dumper::_dump = sub { return "Math::BigInt->new('".$_[1]->bstr()."')" if ref $_[1] eq 'Math::BigInt'; return "rfc822('".$_[1]->strftime("%a, %d %b %Y %H:%M:%S %z")."')" if ref $_[1] eq 'DateTime'; return ref $_[1] if (ref $_[1]) =~ /^DateTime::TimeZone::/; goto &Data::Dumper::_dump_orig; }; print Dumper($var);
    Running it returns this:
    $VAR1 = { 'bigint' => Math::BigInt->new('0'), 'number' => 1, 'tz' => DateTime::TimeZone::America::Chicago, 'datetime' => rfc822('Wed, 03 Dec 2008 04:12:53 +0000') };
    Note that most people would prefer that you subclass Data::Dumper to do the same thing, but this works if you need something quick and dirty.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://727468]
Approved by Fletch
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (7)
As of 2024-04-23 10:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found