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

Discussion in 'Perl Misc' started by Peng Yu, Dec 5, 2012.

  1. Peng Yu

    Peng Yu Guest

    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
    Peng Yu, Dec 5, 2012
    #1
    1. Advertising

  2. Peng Yu <> 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::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);
    Rainer Weikusat, Dec 5, 2012
    #2
    1. Advertising

  3. Peng Yu

    J. Gleixner Guest

    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::Dumper::Deparse variable in the
    Data::Dumper documentation.
    J. Gleixner, Dec 6, 2012
    #3
  4. Data::Dumper (was: How to print function with the code in the function body?)

    Rainer Weikusat <> 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::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 :->.
    Rainer Weikusat, Dec 10, 2012
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. keto
    Replies:
    0
    Views:
    907
  2. David Cournapeau

    print a vs print '%s' % a vs print '%f' a

    David Cournapeau, Dec 30, 2008, in forum: Python
    Replies:
    0
    Views:
    337
    David Cournapeau
    Dec 30, 2008
  3. London Boy
    Replies:
    2
    Views:
    318
    Keith Bowes
    Jan 12, 2004
  4. Replies:
    5
    Views:
    200
    A. Sinan Unur
    Sep 8, 2005
  5. Replies:
    18
    Views:
    527
Loading...

Share This Page