它在文档中的什么地方说,在测试readdir时是否具有定义性? [英] Where in the documentation does it say that while tests readdir for definedness?

查看:111
本文介绍了它在文档中的什么地方说,在测试readdir时是否具有定义性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

另请参阅:方式如何在一段时间内readdir是否为"0"?. (不是重复的;只是紧密相关.)

See also: How is "0" result from readdir not false in a while condition?. (Not a duplicate; just closely related.)

它在文档中的什么地方说在测试readdir的定义性时?例如,这段代码

Where in the documentation does it say that while tests readdir for definedness? For instance, this code

#!/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

我希望这种行为,但是我找不到它的指定位置.在perlop的"I/O运算符"部分中,

I expect this behaviour, but I cannot find where it is specified. In the I/O Operators section of perlop it says

以下几行是等效的:

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>;

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

但是没有提到readdir.

推荐答案

您很正确地认为它没有记录在案.我看上去很努力,也找不到任何特别的参考.正如您所发现的那样,它 很特殊,

You're quite right about it being undocumented. I've looked rather hard, and I can't find any reference to it being special either. It is special, as you've discovered, and as demonstrated by:

$ 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

仔细查看源代码,op.c中的Perl_newWHILEOP特别针对readdirglobreadlineeach进行了测试...嗯,让我们进行一些挖掘,看看readdir何时已添加.

Looking through the source, Perl_newWHILEOP in op.c specifically has tests for readdir, glob, readline and each... Hmm, let's do some digging, and see when readdir was added.

git的一些挖掘表明,至少从1998年以来就是这种方式,而Gurusamy Sarathy在commit 55d729e4中进行了相应的更改.虽然我还没有深入研究过要发布的发行版,但我敢打赌它至少应为5.6.0或更高版本.我在三角洲中找不到任何提及.

A bit of digging with git reveals that it's been that way since at least 1998, with Gurusamy Sarathy making the relevant change in commit 55d729e4. While I haven't gone digging to see which releases that's gone into, I'd wager it would be at least 5.6.0 and above. I can't find any mention of it in the deltas.

在第三版骆驼书中可能会提到 ,但我还没有找到答案.

It might be mentioned in the third edition camel book, but I haven't checked to find out.

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

I think that a patch here (or even just a note to p5p) would certainly be appreciated.

Paul

这篇关于它在文档中的什么地方说,在测试readdir时是否具有定义性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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