Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > How to print function with the code in the function body?

Reply
Thread Tools

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

 
 
Peng Yu
Guest
Posts: n/a
 
      12-05-2012
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:umper;

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
 
Reply With Quote
 
 
 
 
Rainer Weikusat
Guest
Posts: n/a
 
      12-05-2012
Peng Yu <(E-Mail Removed)> writes:
> 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:umper:eparse 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:eparse" 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:eparse".

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

use strict;
use warnings;

use Data:umper;

sub f {
return 0;
}

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

$Data:umper:eparse = 1;
print Dumper(\%hash_of_arrays);
 
Reply With Quote
 
 
 
 
J. Gleixner
Guest
Posts: n/a
 
      12-06-2012
On 12/05/12 09:36, Peng Yu wrote:
> 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:umper:eparse variable in the
Data:umper documentation.
 
Reply With Quote
 
Rainer Weikusat
Guest
Posts: n/a
 
      12-10-2012
Rainer Weikusat <(E-Mail Removed)> writes:

[...]

> 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:umper (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 :->.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
print a vs print '%s' % a vs print '%f' a David Cournapeau Python 0 12-30-2008 03:19 AM
Problem - I want to print Current Output of Pdf file and should print once.I get print dialog box but it is not working keto Java 0 05-30-2007 11:27 AM
Can't print to print server =?Utf-8?B?cGVjaw==?= Wireless Networking 2 02-03-2005 02:41 PM
Unlarging the print to print using PDF file to print Bun Mui Computer Support 3 09-13-2004 03:15 AM



Advertisments