【问题标题】:How do I convert a full path to a relative path using perl?如何使用 perl 将完整路径转换为相对路径?
【发布时间】:2011-11-15 13:45:27
【问题描述】:

我在 Perl 程序的两个变量中拥有文件的完整路径和其父目录之一的完整路径。

什么是计算文件相对于父目录的相对路径的安全方法。需要在 windows 和 unix 上工作。

例如

$filePath = "/full/path/to/my/file";
$parentPath = "/full";
$relativePath = ??? # should be "path/to/my/file"

【问题讨论】:

标签: perl


【解决方案1】:

使用File::Spec

他们有一个abs2rel函数

my $relativePath = File::Spec->abs2rel ($filePath,  $parentPath);

适用于 Windows 和 Linux

【讨论】:

    【解决方案2】:
    use Path::Class;
    my $full = file( "/full/path/to/my/file" );
    my $relative = $full->relative( "/full" );
    

    【讨论】:

    • +1 我喜欢Path::Class,它解决了 File::Spec 中的一些设计缺陷。
    猜你喜欢
    • 2014-10-29
    • 2012-04-01
    • 2013-06-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-16
    • 2017-08-01
    相关资源
    最近更新 更多