#! perl -slw use strict; sub sanitizeDir { require Cwd; my( $path ) = @_; my( $cwd ) = Cwd::getcwd() =~ m[^(.*)$]; return unless do{ local %ENV; $path =~ s[^(.*)$][$1]; chdir $path; }; my $sanitized = Cwd::getcwd(); chdir $cwd; return $sanitized =~ s[^$cwd][$cwd] ? $sanitized : (); } my $dubiousPath = $ARGV[0]; my $absPath = sanitizeDir( $dubiousPath ); if( defined $absPath ) { print 'Absolute path: ', $absPath; } else { print 'Invalid path: ', $dubiousPath; } #### P:\test>perl -T junk.pl8 . Absolute path: P:/test P:\test>perl -T junk.pl8 .. Invalid path: .. P:\test>perl -T junk.pl8 ./.. Invalid path: ./.. P:\test>perl -T junk.pl8 ./../. Invalid path: ./../. P:\test>perl -T junk.pl8 ./used Absolute path: P:/test/used P:\test>perl -T junk.pl8 ./used/.. Absolute path: P:/test P:\test>perl -T junk.pl8 ./used/../.. Invalid path: ./used/../.. P:\test>perl -T junk.pl8 ./used/././t/ Absolute path: P:/test/used/t P:\test>perl -T junk.pl8 ./used/././t/../ Absolute path: P:/test/used P:\test>perl -T junk.pl8 ./used/././t/../.. Absolute path: P:/test P:\test>perl -T junk.pl8 ./used/././t/../.././.. Invalid path: ./used/././t/../.././..