The following solution is not perfect, but works for me most of the time (the version below assumes that $path points to a subdir, but it can easily be adapted to the general case in which $path can also point to a file):
The principal shortcoming of this approach is that it only applies to directories that your program can visit; this excludes non-existent directories, unfortunately. (Hence, a more useful implementation, unlike the sketch above, would not simply die if it fails to visit the input path.)sub cleanpath { my $path; use Cwd; my $cwd = cwd; chdir $path or die "Can't chdir to $path: $!\n"; my $cleanpath = cwd; chdir $cwd or die "Can't return to $cwd$ $!\n"; return $cleanpath; }
Basically, in Unix, if a symbolic links render the path notation ambiguous; in the simplest terms, if foo points to some (non-symbolic) directory bar, then the formal expression foo/.. is ambiguous, because it can be interpreted as either the parent of foo or the parent of bar. (In fact, even foo/. is ambiguous.) The definition of "cleaning up a path" hinges on how you want to resolve this ambiguity. There are uses for both forms of clean-up.
the lowliest monk
In reply to Re: Cleaning up a path
by tlm
in thread Cleaning up a path
by polettix
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |