This glob stuff can get hairy as there are multiple versions of it. I've had some big troubles in the past with this in the Windows environment. Adding a grep to process the list from readdir is portable and is just a few more characters to your standard "formula".

Technically a directory is a file, but -f means the subset of files that aren't directories (which excludes '.' and '..'). Be sure to use a full path name in the grep otherwise you will be testing against files in the directory that the program is executing in, which is probably not what you want! When I do want that, I set $dir = "." and use the same formula.

my $dir="/path/to/dir"; opendir(DIR, $dir) or die "can't open $dir: $!"; my @files = grep {-f "$dir/$_"} readdir DIR; closedir DIR;
Updated: added single quotes to make '.' and '..' more clear in some fonts.

In reply to Re: How do you list all files under a directory? by Marshall
in thread How do you list all files under a directory? by daverave

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.