使用定义的文件句柄和 while 循环 [英] Usage of defined with Filehandle and while Loop

查看:43
本文介绍了使用定义的文件句柄和 while 循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在阅读一本关于高级 Perl 编程的书(1)时,我遇到了这段代码:

While reading a book on advanced Perl programming(1), I came across this code:

while (defined($s = <>)) {
    ...

在这里使用 defined 有什么特殊原因吗?文档perlop 说:

Is there any special reason for using defined here? The documentation for perlop says:

在这些循环结构中,被赋值的(无论赋值是自动或显式)然后进行测试以查看它是否已定义.这定义的测试避免了行具有字符串值的问题Perl 将其视为 false,例如 """0" 没有尾随新队.如果你真的想让这些值终止循环,它们应该明确测试:[...]

In these loop constructs, the assigned value (whether assignment is automatic or explicit) is then tested to see whether it is defined. The defined test avoids problems where line has a string value that would be treated as false by Perl, for example a "" or a "0" with no trailing newline. If you really mean for such values to terminate the loop, they should be tested for explicitly: [...]

那么,会不会有一个角落案例,或者仅仅是因为这本书太旧了并且在最近的 Perl 版本中添加了自动 defined 测试?

So, would there be a corner case or that's simply because the book is too old and the automatic defined test was added in a recent Perl version?

(1) 高级 Perl 编程,第一版,Sriram Srinivasan.奥莱利(1997)

推荐答案

Perl 有很多隐式行为,比大多数其他语言要多得多.Perl 的座右铭是有不止一种方法可以做,而且由于隐含行为太多,所以通常有不止一种方法来表达完全相同的事情.

Perl has a lot of implicit behaviors, many more than most other languages. Perl's motto is There's More Than One To Do It, and because there is so much implicit behavior, there is often More Than One Way To express the exact same thing.

/foo/ 而不是 $_ =~ m/foo/

$x = shift 而不是 $x = shift @_

while (defined($_=)) 而不是 while(<>)

使用哪种表达方式很大程度上取决于您当地的编码标准和个人喜好.更明确的表达会提醒读者幕后真正发生的事情.这可能会也可能不会提高代码的可读性 - 这取决于受众的知识水平以及您是否使用众所周知的习语.

Which expressions to use are largely a matter of your local coding standards and personal preference. The more explicit expressions remind the reader what is really going on under the hood. This may or may not improve the readability of the code -- that depends on how knowledgeable the audience is and whether you are using well-known idioms.

在这种情况下,隐式行为比看起来要复杂一些.有时 perl 会隐式地对 readline 操作符的结果执行一个 defined(...) 测试:

In this case, the implicit behavior is a little more complicated than it seems. Sometimes perl will implicitly perform a defined(...) test on the result of the readline operator:

$ perl -MO=Deparse -e 'while($s=<>) { print $s }'
while (defined($s = <ARGV>)) {
    print $s;
}
-e syntax OK

但有时不会:

$ perl -MO=Deparse -e 'if($s=<>) { print $s }'
if ($s = <ARGV>) {
    print $s;
}
-e syntax OK

$ perl -MO=Deparse -e 'while(some_condition() && ($s=<>)) { print $s }'
while (some_condition() and $s = <ARGV>) {
    print $s;
}
-e syntax OK

假设您担心这种隐式行为应该处理的极端情况.您是否已将 perlop 提交到内存中,以便您了解 Perl 何时使用这种隐式行为,何时不使用?您了解 Perl v5.14 和 Perl v5.6 在这种行为上的区别吗?阅读您代码的人会理解吗?

Suppose that you are concerned about the corner cases that this implicit behavior is supposed to handle. Have you committed perlop to memory so that you understand when Perl uses this implicit behavior and when it doesn't? Do you understand the differences in this behavior between Perl v5.14 and Perl v5.6? Will the people reading your code understand?

同样,关于何时使用更显式的表达式没有正确或错误的答案,但是当隐式行为更深奥时,使用显式表达式的情况就更有说服力.

Again, there's no right or wrong answer about when to use the more explicit expressions, but the case for using an explicit expression is stronger when the implicit behavior is more esoteric.

这篇关于使用定义的文件句柄和 while 循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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