Re: Windows paths in glob



On 2008-03-30 19:27, Martijn Lievaart <m@xxxxxxxxxxxxxxxx> wrote:
On Sun, 30 Mar 2008 19:09:18 +0000, Dmitry wrote:
OK, so there's a well-known difficulty with handling Windows-style paths
in glob: it doesn't like backslashes, nor does it like spaces. One
solution to that is to use Unix-style paths:

glob('C:\Documents and Settings\*'); # Doesn't work
glob('C:/Documents\ and\ Settings/*'); # Works

I didn't expect that but on second thought it makes sense.


Problem is, the rest of Perl's built-in file-handling functionality
behaves the other way around. For instance, with -d:

-d 'C:\Documents and Settings'; # Works -d 'C:/Documents\ and\
Settings'; # Doesn't work

Question: is there any way to use the same path string with glob and
with the rest of Perl, without having to convert them back and forth?

I don't have Windows to test here, but I recall that using either a
forward slash '/' or a backward slash -- properly escaped -- '\\' works
either way in both situations.

You misunderstood the problem. The problem is that glob patterns, like
regexps are mini-languages where some characters (or sequences of
characters) have a special meaning. Just as you cannot just use any
string as a regexp and expect it to match itself (or even be a
well-formed regexp) you cannot use any filename as a glob pattern and
expect it to expand to itself. Actually, for globs the situation is
worse: While any string can be converted to a regexp matching that
string, this is not true for globs. Spaces can be escaped with a
backslash, but I didn't find any way to escape an asterisk or question
mark.

So I guess Gunnar's advice is the best: If you need to deal with
arbitrary file and directory names, avoid glob and use opendir/readdir.
Or maybe File::Find or a similar module (which uses opendir/readdir
internally).

hp
.



Relevant Pages

  • Re: bash scripting problem : $@ vs $1
    ... I can think of a very good reason why you would want a script to receive ... If i could get the string with the glob ... characters in tact, then I could do my sanity check on a string of not ...
    (alt.os.linux)
  • Re: Multiple vendors FTP denial of service
    ... Normally when I write code to sanatise a user entered path with glob ... in it I process the string to remove any directory name ... /nonexistant/../existant/ succeeds where it shouldn't. ... >> directories FTPD can access. ...
    (FreeBSD-Security)
  • Re: only reading every other directory
    ... > Use of uninitialized value in concatenation or string at test.pl line 9. ... list context, this isn't important because you automatically get them ... or "undef" when the list has run out. ... glob occurs in the test part of a "while", ...
    (perl.beginners)
  • Re: str() and repr() question
    ... str = DoTestTime ... Script1.py:1: Imported module (glob) not used ... Script1.py:1: Imported module (string) not used ...
    (comp.lang.python)
  • Re: Escaping blog patterns
    ... > I have a question about escaping the spaces in glob patterns. ... > In my case, that string is not typed literally, ... You can simply escape spaces yourself ...
    (comp.lang.perl.misc)