in reply to How to get code of the calling function or object

In some cases you could use B::Deparse, e.g. via Data::Dumper:

use Data::Dumper; $Data::Dumper::Deparse = 1; sub foo { my ($foo) = @_; print "foo called with $foo\n"; print "going to call bar()...\n"; bar(); } sub bar { my $caller = (caller 1)[3]; print Dumper \&$caller; } foo(42);
$ ./895386.pl foo called with 42 going to call bar()... $VAR1 = sub { my($foo) = @_; print "foo called with $foo\n"; print "going to call bar()...\n"; bar(); };

Replies are listed 'Best First'.
Re^2: How to get code of the calling function or object
by LanX (Saint) on Mar 25, 2011 at 14:28 UTC
    Why not using B::Deparse::coderef2text directly?

    I don't know if it was already mentioned, but that's only a backengineering of the code after constant folding and not the original source.

    IIRC it should be possible to derive the linenumer and filename of the sub's definition. (UPDATE:see here)

    Cheers Rolf