perl-5.38.0
readdir DIRHANDLE

Returns the next directory entry for a directory opened by opendir. If used in list context, returns all the rest of the entries in the directory. If there are no more entries, returns the undefined value in scalar context and the empty list in list context.

opendir でオープンしたディレクトリで、次の ディレクトリエントリを返します。 リストコンテキストで用いると、そのディレクトリの残りのエントリを、すべて 返します。 エントリが残っていない場合には、スカラコンテキストでは未定義値を、 リストコンテキストでは空リストを返します。

If you're planning to filetest the return values out of a readdir, you'd better prepend the directory in question. Otherwise, because we didn't chdir there, it would have been testing the wrong file.

readdir の返り値をファイルテストに使おうと 計画しているなら、頭にディレクトリをつける必要があります。 さもなければ、ここでは chdir はしないので、 間違ったファイルをテストしてしまうことになるでしょう。

    opendir(my $dh, $some_dir) || die "Can't opendir $some_dir: $!";
    my @dots = grep { /^\./ && -f "$some_dir/$_" } readdir($dh);
    closedir $dh;

As of Perl 5.12 you can use a bare readdir in a while loop, which will set $_ on every iteration. If either a readdir expression or an explicit assignment of a readdir expression to a scalar is used as a while/for condition, then the condition actually tests for definedness of the expression's value, not for its regular truth value.

Perl 5.12 から裸の readdirwhile で 使うことができ、この場合繰り返し毎に $_ にセットされます。 readdir 式または readdir 式からスカラへの明示的な代入が while/for の条件部として使われた場合、 条件は通常の真の値かどうかではなく、式の値が定義されているかどうかを テストします。

    opendir(my $dh, $some_dir) || die "Can't open $some_dir: $!";
    while (readdir $dh) {
        print "$some_dir/$_\n";
    }
    closedir $dh;

To avoid confusing would-be users of your code who are running earlier versions of Perl with mysterious failures, put this sort of thing at the top of your file to signal that your code will work only on Perls of a recent vintage:

あなたのコードを以前のバージョンの Perl で実行したユーザーが不思議な 失敗で混乱することを避けるために、コードが最近のバージョンの Perl で のみ 動作することを示すためにファイルの先頭に以下のようなことを 書いてください:

    use v5.12; # so readdir assigns to $_ in a lone while test