簡體   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