Hi, here is a script that will work for you. I use the @ARGV trick to loop thru each line in a directory full of files, and do a regex, then cancel all forks if a regex matches. You don't need to worry about different filehandles, doing it this way. (This works very fast, so you may get 2 matches by the time the forks can be shut down, but that shouldn't be a problem.
#!/usr/bin/perl
use warnings;
use strict;
use Parallel::ForkManager;
my $dir = shift || '.';
my @dirs = get_sub_dirs($dir);
my $max_tasks = 3;
my $pm = new Parallel::ForkManager($max_tasks);
$|++;
my $start = time();
for my $dir (@dirs) {
my $pid = $pm->start and next;
printf "Begin processing $dir at %d secs.....\n", time() - $start;
#push all the $dir/files into @ARGV and search through them
#line by line
@ARGV = <$dir/*>;
while (<ARGV>) {
close ARGV if eof;
if( $_ =~ /desktop/){
print "$ARGV: $. :$_\n";
$pm->finish;
goto END;
}
}
END:
printf ".... $dir done at %d secs!\n", time() - $start;
$pm->finish;
}
print " all done\n";
exit;
##########################################################
sub get_sub_dirs {
my $dir = shift;
opendir my $dh, $dir or die "Error: $!";
my @dirs = grep { -d $_ } readdir $dh;
@dirs = grep !/^\.\.?$/, @dirs;
closedir $dh;
return @dirs;
}
I'm not really a human, but I play one on earth.
flash japh
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.