in reply to Fine grain control over Data::Dumper

Another possibility you may consider is writing a wrapper object that @ISA  = qw(Data::Dumper). Should be a fairly simple task of writing sub _dump such that it tests to see of the object class matches yours, and if not, invokes SUPER::_dump. If you're not familiar with OO in Perl, Overridden Methods has a pretty good summary.

Replies are listed 'Best First'.
Re^2: Fine grain control over Data::Dumper
by rovf (Priest) on May 29, 2009 at 11:51 UTC

    UPDATE: I FOUND THE BUG!

    I should not have called $class->SUPER::Dumper(@args);, but $class->SUPER::Dump(@args);, because Dumper() always blesses into a Data::Dumper object.

    To my surprise, I found it more difficult to override _dump then expected. Maybe you can spot the error? I started like this:

    package PpDumper; # PpDumper is a subclass of Data::Dumper, which provides # a custom _dump() method use Data::Dumper; our @ISA='Data::Dumper'; sub _dump { my ($self, $val, $name) = @_; print "my _dump called\n"; (ref($val) && $val->can('pp')) ? $val->pp($name) : $self->SUPER::_dump($val,$name); } sub PpDumper::Dumper { my ($class,@args) = @_; print "my own Dumper called\n"; $class->SUPER::Dumper(@args); } package main; .... my $h = { ... } { local $Data::Dumper::Useperl=1; print("3:\n",PpDumper->Dumper($h),"\n"); }
    When I run this, I see in the output of course my own Dumper called, but I don't see my _dump called being printed. This means that my custom _dump routine has not been called. This is strange: I verified that Data::Dumper::new (which is eventually called by Data::Dumper::Dumper) uses the two-argument form of bless, so my object should be of type PpDumper. Also, setting Useperl to 1 should ensure that no compiled components get in my way (I don't know whether this is necessary for overriding methods, so I put it there for the safe side).

    Where is the bug?

    -- 
    Ronald Fischer <ynnor@mm.st>
Re^2: Fine grain control over Data::Dumper
by Bloodnok (Vicar) on May 29, 2009 at 11:42 UTC
    That was my first thought, then I realised the presence of the leading _ (underscore) in _dump ... and we all know what that implies :-D

    or, put another way, overloading a private method may (and indeed probably will) result in problems down the line.

    A user level that continues to overstate my experience :-))
Re^2: Fine grain control over Data::Dumper
by rovf (Priest) on May 29, 2009 at 09:53 UTC

    This sounds interesting, and maybe I'll go this way. The main drawback here is that we are relying on an undocumented interface. In case we upgrade to a new version of Perl, with maybe a new verion of dumper, it might break our code. Still, I think I'll give it a try.

    -- 
    Ronald Fischer <ynnor@mm.st>