perl-5.38.0
glob EXPR
glob

In list context, returns a (possibly empty) list of filename expansions on the value of EXPR such as the Unix shell Bash would do. In scalar context, glob iterates through such filename expansions, returning undef when the list is exhausted. If EXPR is omitted, $_ is used.

リストコンテキストでは、 EXPR の値を、Unix シェルの Bash が行なうように ファイル名の展開を行なった結果のリスト(空かもしれません)を返します。 スカラコンテキストでは、glob はこのようなファイル名展開を繰り返し、 リストがなくなったら undef を返します。 EXPR が省略されると、$_ が使われます。

    # List context
    my @txt_files  = glob("*.txt");
    my @perl_files = glob("*.pl *.pm");

    # Scalar context
    while (my $file = glob("*.mp3")) {
        # Do stuff
    }

Glob also supports an alternate syntax using < > as delimiters. While this syntax is supported, it is recommended that you use glob instead as it is more readable and searchable.

glob はまた区切り文字として < > を使うもう一つの文法に 対応しています。 この文法は対応していますが、可読性と検索性がより高いので、代わりに glob を使うことを勧めます。

    my @txt_files  = <"*.txt">;

If you need case insensitive file globbing that can be achieved using the :nocase parameter of the bsd_glob module.

大文字小文字を区別するファイルグロブが必要な場合、 bsd_glob モジュールの :nocase 引数を 使うことで達成できます:

    use File::Glob qw(:globally :nocase);

        my @txt = glob("readme*"); # README readme.txt Readme.md

Note that glob splits its arguments on whitespace and treats each segment as separate pattern. As such, glob("*.c *.h") matches all files with a .c or .h extension. The expression glob(".* *") matches all files in the current working directory. If you want to glob filenames that might contain whitespace, you'll have to use extra quotes around the spacey filename to protect it. For example, to glob filenames that have an e followed by a space followed by an f, use one of:

glob は引数を空白で分割して、それぞれを分割された パターンとして扱います。 従って、glob("*.c *.h").c または .h 拡張子を持つ全てのファイルに マッチングします。 式 glob(".* *") はカレントワーキングディレクトリの全てのファイルに マッチングします。 空白を含んでいるかも知れないファイル名をグロブしたい場合、それを守るために 空白入りファイル名の周りに追加のクォートを使う必要があります。 例えば、e の後に空白、その後に f というファイル名をグロブするには 以下の一つを使います:

    my @spacies = <"*e f*">;
    my @spacies = glob('"*e f*"');
    my @spacies = glob(q("*e f*"));

If you had to get a variable through, you could do this:

変数を通す必要があった場合、以下のようにできました:

    my @spacies = glob("'*${var}e f*'");
    my @spacies = glob(qq("*${var}e f*"));

If non-empty braces are the only wildcard characters used in the glob, no filenames are matched, but potentially many strings are returned. For example, this produces nine strings, one for each pairing of fruits and colors:

空でない中かっこが glob で使われている唯一の ワイルドカード文字列の場合、ファイル名とはマッチングせず、 可能性のある文字列が返されます。 例えば、これは 9 個の文字列を生成し、それぞれは果物と色の組み合わせに なります:

    my @many = glob("{apple,tomato,cherry}={green,yellow,red}");

This operator is implemented using the standard File::Glob extension. See bsd_glob for details, including bsd_glob, which does not treat whitespace as a pattern separator.

この演算子は標準の File::Glob 拡張を使って 実装されています。 空白をパターンのセパレータとして扱わない bsd_glob を含めた 詳細は bsd_glob を参照してください。

If a glob expression is used as the condition of a while or for loop, then it will be implicitly assigned to $_. If either a glob expression or an explicit assignment of a glob 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.

glob 式が whilefor ループの条件として使われた場合、 これは暗黙に $_ に代入されます。 glob 式または glob 式からスカラへの明示的な代入が while/for の条件部として使われた場合、 条件は通常の真の値かどうかではなく、式の値が定義されているかどうかを テストします。

Internal implemenation details:

内部実装の詳細:

This is the internal function implementing the <*.c> operator, but you can use it directly. The <*.c> operator is discussed in more detail in "I/O Operators" in perlop.

これは、<*.c> 演算子を実装する内部関数ですが、 直接使用することもできます。 <*.c>演算子については "I/O Operators" in perlop でより詳細に議論しています。

Portability issues: "glob" in perlport.

移植性の問題: "glob" in perlport