简体   繁体   中英

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.

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.

eg

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

Use File::Spec

They have a abs2rel function

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

Will work on both Windows and Linux

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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