获取相对于当前目录的路径绝对路径的首选方法



>我正在尝试扩展相对于当前目录的路径:

use feature qw(say);
use strict;
use warnings;
use Cwd;
use File::Spec;
my $fn = 'test/my_file';
say File::Spec->rel2abs( $fn );
say Cwd::abs_path( $fn );

在这里,如果目录test不存在,Cwd::abs_path()失败。

为什么File::Spec->rel2abs()工作正常,而Cwd::abs_path()失败?

CwdFile::Spec这两个模块的文档几乎没有提供为什么会发生这种情况的线索。根据以下错误报告,这可能是由于符号链接的扩展,首先是从2004年开始的: "Cwd::abs_path returns undef for non-existent paths"

  • bugs.debian.org
  • rt.perl.org

File::Spec 和更友好的 Path::Class 不接触文件系统,因此它们可以用于您将创建的路径。 Cwd::abs_path,因此可用于返回有效路径。使用最合适的方法。

最新更新