为什么 Perl 不让我从后缀比较中链接后缀循环?
这可以:
$foo++ if $condition;
这也可以:
$foo++ for (1..10);
但这不是:
$foo++ if $condition for (1..10);
如果事情不复杂,我发现后者非常可读,而且它适合一行! 有办法做到这一点还是我应该继续我的生活?
This is ok:
$foo++ if $condition;
And this is ok:
$foo++ for (1..10);
But this isn't:
$foo++ if $condition for (1..10);
I find the latter quite readable if things aren't complicated, and it fits on one line! Is there a way to do this or should I move on with my life?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
每条语句只能有一个后缀操作。 但是您可以通过使用
do
块来做您想做的事情(某种程度),例如,就我个人而言,我发现这种风格非常混乱且难以阅读。 如果我是你,我会避免它。 如果你遇到那么多麻烦,你不妨说
恕我直言。
You can only have one postfix operation per statement. But you can do what you want (sorta) by using a
do
block, e.g.Personally, I find this style extremely confusing and difficult to read. I'd avoid it, if I were you. If you're going to all that trouble, you might as well say
IMHO.
实现与 if 语句相同效果的另一种方法是使用
and
,因此您可以说然而,这不是一种鼓励的做法。
Another way to achieve the same effect as an if statement is with
and
, so you could sayHowever, that is not an encouraged practice.
据我了解,这是 Perl 早期的一个有意的设计决定,以避免人们编写难以阅读或难以获得正确的代码。 Perl 6 至少将允许一些组合(if/for 和 for/if,但不是 for/for 或 if/if 也许?),我相信 Perl 5 在某些时候也会效仿。
更新:Perl 6 将允许在循环内使用条件(例如
EXPR if EXPR for LIST
); 更多的内容需要额外的括号(例如( EXPR for LIST ) if EXPR
)。As I understand it, this was an intentional design decision in the very early days of perl to avoid having people write hard to read or hard to get right code. Perl 6 will allow at least some combinations (if/for and for/if, but not for/for or if/if maybe?) and I believe Perl 5 will follow suit at some point.
Update: Perl 6 will allow a conditional inside a loop (e.g.
EXPR if EXPR for LIST
); anything more requires extra parens (e.g.( EXPR for LIST ) if EXPR
).