Why don't you read the directory entries with internal Perl functions, instead of relying on some external program (OLDTTGW principle):
Fill_List( '/dir', '^error' ); sub Fill_List { my $dir = shift; my $match = shift; my @found; opendir DIR, $dir or die "Cannot open directory $dir: $!"\n; while( defined( my $file = readdir(DIR) )) { push @found, $file if $file =~ /$match/o; } close DIR; return @found; }

Note that I am returning the values in an array. It is better to have a routine do one thing, and one thing only. If you want to write the contents out to a file, do that in another routine. Prepare for (obvious) future uses the code could be put to.

Your code has a few style issues that leap out at me:

update: oops, re-reading my code makes me realise this code will only work correctly the first time it is called, due to the /o modifier on the regexp. (It will just keep on returning files that match what the first call was asked to match. The correct method would be to use the qr// operator if the version of Perl supports that. Just put $match = qr/$match/; before the while loop. Otherwise, for earlier versions, one would have to build the loop up in a string, and eval that, which is downright icky.

--
g r i n d e r
just another bofh

print@_{sort keys %_},$/if%_=split//,'= & *a?b:e\f/h^h!j+n,o@o;r$s-t%t#u';

In reply to Re: File problems by grinder
in thread File problems by coec

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.