tags:

views:

29

answers:

1

Right now I can name the subroutine printargs as follows to get the dump.

perl -MO=Concise,printargs,-main,-terse Hello.pl

Assuming I have several subroutines, how can I build a generic module to dump details for all subroutines?

+1  A: 

To dump a single subroutine you can use

B::Concise::compile($sub)->()

where $sub is the reference to a sub.

If you know the list of the subs in advance, you are done, just do the above for each of them.

Otherwise, to get the list of existing subroutine names in a specific package, you can always walk the symbol table for that package:

no strict 'refs';
for my $k (keys %{"$pkgname\::"}) {
    if (*{${"$pkgname\::"}{$k}}{CODE}) {
        print "$k\n"; # sub name
    }
}

Update: my first line contained a mistake, which is fixed now. And here is the complete example script:

package Blah;

sub x { return "x"; }
sub hehe { print 2*2, "\n"; }
sub meme { die "ouch" }

our $varvar; # to illustrate the {CODE} thingy

package main;
use warnings;
use strict;
use B::Concise;

my $pkgname = "Blah";
no strict 'refs';
for my $k (keys %{"$pkgname\::"}) {
    if (*{${"$pkgname\::"}{$k}}{CODE}) {
        my $sub = \&{${"$pkgname\::"}{$k}};
        print "Dump of $pkgname\::$k():\n";
        B::Concise::compile($sub)->();
    }
}
Grrrr
@GrrrrCould you provide a full working example for the script you gave? Sorry, perl newbie here. [Suppose I have a file hello.pl, what should I do]?
DotDot