in reply to Is it possible to view a string representaion of a code ref?

Several:

use Data::Dumper; $Data::Dumper::Deparse = 1; my $sub; { my $x = "foo"; $sub = sub { my $filename = shift; return "/tmp/$x/" . $filename; }; } print Dumper($sub); __END__ $VAR1 = sub { my $filename = shift @_; return "/tmp/$x/" . $filename; };
use Data::Dump::Streamer; my $sub; { my $x = "foo"; $sub = sub { my $filename = shift; return "/tmp/$x/" . $filename; }; } print Dump($sub); __END__ my ($x); $x = 'foo'; $CODE1 = sub { my $filename = shift @_; return "/tmp/$x/" . $filename; };
use B::Deparse; my $deparse = B::Deparse->new; my $sub; { my $x = "foo"; $sub = sub { my $filename = shift; return "/tmp/$x/" . $filename; }; } print $deparse->coderef2text($sub),"\n"; __END__ { my $filename = shift @_; return "/tmp/$x/" . $filename; }

update: added a block scope, from which you can see that out of those only Data::Dump::Streamer provides the sub's context.

Replies are listed 'Best First'.
Re^2: Is it possible to view a string representaion of a code ref?
by targetsmart (Curate) on May 14, 2009 at 05:58 UTC
    Only the B::Deparse solution seems to work in my case
    Active perl 5.6.1 build 635, MSWin32-x86-multi-thread
    Data::Dumper solution is still printing "DUMMY", i don't have Data::Dump::Streamer installed so I have not tested it

    Vivek
    -- In accordance with the prarabdha of each, the One whose function it is to ordain makes each to act. What will not happen will never happen, whatever effort one may put forth. And what will happen will not fail to happen, however much one may seek to prevent it. This is certain. The part of wisdom therefore is to stay quiet.
        Upgrading Active perl helped. i could see the ref code
        now i have Active perl 5.10.0 built 1004 on MSWin32
        why is it like this, what internal change in 5.10.0 could have made this possible?

        Vivek
        -- In accordance with the prarabdha of each, the One whose function it is to ordain makes each to act. What will not happen will never happen, whatever effort one may put forth. And what will happen will not fail to happen, however much one may seek to prevent it. This is certain. The part of wisdom therefore is to stay quiet.
Re^2: Is it possible to view a string representaion of a code ref?
by astroboy (Chaplain) on May 14, 2009 at 00:22 UTC
    Ah, very cool. Thanks ;-)