在 Perl 函数中打印自由格式文本?

发布于 2024-10-01 22:46:40 字数 340 浏览 3 评论 0原文

当尝试在 Perl 的子例程中打印自由格式文本时,我遇到了一个非常奇怪的错误。下面是我调用的代码

    print OUTFILE <<"HEADER";
The freeform text would go here
HEADER

奇怪的是,这只适用于我的函数的主要部分。一旦我将它放入函数调用中,我就会收到此错误:

Can't find string terminator "HEADER" anywhere before EOF

这意味着它无法找到标头,即使它在那里。不能在函数(子例程)中使用自由格式文本吗?

I have been getting a very odd error when trying to print freeform text in a subroutine in Perl. Below is the code I am calling

    print OUTFILE <<"HEADER";
The freeform text would go here
HEADER

The odd thing is that this only works in the main of my function. As soon as I place it in a function call, I get this error:

Can't find string terminator "HEADER" anywhere before EOF

Meaning it can't find the HEADER, even though it is there. Can you not use freeform text within a function (subroutine)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

薔薇婲 2024-10-08 22:46:40

确保结束字符串标识符(即 HEADER)之前没有空格/制表符/缩进。您的代码应如下所示:

        function someFunc(){
          print OUTFILE <<"HEADER";
      The freeform text would go here
HEADER
        }

请注意,在 HEADER 之前没有空格/制表符/缩进。它应该从该行的第一个字符开始。

查看本教程以获取更多信息:

引用:

要记住的重要规则是
你用同样的方法完成了一个here-doc
你开始的词,它必须是由
自己上线

Make sure that there is no space/tab/indentation before ending string identifier, that is HEADER. Your code should look like this:

        function someFunc(){
          print OUTFILE <<"HEADER";
      The freeform text would go here
HEADER
        }

Notice that there is no space/tab/indentation before HEADER there. It should start from first character of its line.

Check this tutorial out for more information:

Quoting:

The important rule to remember is that
you finish a here-doc using the same
word you started, and it must be by
itself on the line

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文