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:
- You're not using lexical (my) variables, so you're probably not using strict. Until you know why and when it's ok not to use it, you should use strict;
- file handles (list and infile in your example) by convention are always in uppercase;
- if you are not interpolating Perl variables into strings or using meta characters like \n and \t, use 'single quotes' instead of "double quotes";
- and if you are not diddling with subroutine parameter lists, do not use the &func() way of calling function, drop the ampersand. If you don't know what diddling with subroutine parameter lists means, then just avoid using & on subroutine calls.
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';
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.