Search code examples
perlparsingpackageextractsubroutine

How can I write a Perl script to extract the source code of each subroutine in a Perl package?


Given a Perl package Foo.pm, e.g.

package Foo;

use strict;

sub bar {
    # some code here 
}

sub baz {
    # more code here 
}

1;

How can I write a script to extract the textual source code for each sub, resulting in a hash:

$VAR1 = {
    'bar' => 'sub bar {
        # some code here 
    }',
    'baz' => 'sub baz {
        # more code here 
    }'
};

I'd like to have the text exactly as it appears in the package, whitespace and all.

Thanks.


Solution

  • PPI is kind of a pain to work with at the very first; the documentation is not good at telling you which class documents which methods shown in the examples. But it works pretty well:

    use strict;
    use warnings;
    use PPI;
    
    my %sub; 
    my $Document = PPI::Document->new($ARGV[0]) or die "oops";
    for my $sub ( @{ $Document->find('PPI::Statement::Sub') || [] } ) {
        unless ( $sub->forward ) {
            $sub{ $sub->name } = $sub->content;
        }
    }
    
    use Data::Dumper;
    print Dumper \%sub;