Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: Collapsing paths

by lodin (Hermit)
on Dec 05, 2007 at 12:45 UTC ( [id://655076]=note: print w/replies, xml ) Need Help??


in reply to Collapsing paths

canonpath in File::Spec seems to do the trick.

use File::Spec::Functions 'canonpath'; print canonpath('foo/../bar/../baz/'); __END__ baz

lodin

Update: added examplifying code, but see ikegami's reply below.

Replies are listed 'Best First'.
Re^2: Collapsing paths
by tirwhan (Abbot) on Dec 05, 2007 at 12:52 UTC
    canonpath in File::Spec seems to do the trick.

    Did you try it out?

    #!/usr/bin/perl use File::Spec; print File::Spec->canonpath("foo/../bar/../baz/")."\n";

    Output:

    foo/../bar/../baz

    I guess not. Did you read the docs? Quote:

    Note that this does *not* collapse x/../y sections into y. This is by design.
    I guess not.

    All dogma is stupid.
      Depends on your system.
      use File::Spec; print File::Spec->canonpath("foo/../bar/../baz/")."\n";
      \baz

      On system without symlinks, canonpath collapses x/../y (although not always correctly, as shown above).

      Update: Technically, NTFS does have symlinks, but most software act as if they don't exist, including File::Spec.

      Did you try it out?

      Yes I did. That's why I wrote "seems". I didn't know the behaviour was platform dependant. The documentation for File::Spec is contradicting the File::Spec::Win32 documentation which specifically says "foo/../bar" will become "bar".

      lodin

Re^2: Collapsing paths
by rvosa (Curate) on Dec 05, 2007 at 12:55 UTC
    I don't think it does, it looks like it removes extraneous slashes, but doesn't collapse ../../ path fragments.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://655076]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (4)
As of 2024-03-19 07:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found