如何获取具有子程序的起始行和结束行号的perl子程序列表?

ani*_*nde -2 regex unix perl

我正在编写一个用于构建和升级分析的脚本,我需要使用它们的起始和结束行号来定义perl模块中定义的所有子例程.

实现这一目标的最佳方法是什么?

mob*_*mob 5

学习如何使用并不是一件容易的事PPI,但是一旦你这样做,用它PPI来解决这个问题是微不足道的.

use PPI;
$FILE = ...;
$doc = PPI::Document->new($FILE);
$subs = $doc->find('PPI::Statement::Sub');

foreach my $sub (@$subs) {
    my @t = $sub->tokens;
    #my $name = $t[2];  # usually good enough to get the sub name
    my $name = $sub->name;
    my $start = $t[0]->location->[0];
    my $end = $t[-1]->location->[0];
    print "$name => $FILE: $start - $end\n";
}
Run Code Online (Sandbox Code Playgroud)