2条路径之间的Perl差异

Perl difference between 2 paths

如何获取两条路径之间的差异?

我们有定义为基本路径的 $src 变量,我们将修改后的列表放入 FilesList.txt。

作为

$src = "C:\Users\Desktop\Perl\Index"
$_ = "C:\Users\Desktop\Perl\Index\CC\Login.jsp";

现在,我们如何获得 "CC\Login.jsp" 值,我使用下面的代码,但我们没有得到预期的输出。请帮忙

$src="C:\Users\Desktop\Perl\Index";
open IN, "FilesList.txt";  
while(<IN>)
{
    chomp($_);
    $final=$_;
    $final =~ s/$src//;
    print "\nSubvalue is ---$final \n";
}

不要使用正则表达式模式来处理路径字符串。等效路径有多种不同的表示形式,字符串可能不匹配。正则表达式也不会注意路径分隔符,因此它不会纠正基本路径上的尾随分隔符,它可能会匹配部分路径步骤,如 C:\Users\Desktop\Perl\Ind,留下 ex\CC\Login.jsp,这显然是错误的

您需要 File::Spec::Functions

中的 abs2rel 函数

像这样

use strict;
use warnings 'all';
use feature 'say';

use File::Spec::Functions 'abs2rel';

my $src = 'C:\Users\Desktop\Perl\Index';

for ( 'C:\Users\Desktop\Perl\Index\CC\Login.jsp' ) {

    say abs2rel($_, $src);
}

输出

CC\Login.jsp