为什么在 Perl 中打印后不能立即获得文字列表切片?

发布于 2024-08-10 09:10:17 字数 422 浏览 5 评论 0原文

我发现我可以做这样的事情:

print STDOUT (split /\./, 'www.stackoverflow.com')[1];

并打印“stackoverflow”。但是, this: 的

print +(split /\./, 'www.stackoverflow.com')[1];

作用相同,并且 this:

print (split /\./, 'www.stackoverflow.com')[1];

是一个语法错误。那么这里究竟发生了什么?我一直认为一元加号在任何情况下都不执行任何操作。如果“print FILEHANDLE EXPR”有效,我会想象“print EXPR”总是同样有效。有什么见解吗?

I see I can do something like this:

print STDOUT (split /\./, 'www.stackoverflow.com')[1];

and "stackoverflow" is printed. However, this:

print +(split /\./, 'www.stackoverflow.com')[1];

does the same, and this:

print (split /\./, 'www.stackoverflow.com')[1];

is a syntax error. So what exactly is going on here? I've always understood the unary plus sign to do nothing whatsoever in any context. And if "print FILEHANDLE EXPR" works, I would have imagined that "print EXPR" would always work equally well. Any insights?

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

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

发布评论

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

评论(2

我ぃ本無心為│何有愛 2024-08-17 09:10:17

您没有启用警告。在 print(...)[1] 情况下,括号集被视为函数语法的一部分。

print (...) interpreted as function at C:\Temp\t.pl line 4.

来自,perldoc -f print

另请注意,不要在 print 关键字后面加上左括号,除非您希望相应的右括号终止打印的参数 - 插入 + 或在所有参数两边加上括号。< /p>

另请参阅 为什么不打印换行符这个 Perl 代码?

You do not have warnings enabled. In the print(...)[1] case, the set of parentheses are regarded as part of the function syntax.

print (...) interpreted as function at C:\Temp\t.pl line 4.

From, perldoc -f print:

Also be careful not to follow the print keyword with a left parenthesis unless you want the corresponding right parenthesis to terminate the arguments to the print—interpose a + or put parentheses around all the arguments.

See also Why aren't newlines being printed in this Perl code?

┾廆蒐ゝ 2024-08-17 09:10:17

printperldoc 包含以下内容:

还要注意 print 关键字后面不要跟有
左括号,除非您想要相应的右括号
括号终止 print--interpose 的参数
“+”或将所有参数放在括号内。

print 始终在 LIST 上下文中计算其参数。

说一下

print (split /\./, 'www.stackoverflow.com')

就可以了。但是,当您说

print (split /\./, 'www.stackoverflow.com')[0]

解析器在看到第一个 ( 后需要一个 LIST,并在看到结束的 ) 时认为 LIST 是完整的。 [0] 不会被解释为对任何内容进行操作,因此您会收到语法错误。

print "abc","def";       # prints "abcdef"
print ("abc","def");     # prints "abcdef"
print ("abc"), "def";    # prints "abc"


其他可以将 LIST 作为第一个参数的 Perl 函数的行为方式相同:

warn ($message),"\n"   # \n not passed to warn, line # info not suppressed

system ("echo"),"x"    # not same as system("echo","x") or system "echo","x"
                       #    or system(("echo"),"x")

perldoc for print includes this nugget:

Also be careful not to follow the print keyword with
a left parenthesis unless you want the corresponding right
parenthesis to terminate the arguments to the print--interpose
a "+" or put parentheses around all the arguments.

print always evaluates its arguments in LIST context.

To say

print (split /\./, 'www.stackoverflow.com')

is ok. But when you say

print (split /\./, 'www.stackoverflow.com')[0]

the parser expects a LIST after it sees the first (, and considers the LIST to be complete when it sees the closing ). The [0] is not interpreted as operating on anything, so you get a syntax error.

print "abc","def";       # prints "abcdef"
print ("abc","def");     # prints "abcdef"
print ("abc"), "def";    # prints "abc"


Other Perl functions that can take a LIST as the first argument behave the same way:

warn ($message),"\n"   # \n not passed to warn, line # info not suppressed

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