Brad Baxter wrote:
> On Jul 2, 11:46 am, [EMAIL PROTECTED] (Rob Dixon) wrote:
>> 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);
>>
>> }
> 
> Hi,
> 
> Pardon me for being dense, but I can't figure out a case where
> 
>>       if ($path[-1] eq '..') {
> 
> would ever be true.

My intention was to preserve relative paths starting with '..', but I got it
wrong. Here's V2.

Rob


sub canonical_path {

  my $path = shift;
  my @path;

  foreach (File::Spec->splitdir($path)) {

    next if $_ eq '.';

    if ($_ eq '..' and @path and $path[-1] ne '..') {
      pop @path;
    }
    else {
      push @path, $_;
    }
  }

  File::Spec->catdir(@path);
}

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to