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

I have a scalar with a directory like "/home/user/perl/scripts/". This directory does not exists so I cannot check it with -d .
What I want to do is to validate that this string truly is a well-formed path. How do I do it ?

Replies are listed 'Best First'.
Re: How to validate a directory string?
by moritz (Cardinal) on Sep 06, 2008 at 11:08 UTC
    On most Unix systems any string that doesn't contain a null byte is a valid directory name, so that's what you could test (presumably that's m/\A[^\0]+\z/, not tested though.)
      There are good reasons to be a little more selective than that. For instance, allowing things that are not visible ascii characters is generally a bad idea, and including visible ascii characters that typically relate to "shell magic" (vertical bar, ampersand, the various brackets and quotes) can also lead to trouble. More and more, unix-heads are getting used to handling spaces in file names, but allowing TAB, CR and LF as well would be asking for trouble, IMHO.

      I'd be more inclined to something like

      /\A[^\x00-\x1f\x7f&*?|\\"'`<>{}()[\]]+\z/ # allows space and "safe" punctuation
      Of course, that still doesn't address the question of bytes outside the 7-bit ascii range, which includes the whole domain of utf8 wide characters. For that part of the issue, there isn't enough information in the OP to provide a basis for appropriate advice. (Would utf8 file names be allowable/sensible? What is expectable/possible regarding non-ascii content in the input strings?)
Re: How to validate a directory string?
by jettero (Monsignor) on Sep 06, 2008 at 11:10 UTC

    I'd guess File::Spec would help with that.

    -Paul

Re: How to validate a directory string?
by graff (Chancellor) on Sep 06, 2008 at 14:53 UTC
    Can you be a little more specific about what you consider to be "well-formed"? e.g.: Should it always be an absolute path, starting with "/"? Do you want to be really picky and avoid things like "/home/user/../otheruser/../user/perl/scripts/"?

    What's the likelihood that your scalar might contain values (bytes or characters) above the ascii range? Would you consider such values "well-formed"?

Re: How to validate a directory string?
by Your Mother (Archbishop) on Sep 06, 2008 at 16:10 UTC

    You could probably just try making it and if it succeeds it's valid.

    use File::Path; eval { mkpath([...args...]) }; # deal with failure or succes

    However, I'm with graff. You should whitelist the smallest set of input you'll allow first -- like [-_a-z/] -- and pass it through Path::Class or something to normalize it, or you'll eventually end up with a bad surprise given enough user input.