Stories
Slash Boxes
Comments
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

use Perl Log In

Log In

[ Create a new account ]

Ovid (2709)

Ovid
  (email not shown publicly)
http://publius-ovidius.livejournal.com/
AOL IM: ovidperl (Add Buddy, Send Message)

Stuff with the Perl Foundation. A couple of patches in the Perl core. A few CPAN modules. That about sums it up.

Journal of Ovid (2709)

Tuesday April 24, 2007
05:02 AM

Sub::Introspection

[ #33089 ]

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 Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
 Full
 Abbreviated
 Hidden
More | Login | Reply
Loading... please wait.
  • 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