什么是打印<<EOF;它是如何工作的? [英] What is print <<EOF; and how is it working?

查看:36
本文介绍了什么是打印<<EOF;它是如何工作的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能的重复:
用 <<'ESQ'

https://stackoverflow.com/questions/4151279/perl-print-eof<中的声明是什么/a> 到底在做什么?我看到了之前的帖子,但不明白他想解释什么.那个 PETE 是什么?任何人都可以解释每一行吗?代码是如何工作的?

What is the statement in https://stackoverflow.com/questions/4151279/perl-print-eof doing exactly? I came across the previous post but didn't understand what he is trying to explain. What is that PETE? Can anyone explain every line? How is the code is working?

print <<EOF;
This is
a multiline
string
EOF

print <<PETE;
This is
a multiline
string
PETE

这两者有什么区别和相似之处?代替 PETE,我使用了许多其他词,例如 DOG,而且每次都一样.

What is the difference and similarity between these two? In place of PETE I have used many other words like DOG and it works the same every time.

推荐答案

这称为 here-doc.它基本上抓取从下一行到结束标记行的所有内容,并将其作为标准输入呈现给您正在运行的程序.结束标记行由 <<.

This is called a here-doc. It basically grabs everything from the next line up until an end marker line and presents that as standard input to the program you're running. The end marker line is controlled by the text following the <<.

例如,在 bash(我比 Perl 更熟悉)中,命令:

As an example, in bash (which I'm more familiar with than Perl), the command:

cat <<EOF
hello
goodbye
EOF

将运行 cat,然后向其标准输入发送两行(hello 和 goodbye 行).Perl 也有这个特性,虽然语法略有不同(正如你所期望的,因为它是一种不同的语言).尽管如此,它的解释仍然足够接近.

will run cat and then send two lines to its standard input (the hello and goodbye lines). Perl also has this feature though the syntax is slightly different (as you would expect, given it's a different language). Still, it's close enough for the explanation to still hold.

维基百科有一个条目,如果你知道它被称为here-doc,你可能会发现它,但否则就很难弄清楚.

Wikipedia has an entry for this which you probably would have found had you known it was called a here-doc, but otherwise it would be rather hard to figure it out.

在您的特定情况下,使用 EOFPETE 之间没有区别,heredoc 标记之间存在关系(<<) 和标准输入的结束.

In your particular cases, there is no difference between using EOF and PETE, there's a relationship between the heredoc marker (the bit following <<) and the end of standard input.

例如,如果您的输入行之一是 EOF,您就不能真正将其用作标记,因为标准输入会过早终止:

For example, if one of your input lines was EOF, you couldn't really use that as a marker since the standard input would be terminated prematurely:

cat <<EOF
This section contains the line ...
EOF
but then has more stuff
and this line following is the real ...
EOF

在这种情况下,您可以使用 PETE(或任何其他未出现在文本中的任何其他行).

In that case, you could use PETE (or anything else that doesn't appear in the text on its own line).

还有其他选项,例如在标记周围使用引号(因此缩进看起来更好)以及使用单引号或双引号来控制变量替换.

There are other options such as using quotes around the marker (so the indentation can look better) and the use of single or double quotes to control variable substitution.

如果您转到 perlop 页面并搜索 <<EOF,希望一切都变得清晰.

If you go to the perlop page and search for <<EOF, it will hopefully all become clear.

这篇关于什么是打印&lt;&lt;EOF;它是如何工作的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆