my $dep_file="/local/mnt/LINUX/platform/gnss/v02.d"; #is my dep file
open my $FH, $dep_file or die "Could not open $dep_file: $!";
my $cwd = getcwd();
while( my $dep = <$FH>) {
if ($dep =~ /$cwd/) { #interested only in lines starting with $cwd
$dep =~ s/^\s*//; #remove whitespaces in the beginning.
$dep =~ s/\s+\\\s*$//; # remove white space followed by \ at the end.
print File::Spec->rel2abs( $dep ) ; #trying to print full path of file without a ".." in it.
#print $dep;
print "\n";
}
last if $dep =~ /^$/; # don't want to read any line after an empty line.
}
close $FH;
但结果与预期不符:
/local/mnt/LINUX/platform/../source/gnss/api/src/v02.c
/local/mnt/LINUX/project/../source/gnss/api/../../hexagon-infra/q6-00/include/internal.h
我希望在解析之后没有“..”打印行,如下所示
/local/mnt/LINUX/source/gnss/api/src/v02.c
/local/mnt/LINUX/source/hexagon-infra/q6-00/include/internal.h
可以提前感谢,请你帮忙。
答案 0 :(得分:2)
您必须使用realpath()
模块中的Cwd
函数:
use Cwd qw<realpath>;
...
print File::Spec->rel2abs( realpath($dep) ) ;
答案 1 :(得分:2)
/local/mnt/LINUX/platform/../source/gnss/api/src/v02.c
不一定是指
/local/mnt/LINUX/source/gnss/api/src/v02.c
,因为
/local/mnt/LINUX/platform
可以是符号链接。因此,如果没有文件系统检查,则x/..
无法安全地折叠。 File :: Spec不执行任何文件系统检查,但是Cwd会执行。
use Cwd qw( abs_path );
print abs_path( $dep );
答案 2 :(得分:0)
print Cwd::realpath( $dep ) ;
作品。