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.) | [reply] [d/l] |
/\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?) | [reply] [d/l] |
| [reply] |
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"?
| [reply] |
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.
| [reply] [d/l] [select] |