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

such as A:\B.CD(E)~!@#$%^&\E\F.txt, I want to get relative path of file F.txt to folder "A:\B.CD(E)~!@#$%^&", so the expected result is ".\E\F.txt"

Replies are listed 'Best First'.
Re: How to get relative path in perl?
by ikegami (Patriarch) on Jan 18, 2010 at 01:56 UTC

    I prefer Path::Class

    use Path::Class qw( file ); print( file('A:\\B.CD(E)~!@#$%^&\\E\\F.txt') ->relative('A:\\B.CD(E)~!@#$%^&'), "\n" );

    You could also use File::Spec::Functions

    use File::Spec::Functions qw( abs2rel ); print( abs2rel('A:\\B.CD(E)~!@#$%^&\\E\\F.txt', 'A:\\B.CD(E)~!@#$%^&'), "\n" );

    Both of the above return E\F.txt because .\E\F.txt is redundant. You can prepend .\ to the result if you insist on its presence.

      I'm using the second one, because Path::Class is not found and i'm not going to download one.. Thanks very much, that solved my problem!