in reply to Delete files with Perl in Windows

You need to escape spaces in a glob spec unless you want them to be treated as pattern seperator.

>perl -le"print for glob '*.jpg *.gif'" a.jpg b.jpg c.gif >perl -le"print for glob 'C:/Documents and Settings/*'" C:/Documents and >perl -le"print for glob 'C:/Documents\ and\ Settings/*'" C:/Documents and Settings/All Users C:/Documents and Settings/Default User C:/Documents and Settings/ikegami C:/Documents and Settings/kaijugal C:/Documents and Settings/LocalService C:/Documents and Settings/NetworkService

Alternatively, use bsd_glob from File::Glob.

>perl -le"use File::Glob qw( bsd_glob ); print for bsd_glob 'C:/Docume +nts and Settings/*'" C:/Documents and Settings/All Users C:/Documents and Settings/Default User C:/Documents and Settings/ikegami C:/Documents and Settings/kaijugal C:/Documents and Settings/LocalService C:/Documents and Settings/NetworkService

Replies are listed 'Best First'.
Re^2: Delete files with Perl in Windows
by codeacrobat (Chaplain) on Nov 30, 2007 at 06:41 UTC
    If you don't want to set the escapes manually, then quotemeta is your friend.
    perl -le "print for glob quotemeta q(C:/Documents and Settings/*)"

    print+qq(\L@{[ref\&@]}@{['@'x7^'!#2/"!4']});

      Sorry, but that doesn't work. The reason it doesn't work depends on the system.

      • On non-dosish systems, that will escape meta-characters that should remain meta-characters, such as *.

        That means glob quotemeta $expr is the same as ''.$expr.

      • On dosish systems, that will insert characters that will be interpreted as directory separators.

        On DOSISH systems, [...] backslashes (under GLOB_QUOTE) only quote the glob metacharacters '[', ']', '{', '}', '-', '~', and backslash itself. All other backslashes are passed through unchanged.

        That means glob quotemeta 'c:/some dir/*.*' is the same as glob 'c:/some dir/*/.*'.

        To reflect on ikegami's reply, the reasoning is even simpler: quotemeta is intended to quote the regular expression metacharacters, not the metacharacters of all special syntax string parsing problems. Quotemeta is for perl regex only.

        The wildcards in glob() are system-specific; SQL parameter value quoting characters are different from that, and the metacharacters in perl formats, POD and string interpolation are also mini-languages unto themselves. Even for other regex engines, this decision does not hold. How to "escape" these characters also differs between each standard. You escape regex with backslash, sql with arcane quotemarks (or placeholders), format/POD with blank lines or comment marks or tricks with curly braces.

        It just so happens that for perl 5 regex, anything that's not a letter or digit might be considered a metacharacter, and could need escaping. The quotemeta() function for perl 5 thus escapes non-letter, non-digit characters, whether they are actually regex special or not. In deciding regex syntax, it was deemed simpler to remember what should be escaped this way. That's likely not what is required for SQL, POD or other usage patterns.

        --
        [ e d @ h a l l e y . c c ]