此处文档中的括号运算符作为参数?
Bracket operators inside here-document as argument?
我看到一个用作参数的here-document,里面有方括号运算符。它看起来像:
method(<<EOF)[0][0]
lots of text
EOF
方括号运算符的含义是什么?有谁知道那是什么成语吗?
问题是 heredocs 的结构有些混乱,破坏了正常的代码流。 heredoc 的内容从打开 heredoc 的 <<EOF
之后的行开始,到关闭 heredoc 的 EOF
结束,但是包含 heredoc 的表达式照常从左到右继续。
结构为:
method(<<EOF)[0][0]
lots of text
EOF
其实更像这样:
/<<EOF \
|lots of text|
|lots of text|
method|lots of text|[0][0]
|lots of text|
|lots of text|
\EOF /
其中的斜杠和竖线是粗略地尝试绘制非常高的 ASCII 艺术括号;或者,如果你有合适的 unicode 字体:
⎛<<EOF ⎞
⎜lots of text⎟
⎜lots of text⎟
method⎜lots of text⎟[0][0]
⎜lots of text⎟
⎜lots of text⎟
⎝EOF ⎠
您可以将 heredocs 视为一个看起来很有趣的双引号(或者 %Q(...)
,如果您愿意的话),它像您的代码的其余部分一样垂直而不是水平。
这样写(IMO)会更一致:
method(<<EOF
lots of text
EOF)[0][0]
但是 heredocs 有着悠久的历史(一直追溯到 /bin/sh
),我们一直坚持使用它。
回到真正的问题:该表达式的 [0][0]
部分根本不在 heredoc 中,它只是应用于 method("lots of text\nlots of text\n...")
returns.
我看到一个用作参数的here-document,里面有方括号运算符。它看起来像:
method(<<EOF)[0][0]
lots of text
EOF
方括号运算符的含义是什么?有谁知道那是什么成语吗?
问题是 heredocs 的结构有些混乱,破坏了正常的代码流。 heredoc 的内容从打开 heredoc 的 <<EOF
之后的行开始,到关闭 heredoc 的 EOF
结束,但是包含 heredoc 的表达式照常从左到右继续。
结构为:
method(<<EOF)[0][0]
lots of text
EOF
其实更像这样:
/<<EOF \
|lots of text|
|lots of text|
method|lots of text|[0][0]
|lots of text|
|lots of text|
\EOF /
其中的斜杠和竖线是粗略地尝试绘制非常高的 ASCII 艺术括号;或者,如果你有合适的 unicode 字体:
⎛<<EOF ⎞
⎜lots of text⎟
⎜lots of text⎟
method⎜lots of text⎟[0][0]
⎜lots of text⎟
⎜lots of text⎟
⎝EOF ⎠
您可以将 heredocs 视为一个看起来很有趣的双引号(或者 %Q(...)
,如果您愿意的话),它像您的代码的其余部分一样垂直而不是水平。
这样写(IMO)会更一致:
method(<<EOF
lots of text
EOF)[0][0]
但是 heredocs 有着悠久的历史(一直追溯到 /bin/sh
),我们一直坚持使用它。
回到真正的问题:该表达式的 [0][0]
部分根本不在 heredoc 中,它只是应用于 method("lots of text\nlots of text\n...")
returns.