Perl文件匹配模块-File::Glob
2016-06-27 19:54:13 阿炯

Perl中的glob函数的来源有两个:核心的glob(CORE::glob())、File::Glob模块中的glob函数。本文就主要讲解File::Glob及其中的glob函数用法。File::Glob在核心模块有三种存在:

File::Glob - Perl extension for BSD glob routine

File::GlobMapper - Extend File Glob to Allow Input and Output Files

File::DosGlob - DOS like globbing and then some


The glob angle-bracket operator <> is a pathname generator that implements the rules for file name pattern matching used by Unix-like shells such as the Bourne shell or C shell.

通配符匹配操作符< >,它是一个路径名生成器的实现,其规则为文件名称模式匹配所使用,类似于unix shell如Bourne shell或C shell。

File::Glob::bsd_glob() implements the FreeBSD glob(3) routine, which is a superset of the POSIX glob() (described in IEEE Std 1003.2 "POSIX.2"). bsd_glob() takes a mandatory pattern argument, and an optional flags argument, and returns a list of filenames matching the pattern, with interpretation of the pattern modified by the flags variable.

File::Glob::bsd_glob()基于FreeBSD glob(3)的实现,这是一个超集的POSIX glob()(IEEE Std 1003.2中描述“POSIX.2”)。bsd_glob()接受一个强制性的模式匹配参数,和一个可选的标记参数,并返回一个文件名列表,通过标志变量来控制模式的修改与解释。

Since v5.6.0, Perl's CORE::glob() is implemented in terms of bsd_glob(). Note that they don't share the same prototype--CORE::glob() only accepts a single argument. Due to historical reasons, CORE::glob() will also split its argument on whitespace, treating it as multiple patterns, whereas bsd_glob() considers them as one pattern. But see :bsd_glob under EXPORTS, below.

自5.6.0以来,Perl的核心函数glob()实现的bsd_glob()。但注意,它们不共享相同的原型——CORE::glob()只接受一个参数。由于历史原因,CORE::glob()也将空格分割其参数,将它视为多个模式,而bsd_glob()认为他们作为一个模式。

支持的匹配正则:
\:Quote the next metacharacter(引用下一个元字符)
[]:Character class(字符集)
{}:Multiple pattern(多模式)
*:Match any string of characters(匹配任何字符)
?:Match any single character(匹配任何单个字符)
~:User name home directory(用户家目录)


EXPORTS


:bsd_glob
The :bsd_glob export tag exports bsd_glob() and the constants listed below. It also overrides glob() in the calling package with one that behaves like bsd_glob() with regard to spaces (the space is treated as part of a file name), but supports iteration in scalar context; i.e., it preserves the core function's feature of returning the next item each time it is called.

:glob
The :glob tag, now discouraged, is the old version of :bsd_glob . It exports the same constants and functions, but its glob() override does not support iteration; it returns the last file name in scalar context. That means this will loop forever:
use File::Glob ':glob';
    while (my $file = <* copy.txt>) {
    ...
}

bsd_glob
This function, which is included in the two export tags listed above, takes one or two arguments. The first is the glob pattern. The second is a set of flags ORed together. The available flags are listed below under POSIX FLAGS. If the second argument is omitted, GLOB_CSH (or GLOB_CSH|GLOB_NOCASE on VMS and DOSish systems) is used by default.

:nocase and :case
These two export tags globally modify the default flags that bsd_glob() and, except on VMS, Perl's built-in glob operator use. GLOB_NOCASE is turned on or off, respectively.

csh_glob
The csh_glob() function can also be exported, but you should not use it directly unless you really know what you are doing. It splits the pattern into words and feeds each one to bsd_glob(). Perl's own glob() function uses this internally.