How to print function with the code in the function body?


P

Peng Yu

Hi,

I try to use Dumper to print a hash which has a function ref as one of
its value. But I only can get "sub { "DUMMY" }", which is not helpful
for me to know what this function is. Is there a way to somehow print
the function body?


~/linux/test/perl/library/Data/Dumper/Dumper$ cat main_fun.pl
#!/usr/bin/perl

use strict;
use warnings;

use Data::Dumper;

sub f {
return 0;
}

my %hash_of_arrays = (
a => \&f,
);

print Dumper(\%hash_of_arrays);
~/linux/test/perl/library/Data/Dumper/Dumper$ ./main_fun.pl
$VAR1 = {
'a' => sub { "DUMMY" }
};

Regards,
Peng
 
Ad

Advertisements

R

Rainer Weikusat

Peng Yu said:
I try to use Dumper to print a hash which has a function ref as one of
its value. But I only can get "sub { "DUMMY" }", which is not helpful
for me to know what this function is. Is there a way to somehow print
the function body?

In many cases, consulting the fine manual can be helpful:

o $Data::Dumper::Deparse or $OBJ->Deparse([NEWVAL])

Can be set to a boolean value to control whether code
references are turned into perl source code. If set to a
true value, "B::Deparse" will be used to get
the source of the code reference. Using this option will
force using the Perl implementation of the dumper, since
the fast XSUB implementation doesn't
support it.

Caution : use this option only if you know that your
coderefs will be properly reconstructed by "B::Deparse".

--------------
#!/usr/bin/perl

use strict;
use warnings;

use Data::Dumper;

sub f {
return 0;
}

my %hash_of_arrays = (
a => \&f,
);

$Data::Dumper::Deparse = 1;
print Dumper(\%hash_of_arrays);
 
J

J. Gleixner

Hi,

I try to use Dumper to print a hash which has a function ref as one of
its value. But I only can get "sub { "DUMMY" }", which is not helpful
for me to know what this function is. Is there a way to somehow print
the function body?
[...]

See the $Data::Dumper::Deparse variable in the
Data::Dumper documentation.
 
Ad

Advertisements

R

Rainer Weikusat

[...]
Using this option will force using the Perl implementation of the
dumper, since the fast XSUB implementation doesn't support it.

I'm using Data::Dumper (from perl versions 5.10.0 and 5.10.1) do
serialize a set of 'database' into text files (composed of a reference
to an array holding references to objects containg 'plain' data
members and other objects). Some of the older code also uses the
'Freezer' mechanism to transform the 'live' objects into a state
suitable for serialization (by moving transient data items 'out of the
way'). I stopped using the XS implementation mid June this year
because using it would reproducibly cause memory corruption inside the
perl process which ultimatively lead to a segmentation fault
('crash') when serializing 'large' databases (with some hundreds or a
few thousands of array entries). This was a descision I made after
looking at the code of the 'fast' implementation --- debugging that,
especially considering that this has required experimenting with busy
customer installations, didn't seem feasible (this was the kind of
code one can use provided it works but would be better advised to
replace wholesale than trying to work out what it actually does in
case of any problems).

(In general, any 'optimized' implementation of anything should be
avoided --- no amount of 'test cases' will even come close to the
diversity of actual 'use cases' and third-party provided libraries
with a hideously complicated implementation are always a
liability[*]).


[*] That's the "make it so complicated that it has no obvious bugs"
part :->.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top