“0”如何表示? readdir() 的结果在 while 条件下不为 false?

发布于 2024-07-19 04:36:23 字数 873 浏览 3 评论 0原文

另请参阅:在哪里文档中是否说 while 测试 readdir 的定义?。 (不是重复;只是密切相关。


许多人将下面的循环视为惯用的:

while (defined(my $file = readdir($dir)) {
    ...
}

而不是:

while (my $file = readdir($dir)) {
    ...
}

因为据说在后一个版本中,如果文件名只是“0”(零),它应该终止循环,而当没有更多文件时它返回“undef”。

然而,在过去的某个时刻,对 define() 的测试不再是必要的 - 似乎有特殊情况的代码允许后一个版本无论如何都可以工作。

我想知道这是如何运作的?

奇怪的是,如果我用对 foo() 的调用替换对 readdir() 的调用:

sub foo
{
    my ($dir) = @_;
    return readdir($dir);
}

while (my $file = foo($dir)) {
    ...
}

那么代码执行我想要的操作期望,并在找到名为“0”的文件时终止循环。

(在 MacOS X 10.5.6 上使用 Perl 5.8.9 进行测试)

See also: Where in the documentation does it say that while tests readdir for definedness?. (Not a duplicate; just closely related.)


Many people treat the loop below as idiomatic:

while (defined(my $file = readdir($dir)) {
    ...
}

instead of:

while (my $file = readdir($dir)) {
    ...
}

because supposedly with the latter version if the filename is just "0" (zero) it should terminate the loop, whereas it returns 'undef' when there are no more files.

However at some point in the past this test for defined() stopped being necessary - there appears to be special case code that allows the latter version to work regardless.

I'd like to know how this works?

Curiously,if I replace the call to readdir() with a call to foo() instead:

sub foo
{
    my ($dir) = @_;
    return readdir($dir);
}

while (my $file = foo($dir)) {
    ...
}

then the code does do what I'd expect, and terminate the loop when a file named "0" is found.

(tested with Perl 5.8.9 on MacOS X 10.5.6)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

海拔太高太耀眼 2024-07-26 04:36:24

这是魔法。 特别是 while magic (记录在 perlsyn 中,perlop,可能还有其他我不知道的地方记住)。 Perl 允许您使用某些速记符号。 如果你想看看 Perl 在背后做了什么,你可以使用 B::Deparse< /代码>。 这是一个使用速记循环的文件:

#!/usr/bin/perl

use strict;
use warnings;

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

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

如果运行 perl -MO=Deparse filename.pl,您将得到 Perl 看到的代码:

use warnings;
use strict 'refs';
die "$!" unless opendir my $dir, '/tmp';
while (defined(my $file = readdir $dir)) {
    do {
        print "$file\n"
    };
}
filename.pl syntax OK

It is magic. Specifically while magic (documented in perlsyn, perlop, and probably other places I don't remember). Perl allows you certain shorthand notations. If you want to see what Perl is doing behind your back you can use B::Deparse. Here is a file that uses the shorthand loop:

#!/usr/bin/perl

use strict;
use warnings;

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

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

If you run perl -MO=Deparse filename.pl you get the code Perl sees:

use warnings;
use strict 'refs';
die "$!" unless opendir my $dir, '/tmp';
while (defined(my $file = readdir $dir)) {
    do {
        print "$file\n"
    };
}
filename.pl syntax OK
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文