Perl通用文件操作模块-File::Spec
2015-10-04 18:45:29 阿炯

File::Spec - portably perform operations on file names

File::Spec是Perl内置的用于对文件常用操作提供较为多平台(Cygwin、Epoc、Mac、OS2、Unix、VMS、Win32)统一的函数的模块。

This module is designed to support operations commonly performed on file specifications (usually called "file names", but not to be confused with the contents of a file, or Perl's file handles), such as concatenating several directory and file names into a single path, or determining whether a path is rooted. It is based on code directly taken from MakeMaker 5.17, code written by Andreas König, Andy Dougherty, Charles Bailey, Ilya Zakharevich, Paul Schinder, and others.

这个模块的目的是在多个主流平台上支持(通常被称为“文件名”,而不是文件的内容,不要与Perl文件句柄相混淆)一般的执行文件操作规范,比如一些目录和文件名连接为一个路径,或判断一个文件是否路径从根目录开始。它是基于直接取自MakeMaker 5.17代码,代码由安德烈亚斯康尼锡,安迪·多尔蒂,查尔斯•贝利、Ilya Zakharevich、保罗Schinder等编写。

Exports

The following functions are exported by default.
canonpath
catdir
catfile
curdir
rootdir
updir
no_upwards
file_name_is_absolute
path

The following functions are exported only by request.
devnull
tmpdir
splitpath
splitdir
catpath
abs2rel
rel2abs
case_tolerant

All the functions may be imported using the :ALL tag.
想导出所有函数请使用:ALL标签。

方法API

canonpath

No physical check on the filesystem, but a logical cleanup of a path.
没有物理检查文件系统,只是对路径进行了逻辑清理。

$cpath = File::Spec->canonpath( $path ) ;

Note that this does *not* collapse x/../y sections into y. This is by design. If /foo on your system is a symlink to /bar/baz, then /foo/../quux is actually /bar/quux, not /quux as a naive ../-removal would give you. If you want to do this kind of processing, you probably want Cwd 's realpath() function to actually traverse the filesystem cleaning up paths like this.
注意,这个*不*是将x/../y处理为y。通过这样的设计,如果/foo/是一个指向/bar/baz的符号链接,访问/foo/. ./quux实际上是/bar/quux,不是/quux,../将会删除。如果你想做这样的处理,你可能想要调用 Cwd模块中的realpath()函数来遍历文件系统以便清理这样的路径。

catdir

Concatenate two or more directory names to form a complete path ending with a directory. But remove the trailing slash from the resulting string, because it doesn't look good, isn't necessary and confuses OS/2. Of course, if this is the root directory, don't cut off the trailing slash.
连接两个或两个以上的目录名,形成一个完整的路径目录。但除去生成的目录字符串尾部的斜杠,因为它看起来并不需要,也是没有必要的,在OS/2平台具有迷惑性。当然,如果这是根目录,不要移除尾部的斜杠了。

$path = File::Spec->catdir( @directories );

catfile

Concatenate one or more directory names and a filename to form a complete path ending with a filename
连接一个或多个目录名和文件名,形成一个完整的路径与文件名的组合

$path = File::Spec->catfile( @directories, $filename );

curdir

Returns a string representation of the current directory.
返回当前目录名称的字符串。

$curdir = File::Spec->curdir();

devnull

Returns a string representation of the null device.
返回一个空的设备,以字符串的形式。

$devnull = File::Spec->devnull();

rootdir

Returns a string representation of the root directory.
返回根目录,以字符串的形式。

$rootdir = File::Spec->rootdir();

tmpdir

Returns a string representation of the first writable directory from a list of possible temporary directories. Returns the current directory if no writable temporary directories are found. The list of directories checked depends on the platform; e.g. File::Spec::Unix checks $ENV{TMPDIR} (unless taint is on) and /tmp.
返回一个字符串表示的可写目录的临时目录的列表。如果没有可写的临时目录则返回当前目录。目录列表检查取决于平台,例如File::Spec::Unix将检查$ENV{TMPDIR}和/tmp。

$tmpdir = File::Spec->tmpdir();

updir

Returns a string representation of the parent directory.
返回以一个字符串表示的父目录。

$updir = File::Spec->updir();

no_upwards

Given a list of file names, strip out those that refer to a parent directory. (Does not strip symlinks, only '.', '..', and equivalents.)
给定一个文件名列表,剔除那些引用父目录(不带符号链接,只有'.'、'..'和等价)。

@paths = File::Spec->no_upwards( @paths );

case_tolerant

Returns a true or false value indicating, respectively, that alphabetic case is not or is significant when comparing file specifications. Cygwin and Win32 accept an optional drive argument.
分别返回真或假值,字母不或当比较文件规范。Cygwin和Win32接受一个可选的驱动参数。

$is_case_tolerant = File::Spec->case_tolerant();

file_name_is_absolute

Takes as its argument a path, and returns true if it is an absolute path.
路径作为其参数,如果它是一个绝对路径则返回true。

$is_absolute = File::Spec->file_name_is_absolute( $path );

This does not consult the local filesystem on Unix, Win32, OS/2, or Mac OS (Classic). It does consult the working environment for VMS (see file_name_is_absolute in File::Spec::VMS).

path

Takes no argument. Returns the environment variable PATH (or the local platform's equivalent) as a list.
不接受参数。返回路径环境变量(或本地平台)作为一个列表。

@PATH = File::Spec->path();

join

join is the same as catfile.
与catfile相同的。

splitpath

Splits a path in to volume, directory, and filename portions. On systems with no concept of volume, returns '' for volume.
分割出路径的卷、目录和文件名这三部分,系统上没有卷的概念的话则返回""。

 ($volume,$directories,$file) = File::Spec->splitpath( $path );
 ($volume,$directories,$file) = File::Spec->splitpath( $path, $no_file );

For systems with no syntax differentiating filenames from directories, assumes that the last file is a path unless $no_file is true or a trailing separator or /. or /.. is present. On Unix, this means that $no_file true makes this return ( '', $path, '' ).

The directory portion may or may not be returned with a trailing '/'.The results can be passed to catpath() to get back a path equivalent to (usually identical to) the original path.
目录部分可能是或不是带着'/'结尾。结果可以通过catpath()返回一个路径相当于原始路径(通常是相同的)。

splitdir

The opposite of catdir.
与catdir函数作用相反。

@dirs = File::Spec->splitdir( $directories );

$directories must be only the directory portion of the path on systems that have the concept of a volume or that have path syntax that differentiates files from directories.
$directories必须只有目录部分的路径(在系统卷的概念或路径语法),区分文件的目录。

Unlike just splitting the directories on the separator, empty directory names ('') can be returned, because these are significant on some OSes.

catpath()

Takes volume, directory and file portions and returns an entire path. Under Unix, $volume is ignored, and directory and file are concatenated. A '/' is inserted if need be. On other OSes, $volume is significant.
可提供卷、目录和文件部分,并返回一个完整的路径。在Unix下,$volume被忽略,目录和文件会被连接,如果需要插入一个“/”。在其他操作系统,卷是显著的。

$full_path = File::Spec->catpath( $volume, $directory, $file );

abs2rel

Takes a destination path and an optional base path returns a relative path from the base path to the destination path:
需要一个目的地路径和一个可选的基本路径,返回一个相对路径,即基本路径到目的地的路径:

 $rel_path = File::Spec->abs2rel( $path ) ;
 $rel_path = File::Spec->abs2rel( $path, $base ) ;

If $base is not present or '', then Cwd::cwd() is used. If $base is relative, then it is converted to absolute form using rel2abs(). This means that it is taken to be relative to Cwd::cwd().
如果$base不存在或空,那么Cwd::cwd()会被使用。如果$base是相对路径,那么它被转换为绝对路径的方式使用rel2abs()函数。这意味着它是使用了相对于Cwd::cwd()。

On systems with the concept of volume, if $path and $base appear to be on two different volumes, we will not attempt to resolve the two paths, and we will instead simply return $path . Note that previous versions of this module ignored the volume of $base , which resulted in garbage results part of the time.

On systems that have a grammar that indicates filenames, this ignores the $base filename as well. Otherwise all path components are assumed to be directories.

If $path is relative, it is converted to absolute form using rel2abs(). This means that it is taken to be relative to Cwd::cwd().


rel2abs()

Converts a relative path to an absolute path.
将一个相对路径转换为绝对路径。

 $abs_path = File::Spec->rel2abs( $path ) ;
 $abs_path = File::Spec->rel2abs( $path, $base ) ;

If $base is not present or '', then Cwd::cwd() is used. If $base is relative, then it is converted to absolute form using rel2abs(). This means that it is taken to be relative to Cwd::cwd().
如果基础不存在或“美元,那么:慢性消耗性疾病:慢性消耗病()使用。如果$base是相对的,那么它被转换为使用rel2abs()绝对形式。这意味着它是相对于Cwd::cwd()。

On systems with the concept of volume, if $path and $base appear to be on two different volumes, we will not attempt to resolve the two paths, and we will instead simply return $path . Note that previous versions of this module ignored the volume of $base , which resulted in garbage results part of the time.

On systems that have a grammar that indicates filenames, this ignores the $base filename as well. Otherwise all path components are assumed to be directories.

If $path is absolute, it is cleaned up and returned using canonpath.
如果$path是绝对的,它是使用canonpath清理并返回。

参考文档

File::Spec