in reply to Re: Printing the code of a coderef
in thread Printing the code of a coderef

Very helpful, but I get a weird nested structure as output, though it would be equivalent to the original code:
{ do { do { print "some stuff\n" } }; }
Is there any way I can get it to print without the excessive do's?

Replies are listed 'Best First'.
Re^3: Printing the code of a coderef
by sgt (Deacon) on Jun 26, 2007 at 13:08 UTC

    the new generation of data dumping does streamline this using B::Deparse

    % steph@apexPDell2 (/home/stephan/t1) % % date Tue Jun 26 15:04:14 2007 % steph@apexPDell2 (/home/stephan/t1) % % date; cat Some/Module.pm Tue Jun 26 15:04:27 2007 package Some::Module; sub somecode { do { print "some stuff\n" } } 1; % steph@apexPDell2 (/home/stephan/t1) % % perl -MDDS -MSome::Module -e 'Dump(\&Some::Module::somecode)'; date $CODE1 = sub { package Some::Module; do { do { print "some stuff\n" } }; }; Tue Jun 26 15:04:32 2007
    cheers --stephan
Re^3: Printing the code of a coderef
by naikonta (Curate) on Jun 26, 2007 at 14:14 UTC
    I guess that's the best we can get now. I don't know what B::Deparse exactly does, but it seems to handle the do statement specially. As shown in another reply with Sub::Information, the result is the same with with Data::Dump::Streamer which in turn also uses B::Deparse. Consider and compare with the following code.
    use Data::Dump::Streamer; sub somecode { my $x = shift; if ($x) { print "x ok\n"; } else { print "what do you want?\n"; } return 1; } print Dump(\&somecode); __END__ # result: $CODE1 = sub { my $x = shift @_; if ($x) { print "x ok\n"; } else { print "what do you want?\n"; } return 1; };

    Open source softwares? Share and enjoy. Make profit from them if you can. Yet, share and enjoy!