Adapt to your path and search pattern:
opendir (DIR, "C:/temp") or die "unable to open C:/temp $!\n"; my @files2 = grep{/\.pl$/}readdir DIR; print join("\n",@files2),"\n";
The above gets only the file names, if you want the full path, then my @files2 = map{"C:/temp/".$_}grep{/\.pl$/}readdir DIR; The glob operator can be used also albeit there are pitfalls as there are several different incompatible glob()'s out there.

my @files = glob("C:/temp/*.pl"); print join("\n",@files),"\n"; #full paths
update: if you want all the files in a directory, then
opendir (DIR, "/root") or die "unable to open /root $!\n"; @all = grep{-f}readdir DIR;
readdir returns all files. Directories are a special type of file. the files . and .. are files. The -f file test in the grep will filter the output down to all "normal" files (no directories or . and .. entries).

Update: Well I think I mis-understood the question. I guess the question should have been:
I want an efficient way to search through a directory and get a list of files, each of which contain a certain pattern. .
I think using command line grep is just fine. What about that solution is not "fast enough" or "efficient enough" for the application? Are you having trouble capturing and using the output of the command line grep command? Is this a Windows box and you don't have grep. Please explain the problem further.

Every file will have to be opened and read sequentially until either the desired pattern is found or EOF is reached - that is true no matter what you do. A Perl program can use a different regex syntax than the command line grep, but that was not even part of the question.


In reply to Re: Find filename that has the pattern in a directory by Marshall
in thread Find filename that has the pattern in a directory by shawshankred

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.