我正在尝试提取 Perl POD 的一部分,而不是其他任何内容。如果我运行以下命令:
use strict;
use warnings;
use Pod::Simple;
use Pod::Text;
=head1 podTest
normal pod
=cut
print "normalPod:\n";
my $writeNormalPod = Pod::Text->new();
$writeNormalPod->parse_from_file(__FILE__);
=pod
all pod
=cut
print "\nallPod:\n";
my $writePod = Pod::Text->new();
$writePod->accept_targets( ('special') );
$writePod->parse_from_file(__FILE__);
=begin special
special pod
=end special
=cut
print "\nspecialPod:\n";
my $writeSpecialPod = Pod::Text->new();
# what to have here?
$writeSpecialPod->parse_from_file(__FILE__);
# in order to print "special pod" and nothing else
然后我得到输出:
normalPod:
podTest
normal pod
all pod
allPod:
podTest
normal pod
all pod
special pod
specialPod:
我怎样才能获得特殊 Pod
而没有其他东西?
我尝试过的事情:
- 不接受代码、指令和目标
- 附加代码(以及剪切、pod 和白线)处理程序
- 网络搜索(最多可以显示如何使用格式名称)
有没有一种简单的方法可以从 POD 中仅获取特定格式,或者我应该自己解析该文件?
最佳答案
Things I've tried: unaccepting directives
是的,似乎Pod::Simple
不支持不接受标准指令。如果你尝试:
$writePod->unaccept_directives( 'head1' );
它终止并显示错误消息:
But you must accept "head1" directives -- it's a builtin!
您也许可以通过子类化 Pod::Text
来覆盖其输出方法来解决该限制。例如:
p.pl:
BEGIN {
package My::Pod::Text;
use strict;
use warnings;
use parent qw(Pod::Text);
sub cmd_head1 {} #override this to not print head1
sub cmd_para {} #override this to not print paragraphs
$INC{"My/Pod/Text.pm"} = 1;
}
package main;
use feature qw(say);
use strict;
use warnings;
use My::Pod::Text;
=head1 podTest
normal pod
=begin special
special pod
=end special
=cut
my $writePod = My::Pod::Text->new();
$writePod->accept_targets( ('special') );
$writePod->parse_from_file(__FILE__);
仅输出:
special pod
关于perl - 如何在 Perl POD 中仅打印特定格式名称?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71296476/