如何使用 PPI 查找注释,然后在其前面插入代码?
我试图在 perl 源文件中找到注释 # VERSION
。然后我想在评论之前插入版本(或代替无关紧要)。谁能告诉我使用 PPI 执行此操作的正确方法?
之前,
use strict;
use warnings;
package My::Package;
# VERSION
...
之后
use strict;
use warnings;
package My::Package;
our $VERSION = 0.1;# VERSION
...
在维护最终结果中的 # VERSION
我实际上对如何查找 # VERSION 有一些想法,但其中一个是序列化 ppi 文档的正则表达式,这似乎不正确,并且另一个是在评论上使用 find_first ,但如果它不是第一个,我不知道该怎么做。
更新的代码 这似乎更接近正确的解决方案,因为它只查看注释。但我不确定如何使用或真正如何创建新变量。
#!/usr/bin/env perl
use 5.012;
use strict;
use warnings;
use PPI;
my $ppi = PPI::Document->new('test.pm');
my $comments = $ppi->find('PPI::Token::Comment');
my $version = PPI::Statement::Variable->new;
foreach ( @{$comments} ) {
if ( /^\s*#\s+VERSION\b$/ ) {
$_->replace($version);
}
}
更新
这个问题的答案成为DZP::OurPkgVersion
I'm trying to find the comment # VERSION
in a perl source file. I then want to insert the version before the comment (or in place of doesn't matter). Could anyone tell me the right way to do this with PPI?
before
use strict;
use warnings;
package My::Package;
# VERSION
...
after
use strict;
use warnings;
package My::Package;
our $VERSION = 0.1;# VERSION
...
maintaining the # VERSION
in the end result is optional
I actually have a couple of ideas on how to find # VERSION but one is a regex of a serialized ppi doc which doesn't seem right, and the other is using find_first on a Comment but if it's not the first I'm not sure what to do.
Updated code This seems closer to a correct solution since it only looks at the comments. but I'm not sure how to use or really how to create a new variable.
#!/usr/bin/env perl
use 5.012;
use strict;
use warnings;
use PPI;
my $ppi = PPI::Document->new('test.pm');
my $comments = $ppi->find('PPI::Token::Comment');
my $version = PPI::Statement::Variable->new;
foreach ( @{$comments} ) {
if ( /^\s*#\s+VERSION\b$/ ) {
$_->replace($version);
}
}
UPDATE
The answer to this question became the foundation for DZP::OurPkgVersion
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这里有一些代码可以完成您所描述的操作 - 无论如何它都会让您开始。它是从 Catalyst::Helper::AuthDBIC (来源),这是使用 PPI 的完整示例(尽管其中一些可能不是最佳实践):
我想在您的情况下,您会获取 $comments arrayref 并修改与 /VERSION/ 匹配的第一个项目与替换内容。
这是海报提供的最终代码:
Here's some code that does something like what you describe - It'll get you started anyway. It's edited from Catalyst::Helper::AuthDBIC (source), which is a full example of working with PPI (although bits of it may not be best practices):
I suppose in your case you grab the $comments arrayref and modify the first item that matches /VERSION/ with the replacement content.
And here's the final code courtesy of the poster: