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您能帮我一下吗,提前谢谢。
发布于 2013-12-05 00:46:44
/local/mnt/LINUX/platform/../source/gnss/api/src/v02.c并不一定是指
/local/mnt/LINUX/source/gnss/api/src/v02.c因为
/local/mnt/LINUX/platform可能是个符号链接。因此,如果不进行文件系统检查,则无法安全地折叠x/..。Spec不执行任何文件系统检查,但Cwd执行。
use Cwd qw( abs_path );
print abs_path( $dep );发布于 2013-12-05 00:14:09
您必须使用Cwd模块中的函数realpath():
use Cwd qw<realpath>;
...
print File::Spec->rel2abs( realpath($dep) ) ;发布于 2013-12-05 00:15:38
print Cwd::realpath( $dep ) ;很管用。
https://stackoverflow.com/questions/20380116
复制相似问题