Chen Yue wrote:
>
> Rob Dixon wrote:
>>
>> Chen Yue wrote:
>>>
>>> I have a file containing UNIX-styled Path in each line. But the path is
>>> simplified enough. Some of them has ".." and "." in the middle, such as
>>> "/a/b/./c/../d".
>>> Now I want to simplify each Path according to Unix tradition.
>>>
>>> /a/b/./c/../d -> /a/b/d
>>>
>>> The only way I could think out is to split the path and reconstruct them
>>> in reverse order. But I don't think it is a smart solution. Is there a quick
>>> way to employ regexp or a library to fix this?
>>
>> Take a look at the abs_path function in the Cwd module.
>
> Thank you for the suggestion. Actually, abs_path requires the original path
> exists on the host. But my case does not fulfill this. This is what puzzled
> me.
This seems to do the job. Hope it helps.
Rob
sub canonical_path {
my $path = shift;
my @path;
foreach (File::Spec->splitdir($path)) {
if ($_ eq '..') {
if ($path[-1] eq '..') {
push @path, $_;
}
else {
pop @path;
}
}
elsif ($_ ne '.') {
push @path, $_;
}
}
File::Spec->catdir(@path);
}
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/