Data::Dumping code refs

ank on 2008-06-24T13:47:21

Just another reason to love Data::Dumper which is, in my opinion, the best module in perlland. Pay close attention.

Suppose you have this data structure:

my $ref = { a => sub { print "a"; } };

Data::Dumper dumps it as:

$VAR1 = { 'a' => sub { "DUMMY" } };

Because it doesn't print out sub references. But you can tell it to deparse them (through B::Deparse) by setting $Data::Dumper::Deparse:

use Data::Dumper;
$Data::Dumper::Deparse = 1;

my $ref = { a => sub { print "a"; } };
print Dumper $ref;

produces

$VAR1 = {
 'a' => sub {
  print 'a';
 }
};

I didn't know this feature existed, and was considering creating a new module to mix Data::Dumper and B::Deparse.


someone beat you to it

hdp on 2008-06-24T16:59:13

see Data::Dump::Streamer

Re:someone beat you to it

jjore on 2009-04-22T21:27:49

Yes, what he said. Data::Dump::Streamer will do Data::Dumper's job plus it'll also dump the closure's data too.

my %omg = (...);
sub ... { $omg{...} }
 
# Dumps %omg too because it is part of the data
Dump( \&... );