如何使用Pod :: Text :: Termcap输出带有数字的代码文本?

时间:2015-03-14 10:57:40

标签: perl

我正在尝试使用Pod::Text::Termcap将Pod作为文本输出到终端窗口。除了使用Pod C<>标签显示代码文本的一种情况外,它的效果非常好。通常,Pod::Text::Termcap以双引号显示代码文本,这很好。但是,如果我编写的代码文本只包含数字或后跟数字的美元符号,则不再引用代码文本。例如:

use strict;
use warnings;
use Pod::Text::Termcap;

my $pod_str = '=pod

This is a quoted name C<Peter>. The next text:  C<$1> , should also be quoted.

=cut';

my $parser = Pod::Text::Termcap->new();
my $str;
$parser->output_string( \$str );
$parser->parse_string_document( $pod_str );
print $str;

输出结果为:

  

这是一个引用的名字&#34; Peter&#34;。下一个文字:$ 1,也应该是   引述。

1 个答案:

答案 0 :(得分:2)

答案在于Pod::Text->cmd_c,这是由Russ Allbery在2001年写的,并且基本保持不变。该函数的评论解释了被认为不会从引用中获益的内容。

# Apply a whole bunch of messy heuristics to not quote things that don't
# benefit from being quoted.  These originally come from Barrie Slaymaker and
# largely duplicate code in Pod::Man.
sub cmd_c {
    my ($self, $attrs, $text) = @_;

    # A regex that matches the portion of a variable reference that's the
    # array or hash index, separated out just because we want to use it in
    # several places in the following regex.
    my $index = '(?: \[.*\] | \{.*\} )?';

    # Check for things that we don't want to quote, and if we find any of
    # them, return the string with just a font change and no quoting.
    $text =~ m{
      ^\s*
      (?:
         ( [\'\`\"] ) .* \1                             # already quoted
       | \` .* \'                                       # `quoted'
       | \$+ [\#^]? \S $index                           # special ($^Foo, $")
       | [\$\@%&*]+ \#? [:\'\w]+ $index                 # plain var or func
       | [\$\@%&*]* [:\'\w]+ (?: -> )? \(\s*[^\s,]\s*\) # 0/1-arg func call
       | [+-]? ( \d[\d.]* | \.\d+ ) (?: [eE][+-]?\d+ )? # a number
       | 0x [a-fA-F\d]+                                 # a hex constant
      )
      \s*\z
     }xo && return $text;

    # If we didn't return, go ahead and quote the text.
    return $$self{opt_alt}
        ? "``$text''"
        : "$$self{LQUOTE}$text$$self{RQUOTE}";
}

逻辑似乎是他们认为没有必要引用看起来像代码的东西。已引用的$variablesfunction_calls(),数字和代码。要获得明确答案,您可以发送电子邮件给Russ。

如果您想了解更多信息,您可以从他们的git存储库中收集一些信息,但是提交条目对决策没有任何影响。

commit d374354c961250d1ee23342451713313fd934eda
Author: Russ Allbery <rra@stanford.edu>
Date:   Tue Jul 10 10:54:27 2001 +0000

        * lib/Pod/Text.pm (seq_c): Add heuristics to decide whether or not
        to quote the argument of C<>.

        * lib/Pod/Text.pm: Added a LICENSE section to the documentation.