好吧,这是我想到的:
sub mangle_path {
# NOT PORTABLE
# Attempt to remove relative components from a path - can return
# incorrect results for paths like ../some_symlink/.. etc.
my $path = shift;
$path = getcwd . "/$path" if '/' ne substr $path, 0, 1;
my @dirs = ();
for(split '/', $path) {
pop @dirs, next if $_ eq '..';
push @dirs, $_ unless $_ eq '.' or $_ eq '';
}
return '/' . join '/', @dirs;
}
我知道这可能是不安全和无效的,但是这个例程的任何输入都将来自我的命令行,它为我解决了一些棘手的用例。