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; }