perl - 文档中的哪个地方说 while 测试 readdir 的定义?

标签 perl perldoc

另见:How is "0" result from readdir not false in a while condition? . (不是重复;只是密切相关。)

文档中的哪个地方说 while 测试 readdir 的定义?例如,这段代码

#!/usr/bin/perl

use strict;
use warnings;

opendir my $dir, "/tmp" or die "$!";

while (my $file = readdir($dir)) {
        print "$file\n";
}
closedir $dir;

运行时 B::Deparse产量:
use warnings;
use strict 'refs';
die "$!" unless opendir my $dir, '/tmp';
while (defined(my $file = readdir $dir)) {
    do {
        print "$file\n"
    };
}
z.pl syntax OK

我期待这种行为,但我找不到它的指定位置。在 I/O Operators section of perlop它说

The following lines are equivalent:

     while (defined($_ = <STDIN>)) { print; }
     while ($_ = <STDIN>) { print; }
     while (<STDIN>) { print; }
     for (;<STDIN>;) { print; }
     print while defined($_ = <STDIN>);
     print while ($_ = <STDIN>);
     print while <STDIN>;



但是没有提到readdir .

最佳答案

你说它没有证件是对的。我看起来很努力,我也找不到任何关于它特别的地方。正如您所发现的,它很特别,并且如下所示:

$ perl -MO=Deparse \
       -E'opendir(my $dir, "."); while($_ = readdir($dir)) { say; }'

BEGIN {
    $^H{'feature_say'} = q(1);
    $^H{'feature_state'} = q(1);
    $^H{'feature_switch'} = q(1);
}
opendir my $dir, '.';
while (defined($_ = readdir $dir)) {
    say $_;
}
-e syntax OK

翻翻源码,Perl_newWHILEOPop.c专门针对 readdir 进行了测试, glob , readlineeach ...嗯,让我们做一些挖掘,看看什么时候readdir加入。

git 进行一些挖掘表明至少从 1998 年开始就是这样,Gurusamy Sarathy 在提交 55d729e4 中做出了相关更改。 .虽然我还没有深入了解哪些版本已经发布,但我敢打赌它至少是 5.6.0 及更高版本。我在三角洲中找不到任何提及它。

可能在第三版 Camel 书中提到过,但我还没有查到。

我认为这里的补丁(甚至只是 p5p 的注释)肯定会受到赞赏。

保罗

关于perl - 文档中的哪个地方说 while 测试 readdir 的定义?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/844625/

相关文章:

perl - 如何查找(离线)perl 文档 - "case insensitive"或使用正则表达式

Perl 按位运算给出意想不到的结果

perl - 在 Perl 脚本中分离配置数据和脚本逻辑

perl - 将环境的语言环境强加在 Perl block 中

perl - 在 perl 中。 hash是如何在内存中存储数据的

xml - 如何从 XML 中过滤出 '?'?

perl - 如何使用 perldoc 查找 %ENV 变量?

Perl "do",相对路径以 "."或 ".."开头