如何使用 perl 将完整路径转换为相对路径? [英] How do I convert a full path to a relative path using perl?

查看:37
本文介绍了如何使用 perl 将完整路径转换为相对路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 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.

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

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.

例如

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

推荐答案

使用 File::Spec

Use File::Spec

他们有一个abs2rel函数

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

适用于 Windows 和 Linux

Will work on both Windows and Linux

这篇关于如何使用 perl 将完整路径转换为相对路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆