Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Condensed output of Data::Dumper used on DateTime

by andreas1234567 (Vicar)
on Feb 01, 2012 at 12:31 UTC ( #951201=perlquestion: print w/ replies, xml ) Need Help??
andreas1234567 has asked for the wisdom of the Perl Monks concerning the following question:

Honorable Monks,

I'm looking for a way to increase the readability of log files.

If a reference to a DateTime object gets dumped, then the output can span multiple lines of relatively uninteresting and confusing output. Is there a way to override the 'dump' call so that what gets logged is a single line (e.g. $dt->dmy . q{ } .$dt->hms or equivalent)? This would be analogous to the TO_JSON support in JSON::XS.

$ perl -wle 'use Data::Dumper; use DateTime; my $dt = DateTime::->now( +); print Dumper($dt); print $dt->dmy . q{ } .$dt->hms' $VAR1 = bless( { 'local_rd_secs' => 44783, 'local_rd_days' => 734534, 'rd_nanosecs' => 0, 'locale' => bless( { 'default_time_format_length' => +'medium', 'native_territory' => 'United St +ates', 'native_language' => 'English', 'native_complete_name' => 'Engli +sh United States', 'en_language' => 'English', 'id' => 'en_US', 'default_date_format_length' => +'medium', 'en_complete_name' => 'English U +nited States', 'en_territory' => 'United States +' }, 'DateTime::Locale::en_US' ), 'local_c' => { 'hour' => 12, 'second' => 23, 'month' => 2, 'quarter' => 1, 'day_of_year' => 32, 'day_of_quarter' => 32, 'minute' => 26, 'day' => 1, 'day_of_week' => 3, 'year' => 2012 }, 'utc_rd_secs' => 44783, 'formatter' => undef, 'tz' => bless( { 'name' => 'UTC' }, 'DateTime::TimeZone::UTC' ), 'utc_year' => 2013, 'utc_rd_days' => 734534, 'offset_modifier' => 0 }, 'DateTime' ); 01-02-2012 12:26:23
--
No matter how great and destructive your problems may seem now, remember, you've probably only seen the tip of them. [1]

Comment on Condensed output of Data::Dumper used on DateTime
Select or Download Code
Re: Condensed output of Data::Dumper used on DateTime
by jmcnamara (Monsignor) on Feb 01, 2012 at 13:11 UTC
Re: Condensed output of Data::Dumper used on DateTime
by thargas (Chaplain) on Feb 01, 2012 at 13:26 UTC
    Data::Dump's dump_filtered() will allow you to apply whatever filters seem right to you. I don't see any such hook in Data::Dumper
Re: Condensed output of Data::Dumper used on DateTime
by tobyink (Abbot) on Feb 01, 2012 at 14:03 UTC

    Dump Data::Dumper.

    Use Data::Printer instead.

    Data::Dumper generates executable Perl code as its output. That's great. But when debugging, do you actually need executable Perl to be dumped? Probably not. You can probably get more readable information if you sacrifice your dump's executability.

    One of the examples given in the Data::Printer pod is dealing with DateTime.

Re: Condensed output of Data::Dumper used on DateTime
by educated_foo (Vicar) on Feb 01, 2012 at 16:17 UTC
    Yes. Set $Data::Dumper::Freezer to the name of the method you want called, then implement that method in DateTime, e.g.
    $Data::Dumper::Freezer = 'my_dumper'; sub DateTime::my_dumper { ... }
    Even better, you can localize the change to $Data::Dumper::Freezer so pretty-printing only happens when you want it to.

    EDIT: Corrected to DateTime::my_dumper...

Re: Condensed output of Data::Dumper used on DateTime
by andreas1234567 (Vicar) on Feb 28, 2012 at 22:26 UTC
    A complete working sample using Data::Dumper where an object having a DateTime reference can be printed in short and long versions depending on $Data::Dumper::Freezer:
    # Foo.pm package Foo; use strict; use warnings; use DateTime; sub new { my $class = shift; my $self = { _timevalue => DateTime->now, }; bless( $self, $class ); return $self; } # Modify _timevalue and return blessed reference sub _dumper_hook { $_[0] = bless { %{ $_[0] }, _timevalue => $_[0]->{_timevalue}->ymd . q{ } . $_[0]->{_timevalue}->hms, }, ref( $_[0] ); } 1; __END__
    Test:
    # Foo.t: use strict; use warnings; use Data::Dumper; use Test::More; plan tests => 2; use Foo; my $f = Foo::->new; { local $Data::Dumper::Freezer = '_dumper_hook'; like( Dumper($f), qr/\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2}/, q{prints short version} ); } unlike( Dumper($f), qr/\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2}/, q{prints long version} ); __END__
    Run:
    $ perl Foo.t 1..2 ok 1 - prints short version ok 2 - prints long version $
    --
    No matter how great and destructive your problems may seem now, remember, you've probably only seen the tip of them. [1]

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (10)
As of 2014-08-01 08:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (257 votes), past polls