Re: How to get code of the calling function or object
by Eliya (Vicar) on Mar 25, 2011 at 05:40 UTC
|
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();
};
| [reply] [d/l] [select] |
|
|
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)
| [reply] [d/l] |
Re: How to get code of the calling function or object
by BrowserUk (Patriarch) on Mar 25, 2011 at 05:55 UTC
|
use Data::Dump::Streamer;
## Calling this sub
sub callersCode{
my $d=Data::Dump::Streamer->new;
my @i=caller(1);
$d->Deparse(1);
return scalar $d->Dump( *{$i[3]} )->Out();
};;
## from this one
sub fred {
my( @x ) = @_;
for ( @x ) {
m[a] and s[a][A]g;
}
print callersCode();
};;
## When called
fred( 1,2,3 );;
## this output is produced
$VAR1 = *::fred;
*::fred = sub {
BEGIN {
$^H{'feature_say'} = q(1);
$^H{'feature_state'} = q(1);
$^H{'feature_switch'} = q(1);
}
my(@x) = @_;
foreach $_ (@x) {
s/a/A/g if /a/;
}
print callersCode();
};
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
| [reply] [d/l] |
|
|
Just read that Data::Dumper dumps Perl data structures does this mean that code itself is a data structure. If yes can we modify this code properly and feed back to the Interpreter?
| [reply] |
|
|
Just read that Data::Dumper dumps Perl data structures does this mean that code itself is a data structure.
Internally, code is held in data structures, but these are not standard Perl visible data structures like hashes or arrays, so you cannot easily manipulate them from Perl.
DDS just encapsulates B:;:Deparse to retrieve the source code.
can we modify this code properly and feed back to the Interpreter?
The return from DDS is a string. Perl can edit strings. Perl can also eval strings. So, you certainly could, edit the subroutine code and then eval (ie. reparse) the edited string to dynamically overlay the original subroutine.
Also, I believe that it is possible to use B::Deparse directly to modify the AST of a subroutine on the fly. I did look at this once (briefly) with the idea that it might be possible to extract the body of a subroutine and 'inline it', at the call sites to provide a macro facility. A nice idea, but fraught with complexities. It took very little reading before I gave up on the idea.
And remember, just because you can doesn't mean you should!.
I'm not shy of pushing at the boundaries, but my experience of using self-modifying code from way back when, is that it is a very bad idea. Imagine what happens when things go awry. Something failed, but you don't know what source code was being run when it went wrong. Was it that the editing process screwed up and produced valid, but unintended wrong code? Or Did it produce code as designed, but the design is wrong?
What happens if you have 2, 3 or half a dozen routines that optionally, dynamically self-modify, and interact to cause failures. But which combination of original and modified routines interact to cause that failure? It's a combinatorial nightmare.
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
| [reply] |
|
|
|
|
|
|
|
|
| [reply] |
Re: How to get code of the calling function or object
by Your Mother (Archbishop) on Mar 25, 2011 at 05:44 UTC
|
This might not be quite right but might offer a jump off. Update: Eliya's answer and example is better.
use warnings;
use strict;
my $hai = O::HAI->new({ arg => "KTHX" });
BEGIN {
package O::HAI;
use B::Deparse;
use Moose;
has "arg" => isa => "Str", is => "ro";
sub BUILD {
my ( $package, $filename, $line, $subroutine ) = caller(4);
my $sub_ref = do {
no strict "refs";
\&{join("::",$package,$subroutine)};
};
my $deparse = B::Deparse->new("-p", "-sC");
my $caller = $deparse->coderef2text($sub_ref);
print $caller, $/;
}
}
{
package Moose::Object;
use warnings;
use strict 'refs';
(my $class = shift(@_));
(my $real_class = (&Scalar::Util::blessed($class) || $class));
(my $params = $real_class->BUILDARGS(@_));
return('Class::MOP::Class'->initialize($real_class)->new_object($p
+arams));
}
| [reply] [d/l] [select] |
Re: How to get code of the calling function or object
by Khen1950fx (Canon) on Mar 26, 2011 at 08:47 UTC
|
#!perl
use Modern::Perl;
use Data::Dump::Streamer;
use Sub::Information as => 'peek';
my $info = peek(\&fred);
say "Name: ", $info->name;
say "Package: ", $info->package;
say "Filename: ", $info->file;
say "Address: ", $info->address;
say "Starts at line: ", $info->line;
say "Variables: ", Dump($info->variables);
say $info->code;
say $info->dump;
sub fred {
my(@x) = @_;
foreach (@x) {
s/a/A/g if /a/;
}
};
| [reply] [d/l] |
Re: How to get code of the calling function or object
by LanX (Saint) on Mar 25, 2011 at 15:09 UTC
|
I suppose your intention is only manual debugging.
my @call=caller(0);
print join ":",$call[1],$call[2],"\n";
Prints path and linenumber of the call!
With this formating my emacs in cperl-mode even allows to click on the output and to jump into the addressed line of code.
| [reply] [d/l] |
Re: How to get code of the calling function or object
by locked_user sundialsvc4 (Abbot) on Mar 25, 2011 at 05:03 UTC
|
As far as I know, there is not. Your mileage may vary.
| |
|
|
| [reply] |
|
|
Did you bother to read the documentation you link to? caller_cv returns a code ref - not the actual code.
| [reply] [d/l] |