我正在编写一个用于构建和升级分析的脚本,我需要使用它们的起始和结束行号来定义perl模块中定义的所有子例程.
实现这一目标的最佳方法是什么?
学习如何使用并不是一件容易的事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)
| 归档时间: |
|
| 查看次数: |
191 次 |
| 最近记录: |