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

Hello Monks,

I'm trying to use the Seen() method of Data::Dumper to make coderefs show as something more useful than sub { "DUMMY" }, but I'm not having any luck, and I don't understand the example in the documentation - can anyone help me out?

use Data::Dumper; $foo = sub {return []}; $bar = {arefpromise => $foo}; $d = Data::Dumper->new([$bar]); print "foo is a coderef $foo returning ".$foo->()."\n"; $d->Seen ({$foo => $foo->()}); print $d->Dump();

outputs:

foo is a coderef CODE(0x8154684) returning ARRAY(0x8153c28) $VAR1 = { 'arefpromise' => sub { "DUMMY" } };


Basically, in the output of Data::Dumper I'd like to get the thing a coderef would return, or maybe the string overload of the package of $foo - in fact, wouldn't 'arefpromise' => sub { "CODE(0x8154684)" } be a more useful default?

Thanks!

Replies are listed 'Best First'.
Re: using Data::Dumper->Seen() with coderefs?
by tilly (Archbishop) on Jan 11, 2009 at 00:54 UTC
    You need to set $Data::Dumper::Deparse to a true value.
    use Data::Dumper; $Data::Dumper::Deparse = 1; $foo = sub {return []}; $bar = {arefpromise => $foo}; $d = Data::Dumper->new([$bar]); print "foo is a coderef $foo returning ".$foo->()."\n"; $d->Seen ({$foo => $foo->()}); print $d->Dump();
    will print
    foo is a coderef CODE(0x801734) returning ARRAY(0x801944) $VAR1 = { 'arefpromise' => sub { return []; } };