I've found that glob sometimes needs two backslashes in order to recognize some special characters in file names. I only tested it with []{} so far, but will do further testing. First let's prepare the field:

$ mkdir 'a{bc.d'; touch 'a{bc.d/GOT ITabcde' $ mkdir 'a {bc.d'; touch 'a {bc.d/GOT ITabcde' $ ls a {bc.d a{bc.d

Now the game begins:

$ cat foo $a=q|a\ \\\{bc.d/*|;print "pattern <", $a, "> globs to <", (glob $a), +">\n"; $ perl foo pattern <a\ \\{bc.d/*> globs to <a {bc.d/GOT ITabcde>

As soon as I prepend only one backslash to the opening curly, glob fails to expand the directory:

$ cat foo $a=q|a\ \{bc.d/*|;print "pattern <", $a, "> globs to <", (glob $a), "> +\n"; $ perl foo pattern <a\ \{bc.d/*> globs to <a >

On the other hand, it's odd that, it the file doesn't contain spaces, it suffices to use only one backslash:

$ cat foo $a=q|a\{bc.d/*|;print "pattern <", $a, "> globs to <", (glob $a), ">\n +"; $ perl foo pattern <a\{bc.d/*> globs to <a{bc.d/GOT ITabcde>

In fact, using three makes glob produce no output:

$ cat foo $a=q|a\\\{bc.d/*|;print "pattern <", $a, "> globs to <", (glob $a), "> +\n"; $ perl foo pattern <a\\{bc.d/*> globs to <a\>

This happens in all these 5 systems:

I think that the shell must be involved at some point when the pattern has spaces, so the two backslashes become one. The doc of File::Glob says something but I'm not sure it's related ("Due to historical reasons, CORE::glob() will also split its argument on whitespace, treating it as multiple patterns").

FWIW, perl, version 5.005_02 built for PA-RISC1.1 gives internal error: glob failed at foo line 1. on HP-UX td192 B.11.11 when it sees more than one backslash in a row. All four tests work as expected (ie, perform the glob correctly) if I use just one backslash. I'm downloading some old versions of Debian to test 5.6 and 5.004.

Ideas?

Update: Added perl 5.6.1.

--
David Serrano


In reply to glob with special characters by Hue-Bond

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.