2011-11-15 16 views
12

私は、Perlプログラムの2つの変数でファイルへのフルパスとその親ディレクトリの1つへのフルパスを持っています。Perlを使って絶対パスを相対パスに変換するにはどうすればよいですか?

親ディレクトリに対するファイルの相対パスを計算する安全な方法は何ですか? WindowsやUnix上で作業する必要があります。

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

/questions/6278143/how-to-assign-a-regex-a-a-new-a-single-in-a-lineの変数 – daxim

答えて

22

使用File::Spec彼らはabs2rel機能

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

は、WindowsとLinuxの両方で動作します持って

http://stackoverflow.com関連
9
use Path::Class; 
my $full = file("/full/path/to/my/file"); 
my $relative = $full->relative("/full"); 
+3

+1 [Path :: Class](http:// p3rl。 org/Path :: Class)、それはFile :: Specのいくつかのデザイン疣贅について書いています。 – daxim

関連する問題