繁体   English   中英

如何使用Perl将完整路径转换为相对路径?

[英]How do I convert a full path to a relative path using perl?

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

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

例如

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

使用文件:: Spec

它们具有abs2rel函数

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

在Windows和Linux上均可使用

use Path::Class;
my $full = file( "/full/path/to/my/file" );
my $relative = $full->relative( "/full" );

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM