我想要的Perl的等效Python的os.path.normpath()
的:如何规范Perl中的路径? (而不检查文件系统)
通过折叠冗余分离器和上一级的引用归一路径名,使得甲// B,A/B /,A /。/B和A/foo /../ B都成为A/B。此字符串操作可能会更改包含符号链接的路径的含义。 [...]
例如,我想将'/a/../b/./c//d'
转换为/b/c/d
。
我操纵的路径并不代表本地文件树中的真实目录。没有涉及的符号链接。所以一个普通的字符串操作很好。
我试过Cwd::abs_path
和File::Spec
,但他们没有做我想做的。
my $path = '/a/../b/./c//d';
File::Spec->canonpath($path);
File::Spec->rel2abs($path, '/');
# Both return '/a/../b/c/d'.
# They don't remove '..' because it might change
# the meaning of the path in case of symlinks.
Cwd::abs_path($path);
# Returns undef.
# This checks for the path in the filesystem, which I don't want.
Cwd::fast_abs_path($path);
# Gives an error: No such file or directory
可能相关链接:
- Normalized directory paths - perlmonks:人们讨论的几种方法。
此代码失败用于''A/B /../../ C/D'' –