Because that's the way shells do it and File::Glob (and glob as of not-so-recent versions) uses the BSD csh (I believe) globbing implementation.

It might help to think of the character class being a predicate ("Is there a file present in the cwd which has one of these characters at the position in question?"), whereas the curly braces work more as a pattern that you're asking the globbing engine to generate irrespective of the contents of the current working directory ("Give me a list made up of items with the un-braced portion followed by each of the alternatives.").

Yes, it's nonintuitive. But that's the way it works.

Update: Durr, says right on the label: Post 5.6.0 glob is implemented with File::Glob, and I want to say the BSD-based C implementation was made at the same time. So glob should behave more-or-less the same way everywhere for not-unreasonably-old perls.

Shell example: Just as way of proof, here's the way zsh (another shell which implements csh-esque globbing) behaves. Prior to the first command there's no files in the directory.

$ zsh -c 'print foo{a,b}' fooa foob $ zsh -c 'print foo[a,b]' zsh: no matches found: foo[ab] $ touch fooa $ zsh -c 'print foo[ab]' fooa

Just remember that [] is a predicate on directory contents, {} is dumb pattern expansion.


In reply to Re: How glob expands by Fletch
in thread How glob expands by throop

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.