I want to resolve a path like '/this/that/../instead/here/' to '/this/instead/here'.
Cwd::abs_path() will do it. It will also resolve symlinks. Which I foresee a possible problem with in the future, for what I am working on.
What I think I need is some way to resolve a path without resolving symlinks.. I looked at File::Spec::Functions 'canonpath', it cleans things up a bit- but that's all.
I was playing with the following script, but I don't trust my imperfect mind's ability to do this right..
use Test::Simple 'no_plan'; use strict; #use File::Spec::Functions; use constant DEBUG => 1; my $p = { # mess is => should be '/self/xplanatori' => '/self/xplanatori', '/usr/lib/perl5/../../' => '/usr', '/home/fake4/../fake/' => '/home/fake', '/resolve/me/../right/' => '/resolve/right', '/./resolve////me/../right/.' => '/resolve/right', '/../.././usr/././.' => '/usr', '/../.././////././.' => '/', '/.....' => '/.....', './//../' =>'/' }; #map { ok( canonpath($_) eq $p->{$_}, "$_" ) } keys %$p; map { ok( abs_path2($_) eq $p->{$_}, "[$_]" ) } keys %$p; sub abs_path2 { my $path = shift; print STDERR " [$path]\n" if DEBUG; $path=~s/(?<=\/)\.(?=\/|\z)/\//sg; # /./ or /. to / print STDERR " [$path]\n" if DEBUG; 1 while $path=~s/[^\/]+\/+\.\.[\/|\Z]*//g; # anything/../ to / print STDERR " [$path]\n" if DEBUG; $path=~s/\/{2,}/\//g; # /// to / $path=~s/(?<=.)\/+$//g; # a/ to a print STDERR " [$path]\n" if DEBUG; return $path; }
Any suggestions?
In reply to cleaning up absolute path without resolving symlinks by leocharre
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |