When you're in the habit of using a lot of anonymous subroutines, debugging becomes a pain. I think I need to write this (or you can, if you want. Most of this shouldn't be too hard).
use Sub::Introspection as => 'peek';
my $sub = sub {
my $x = shift;
my $y = 3;
return $x + y;
};
*adder = $sub;
peek($sub);
print $sub->(3); # prints 5 (original behavior)
print $sub->name; # prints 'main::__ANON__';
print $sub->slot; # prints 'main::adder' (if assigned to a slot)
print $sub->code; # Uses something like Data::Dumper::Streamer
foreach ( my $var = $sub->vars ) {
print $var->name;
print $var->value;
}
The main problem I see is that I cannot figure out how to write the 'slot' method and that's what I really need right now.
Update: I do realize that blessing the subref directly which tries to use the 'ref' function, so in scalar context it could return a new 'Sub::Introspection' object.
The main problem I see is that I cannot figure out how to write the 'slot' method and that's what I really need right now.
You can use B to fetch the info out of the STASH for the code (I do this in Class::MOP, specifially Class::MOP::Method) but perl wont attach the new stash info into code refs in all cases (I can't recall which at this moment though). I use Sub::Name to assure that methods added through Class::MOP get all their proper stash info set up.
- Stevan