Stories
Slash Boxes
Comments
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

use Perl Log In

Log In

[ Create a new account ]

Ovid (2709)

Ovid
  (email not shown publicly)
http://publius-ovidius.livejournal.com/
AOL IM: ovidperl (Add Buddy, Send Message)

Stuff with the Perl Foundation. A couple of patches in the Perl core. A few CPAN modules. That about sums it up.

Journal of Ovid (2709)

Monday June 23, 2008
11:16 AM

Data::Dumper::Overload

[ #36753 ]

Dear Lazyweb: tell me that there's a Data::Dumper (or better yet, Data::Dump::Streamer) alternative that allows me to respect string overloading. Specifically, I need to have a Data::Dumper-like module which accepts a hashref of which overloaded modules to respect for string overloading (sometimes I want all, sometimes I want none). Or better still, a callback system whereby I can specify how a particular data structure gets represented when dumped. I hack together the following, but it was for a very specific, internal need. Thus, it's probably useless for your purposes, but it suits mine perfectly (and it's a one-shot deal and not designed to live beyond a few hours).

package PrettyDumper;

use strict;
use warnings;
use Scalar::Util qw(blessed reftype);
use Clone 'clone';
use Sub::Information as => 'inspect';
use Carp 'croak';
require overload;

use Exporter::NoWork;
my %dispatch = (
    ''      => sub { shift || '<undef>' },
    'ARRAY' => \&_array,
    'HASH'  => \&_hash,
    'CODE'  => sub { inspect(shift)->code },
);

sub pretty_dump {
    my $thing = clone(shift);
    require Data::Dumper;
    local $Data::Dumper::Terse = 1;
    chomp( my $dump =  Data::Dumper::Dumper(_pretty_dump($thing)) );
    return $dump;
}

sub _pretty_dump {
    my $thing = shift;
    my $ref = reftype($thing) || '';
    if ( my $dispatch = $dispatch{$ref} ) {
        return $dispatch->($thing);
    }
    return Data::Dumper::Dumper($thing);
}

sub _array {
    my $array = shift;
    if ( overload::Overloaded($array) ) {
        if ( my $method = overload::Method( $array, '""' ) ) {
            return $array->$method;
        }
    }
    else {
        foreach (@$array) {
            $_ = _pretty_dump($_);
        }
    }
    return $array;
}

sub _hash {
    my $hash = shift;
    if ( overload::Overloaded($hash) ) {
        if ( my $method = overload::Method( $hash, '""' ) ) {
            return $hash->$method;
        }
    }
    else {
        foreach (keys %$hash) {
            $hash->{$_} = _pretty_dump($hash->{$_});
        }
    }
    return $hash;
}

1;

It doesn't offer me the 'per package' control that I want, but it's close enough for what I need.

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
 Full
 Abbreviated
 Hidden
More | Login | Reply
Loading... please wait.
  • Here is a demonstration.

    use Data::Dump::Streamer;
    my $obj = bless {}, "Foo";
    Dump($obj)->Out;
    Dump($obj)->Freezer(
            "Foo", sub {my $self = shift; "$self"}
        )->Out;

    Now obviously in this case Foo does not have a useful stringify method. But if it did, it would be used.

  • Hearing you in the vicinity of the Data::Dumper namespace just reminded me that I'm long overdue in sending thanks for creating Data::Dumper::Simple. It's one of those modules that just happily motors into place when required, does its job without any fuss, and produces exactly what I want to see.

    So... thanks,

    Paul

    • Thanks you! It's always good to get feedback about what people do and don't like about my code (especially the "do like" bits). Since it's based on a source filter, I was given a bit of grief over its creation, but I'm pleasantly surprised by people's reactions to it. My favorite is woman back in the States who tells me that she can never hate me because she uses Data::Dumper::Simple so much that it would make her mad every time she programs :)

      • Ovid and the women… ;-)

        • Regrettably, any woman with even the remotest interest in me seems to be doomed to live in a different country. I want to find this "Fate" guy and put the boot in a few times.