in reply to File problems
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.
--
|
|---|