Perl 的标量逗号运算符什么时候有用?
除了在 for 循环之外,是否有任何理由在任何地方使用标量逗号运算符?
Is there any reason to use a scalar comma operator anywhere other than in a for
loop?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
由于 Perl 标量逗号是 C 逗号运算符的“端口”,这些注释 可能是恰当的:
因此,我认为您的直觉认为它主要在
for
循环中有用,这是一个很好的直觉。Since the Perl scalar comma is a "port" of the C comma operator, these comments are probably apropos:
So, your instinct that it's mainly useful in
for
loops is a good one, I think.我偶尔会在条件(有时错误地称为“三元”)运算符中使用它,如果代码比将其分解为真正的 if/else 更容易阅读:
它也可以在最后结果很重要的某些表达式中使用,例如在
grep
表达式中,但在这种情况下,它与使用分号相同:I occasionally use it in the conditional (sometimes erroneously called "the ternary") operator, if the code is easier to read than breaking it out into a real if/else:
It could also be used in some expression where the last result is important, such as in a
grep
expression, but in this case it's just the same as if a semicolon was used: