我想要的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'' –