in reply to Path strings

If you're asking how to portably manipulate path names, File::Spec is the answer. I'm not sure what you mean by needing to "massage the strings into shape" - could you give us an example? Perhaps you haven't found the right method to make things easier.

Update: I missed the emphasis on relative path names in the OP. To build upon my original answer, note that File::Spec has two methods that you might find useful: abs2rel and rel2abs.

Replies are listed 'Best First'.
Re^2: Path strings
by Anonymous Monk on Jan 27, 2007 at 05:05 UTC

    When I use File::Spec->splitpath() the final directory is seen as a file unless it has a trailing delimiter, in which case it is seen as part of the directory. Also the doc on the module states "The directory portion may or may not be returned with a trailing '/'".

    When I use File::Basename->parsefile() the final directory is seen as a suffix unless it has a trailing delimiter, in which case it is seen as part of the directory.

    Also when incorporating other peoples code, this situation crops up.

    When I say "massage", I mean I must add and remove the delimiters from the strings to split them, join them, pass them on etc.

      If you're using splitpath to split a path that ends in a directory rather than a file, then you should be using splitdir instead*. That may solve your trailing '/' issue.

      I don't think I've ever had to manually add or remove delimiters from paths (with or without filenames) when using File::Spec. If you have specific examples that cause you to jump through hoops, please post them. I'm sure the monks around here will provide several approaches to solving the problem. :-)

      *Update: You could use splitpath, but you would need to set the second param to true (or, as you found, add a trailing directory separator). See the docs for more info.

Re^2: Path strings
by Anonymous Monk on Jan 27, 2007 at 05:22 UTC

    When I run:,

    my $curdir = File::Spec->rel2abs(File::Spec->curdir());

    I am returned an absolute path without a trailing delimiter, which I would expect (like) to see.

      A quick look at the source code shows that rel2abs (like many of the functions in File::Spec) calls canonpath. One of the things that function does is remove the trailing delimiter. According to the docs (for catdir, as it turns out), this is because

      it doesn't look good, isn't necessary and confuses OS/2

      I am not as familiar with the functions in Cwd, but after looking at the docs and source code it also appears to leave off the trailing delimiter.

      If you really want a trailing delimiter, then you may have to add it yourself. Sorry. :-)

        With this in mind, I will have to continue with the extra routines to achieve a sturdy solution.

        Thanks for assistance.