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):

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

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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.