代码之家  ›  专栏  ›  技术社区  ›  Teepeemm

如何在Perl POD中只打印特定的格式名称?

  •  1
  • Teepeemm  · 技术社区  · 4 年前

    我试图提取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:
    

    我怎么能得到 special pod 什么都没有?

    我尝试过的事情:

    • 不接受代码、指令和目标
    • 附加代码(以及cut、pod和白线)处理程序
    • Web搜索(充其量,这会显示如何使用格式名称)

    有没有一种简单的方法可以从POD中只获取特定的格式,或者我应该自己解析文件?

    0 回复  |  直到 4 年前
        1
  •  3
  •   Håkon Hægland    4 年前

    我尝试过的事情:不接受指令

    是的,看起来 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
    
        2
  •  1
  •   Nathan Mills    4 年前

    如果你可以使用CPAN模块,下面是如何获得特殊的内容 =begin 具有的部分 Pod::POM 这个 findBegin 子程序查找所有节点,以找到一个具有特殊格式的开始。它将根节点的子节点推送到堆栈上,并使用 pop ,如果该节点是特殊的开始块,则将其推到结果数组上,然后将该节点的子节点推到堆栈上,然后返回到弹出节点。最后返回结果。

    print "Pod::POM results\n";
    my $parser = Pod::POM->new();
    print "\$parser is $parser\n";
    
    my $pom = $parser->parse_file(__FILE__);
    
    #print "\$pom is \"$pom\"\n";
    my (@special) = findBegin($pom);
    
    for my $special (@special) {
        print $special->text();
    }
    
    sub findBegin {
        my ($pom) = @_;
        my @nodes = $pom->content();
        my @results;
        while (@nodes) {
            my $node = pop @nodes;
            if ($node->type() eq 'begin' && $node->format() eq 'special') {
                #print "Returning $node\n";
                push @results, $node;
            }
            my @children = $node->content();
            push @nodes, @children if scalar @children;
        }
        return @results;
    }
    

    您可以扩展findBegin,这样您就可以通过更改的第一行来为每次调用使用不同的条件 findBegin

    my ($pom, $callback) = @_;
    

    以及if语句:

    if ($callback->($node)) {
    

    然后将其称为:

    my (@special) = findBegin($pom, sub {
        my ($node) = @_;
        return $node->type() eq 'begin' && $node->format() eq 'special';
    });
    

    将return语句中的条件替换为要测试的任何条件 $node

    推荐文章