ppti has asked for the wisdom of the Perl Monks concerning the following question:

Qn. I have a file which has entries of file paths like below
platform/process/src/../../data/test.dat
So, actual path name is platform/data/test.dat. I tried using canonpath() , rel2abs(). But it does not work.

Is there any other funtion? which travels upwards and does the trick?

Thanks ppti

Here is my code.

#!/usr/bin/perl -w use File::Spec; my $path = "/platform/process/src/../../common/src/line.c"; print $path,"\n"; $new_path = File::Spec->canonpath($path); print $new_path, "\n"; $c_path = File::Spec->rel2abs($path,"/platform"); print $c_path, "\n"; output: /platform/process/src/../../common/src/line.c /platform/process/src/../../common/src/line.c /platform/process/src/../../common/src/line.c
I want /platform/common/src/line.c

Thanks ppti

Replies are listed 'Best First'.
Re: Embedded relative paths
by ikegami (Patriarch) on Dec 12, 2009 at 00:21 UTC

    [ Your node is unreadable. Please add a <p> tag at the start of every paragraph and add <c>...</c> tags around computer text. ]

    platform/process/src/../../data/test.dat
    and
    platform/data/test.dat

    are not necessarily equivalent. For example,

    $ readlink -f /tmp/platform/process/src/../../data/test.dat /tmp/platform/process/data/test.dat

    Notice that

    platform/process/src/../../data/test.dat

    is equivalent to

    platform/process/data/test.dat

    from that directory.

    Tools like File::Spec that don't access the disk can't fold away ".." safely. You need Cwd's realpath

    $ perl -MCwd=realpath -le'print realpath $ARGV[0]' \ /tmp/platform/process/src/../../data/test.dat /tmp/platform/process/data/test.dat

    You can then make it relative again if that's what you desire.

    (Yeah, me bad in the CB)

      Thanks for your input. Got it.