简体   繁体   English

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

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

I have the full path to a file and the full path to one of its parent directories in two variables in Perl program. 我在Perl程序中的两个变量中具有文件的完整路径和其父目录之一的完整路径。

What is a safe way to calculate the relative path of the file relative to the parent directory. 什么是计算文件相对于父目录的相对路径的安全方法。 Needs to work on windows and unix. 需要在Windows和Unix上工作。

eg 例如

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

Use File::Spec 使用文件:: Spec

They have a abs2rel function 它们具有abs2rel函数

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

Will work on both Windows and Linux 在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