为什么 Filter::Indent::HereDoc 抱怨 HereDoc 中间有空行
Why Filter::Indent::HereDoc complain when blank line in middle of HereDoc
我正在尝试 Filter::Indent::HereDoc,它允许缩进 HereDocument。这非常有用,能够让 HereDoc 随代码逻辑流动。
从上面link
When a 'here document' is used, the document text and the termination string must be flush with the left margin, even if the rest of the code block is indented.
Filter::Indent::HereDoc removes this restriction, and acts in a more DWIM kind of way - that if the terminator string is indented then that level of indent will apply to the whole document.
可以使用 sudo cpanm Filter::Indent::HereDoc
但是,我发现如果 HereDoc 中有空行,则会报错
Can't find string terminator "EOT" anywhere before EOF at....
当我删除空白行时不会发生这种情况。这是 MWE
#!/usr/bin/perl
use strict;
use warnings;
use Filter::Indent::HereDoc;
my $str = <<'EOT';
some text
some text
EOT
print $str;
以上运行没有问题,即使 space 在 EOT
之前存在(如果不使用 Filter::Indent::HereDoc
就会失败)
>perl t4.pl
some text
some text
>
现在我只是在中间插入一个空行,像这样
#!/usr/bin/perl
use strict;
use warnings;
use Filter::Indent::HereDoc;
my $str = <<'EOT';
some text
some text
EOT
print $str;
现在
>perl t4.pl
Can't find string terminator "EOT" anywhere before EOF at t4.pl line 6.
>
如果 EOT
之前的 space 现在被删除,那么它可以工作:
#!/usr/bin/perl
use strict;
use warnings;
use Filter::Indent::HereDoc;
my $str = <<'EOT';
some text
some text
EOT
print $str;
现在
>perl t4.pl
some text
some text
>
但是使用这个模块的整个想法是允许缩进 HereDoc。
知道发生了什么吗?
信息:
>perl --version
This is perl 5, version 18, subversion 2 (v5.18.2) built
for i686-linux-gnu-thread-multi-64int
If there is no terminator string (so the here document stops at the first blank line), then enough whitespace will be stripped out so that the leftmost character of the document will be flush with the left margin, e.g.
print <<; Hello, World! # This will print: Hello, World!
这是您在问题中引用的段落的正下方段落。 它在空行处停止是一个特性。