note
andreas1234567
A complete working sample using [mod://Data::Dumper] where an object having a [mod://DateTime] reference can be printed in short and long versions depending on <c>$Data::Dumper::Freezer</c>:
<c>
# 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__
</c>
Test:
<c>
# 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__
</c>
Run:
<c>
$ perl Foo.t
1..2
ok 1 - prints short version
ok 2 - prints long version
$
</c>
<!-- Node text goes above. Div tags should contain sig only -->
<div class="pmsig"><div class="pmsig-321512">
<small>
--<br>
No matter how great and destructive your problems may seem now, remember, you've probably only seen the tip of them. <a href="http://www.despair.com/viewall.html">[1]</a>
</small>
</div></div>
951201
951201