use strict; use warnings; my @paths = qw{ /self/xplanatori /usr/lib/perl5/../../ /home/fake4/../fake/ /resolve/me/../right/ /./resolve////me/../right/. /../.././usr/././. /../.././////././. /..... .///../}; print qq{@{[cleanPath($_)]}\n} for @paths; # --------- sub cleanPath # --------- { my $absPath = shift; return $absPath if $absPath =~ m{^/$}; my @elems = split m{/}, $absPath; my $ptr = 1; while($ptr <= $#elems) { if($elems[$ptr] eq q{}) { splice @elems, $ptr, 1; } elsif($elems[$ptr] eq q{.}) { splice @elems, $ptr, 1; } elsif($elems[$ptr] eq q{..}) { if($ptr < 2) { splice @elems, $ptr, 1; } else { $ptr--; splice @elems, $ptr, 2; } } else { $ptr++; } } return $#elems ? join q{/}, @elems : q{/}; }
Here's the output.
/self/xplanatori /usr /home/fake /resolve/right /resolve/right /usr / /..... /
I hope this is of interest.
Cheers,
JohnGG
In reply to Re: cleaning up absolute path without resolving symlinks
by johngg
in thread cleaning up absolute path without resolving symlinks
by leocharre
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |