perl - 如何在 Perl POD 中仅打印特定格式名称?

标签 perl perl-pod perldoc

我正在尝试提取 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/

相关文章:

perl - 如何查找数组是否具有列表中未找到的值

ios - 将 iOS .strings 文件解析为 perl 哈希

linux - 一个用于计算按键排序的字段的平均值的单行?

perl - 如何生成目录中 Perl 模块或脚本的 HTML 摘要?

perl - perldoc perlxxx 选项是什么?

regex - 用正则表达式提取单词

perl - 使用 pod2html 生成带有自定义文本的 http 链接的正确语法是什么?

perl - 在线Perl POD渲染器

perl - 如何简洁地记录 Perl 代码中的方法?

perl - 如何在 Pod 和 perldoc 中使用 Unicode 字符?