in reply to Re: Delete files with Perl in Windows
in thread Delete files with Perl in Windows

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']});

Replies are listed 'Best First'.
Re^3: Delete files with Perl in Windows
by ikegami (Patriarch) on Nov 30, 2007 at 08:58 UTC

    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 ]

        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.

        I believe "its" intent is to perform as documented. quotemeta is documented to escape all characters except /[A-Za-z_0-9]/. Regular expressions have nothing to do with it. quotemeta can be used anywhere such an escaping scheme is acceptable, including to quote text to include in generated

        • regular expressions (/\Q$text\E/),
        • sh commands (system("tool \Q$arg\E 2>&1")),
        • non-Windows glob patterns (glob("\Q$path\E/*")),
        • Perl code (qq{"\Q$text\E/"}),
        • etc.

        (Note that \Q..\E is just a shortcut for quotemeta.)

        The wildcards in glob() are system-specific

        Not anymore. They are documented in File::Glob.

        Update: Added code samples.