Both. :-)

It comes from shell glob expansion behaviour. That is what the shell does to the command line before it passes it on to the program. So if you have just listed some arguments, it will pass along those arguments. If you have listed some arguments with wildcards, it will expand those wildcards into a file list. And so on.

It even has some features to make it easy to write things like this:

cp some-long-filename{,.bak}
(Fans of Perl golf sometimes find find the treatment of {} very useful.) This is very convenient in an interactive command-line environment. Shell scripters were also used to using this, and Perl just borrowed the same because Larry was trying to leverage existing knowledge.

That said, if you don't come to Perl with this background, Perl's glob behaviour is very surprising. And can lead to lots of subtle bugs. Plus when I'm in defensive mode I don't trust programs that will do different (and surprising!) things depending on whether the file I'm looking for is there.

So it's a feature. But surprises and can lead to bugs.

Which is why in "real code" I tend to use opendir and readdir directly (possibly hidden behind a function).

PS If you want behaviour closer to Ruby's, just grep for files:

perl -e 'print grep -f, <x.>'

In reply to Re: Perl's glob behaviour by tilly
in thread Perl's glob behaviour by Anonymous Monk

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.