如何在 Perl 中将文件路径更正为操作系统特定的路径?

发布于 2024-09-27 05:31:39 字数 640 浏览 0 评论 0原文

由于一些混乱的遗留代码,

$path = [OS specific base DIR name][hardcoded Linux file path]

在 Linux 上有这样的情况,

$path = /common/path/to/dir/pathtofile/name.extension

但在 Windows 上它变成了这样

$path = C:\path\to\dir\pathtofile/name.extension

有些代码在 Windows 上失败,因为它需要一个 \ 而它得到一个 /。

有 Perl 函数可以帮助我吗?

类似

print "$path\n";
$path = <some function> $path;
print "$path\n";

C:\path\to\dir\pathtofile/name.extension
C:\path\to\dir\pathtofile\name.extension

Due to some messed up legacy code,

I have

$path = [OS specific base DIR name][hardcoded Linux file path]

So on Linux, it is something like

$path = /common/path/to/dir/pathtofile/name.extension

but on Windows it becomes this

$path = C:\path\to\dir\pathtofile/name.extension

Some of the code fails on Windows because it is expecting a \ while it gets a /.

Is there a Perl function that can help me here?

Something like

print "$path\n";
$path = <some function> $path;
print "$path\n";

C:\path\to\dir\pathtofile/name.extension
C:\path\to\dir\pathtofile\name.extension

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

定格我的天空 2024-10-04 05:31:39

File::Spec 系列模块完全存在因为这个原因。您可能需要考虑使用它而不是硬编码 UNIX 路径。

use File::Spec::Functions 'catfile';

my $path = catfile($os_specific_base_dir, 'pathtofile', 'name.extension');

如果您确实需要在某个地方硬编码 unix 路径(这看起来确实不是一个好主意),您可能应该使用 File::Spec::Unix 将路径拆分为其组件,然后使用目标系统本机的 File::Spec 变体来构建,将组件重新组合在一起。

The File::Spec family of modules exists exactly for that reason. You might want to consider using that instead of hardcoding unix paths.

use File::Spec::Functions 'catfile';

my $path = catfile($os_specific_base_dir, 'pathtofile', 'name.extension');

If you really need to hardcode unix paths somewhere, which really doesn't seem like a good idea, you should probably use File::Spec::Unix to split up the path into its components, and then use the File::Spec variant native to your target system to build put the components back together again.

活雷疯 2024-10-04 05:31:39

您是否看过 File::Spec 核心模块以及相关模块?

Have you looked at the File::Spec core modules - and related ones?

波浪屿的海角声 2024-10-04 05:31:39

听起来您好像是从其他地方将整个 $path 作为一个单元传递给您,而不是单独处理各个部分。在这种情况下,如果我们在 Windows 下运行,我认为没有比用反斜杠替换所有斜杠的丑陋解决方案更好的答案了。

以下内容应该可以满足您的需要:

if ($^O =~ /^MSWin/) {
    $path =~ s{/}{\\}g;
}

编辑:再想一想 - 看了 File::Spec 后,看起来您想要 canonpath 函数:

use File::Spec;
# ...
$path = File::Spec->canonpath($path);

It sounds like you're getting the entire $path handed to you from somewhere else as one unit, rather than having the luxury of dealing with the pieces separately. In which case, I don't see a better answer than the ugly solution of replacing all slashes with backslashes if we're running under Windows.

The following should do what you need:

if ($^O =~ /^MSWin/) {
    $path =~ s{/}{\\}g;
}

EDIT: On second thought -- having taken a look at File::Spec, it looks like you want the canonpath function:

use File::Spec;
# ...
$path = File::Spec->canonpath($path);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文