简体   繁体   English

如何在Perl中获取目录(文件路径)分隔符?

[英]How can I get the directory (file path) separator in Perl?

In case of Java, we can get the path separator using 在Java的情况下,我们可以使用路径分隔符

System.getProperty("path.separator");

Is there a similar way in Perl? Perl中有类似的方法吗? All I want to do is to find a dir, immediate sub directory. 我想做的就是找到一个目录,直接子目录。 Say I am being given two arguments $a and $b ; 说我有两个参数$a$b ; I am splitting the first one based on the path separator and joining it again except the last fragment and comparing with the second argument. 我基于路径分隔符拆分第一个并再次连接它,除了最后一个片段并与第二个参数进行比较。

The problem is my code has to be generic and for that I need to know whats the system dependent path separator is? 问题是我的代码必须是通用的,为此我需要知道系统相关的路径分隔符是什么?

You should not form file paths by hand - instead use File::Spec module: 您不应该手动形成文件路径 - 而是使用File :: Spec模块:

($volume, $directories,$file) = File::Spec->splitpath( $path );
@dirs = File::Spec->splitdir( $directories );
$path = File::Spec->catdir( @directories );
$path = File::Spec->catfile( @directories, $filename );

您可以在File :: Util中使用SL常量

The accepted answer solves your real problem, but if you really want to get the separator (using only perl core modules): 接受的答案解决了您的真正问题,但如果您真的想要获取分隔符(仅使用perl核心模块):

my $sep = File::Spec->catfile('', '');

This joins two empty file names with the current system's separator, leaving only the separator. 这将两个空文件名与当前系统的分隔符连接起来,只留下分隔符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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