#! perl -slw use strict; use threads; use Thread::Queue; sub worker { my $Q = shift; my $count = 0; while( my $path = $Q->dequeue ) { chomp $path; my $file = do{ local( @ARGV, $/ ) = $path; <> }; $count += () = $file =~ m[my]g; } return $count; } our $THREADS //= 4; my $Q = new Thread::Queue; my @workers = map{ threads->create( \&worker, $Q ); } 1 .. $THREADS; open DIR, '-|', q[ dir /s /b *.pl ] or die $!; $Q->enqueue( ); close DIR; $Q->enqueue( (undef) x $THREADS ); my $count = 0; $count += $_->join for @workers; print "Found $count 'my's"; __END__ C:\test>921522-2.pl Found 20825 'my's