If your not stuck on using processes, it's pretty simple with threads.

#! perl -slw use strict; use threads; use Thread::Queue; our $W ||= 3; my( $dir, $re ) = @ARGV; my $Q = new Thread::Queue; my $running : shared = 0; sub worker { $running++; sleep 1; while( $Q->pending ) { my $file = $Q->dequeue; print "Processed $file"; } $running--; } $Q->enqueue( grep{ m[$re] } glob "$dir/*" ); threads->new( \&worker )->detach for 1 .. $W; sleep 1 while $running < $W; sleep 1 while $running; __END__ [ 1:55:22.37] P:\test>419115 . htm$ Processed ./370949.htm Processed ./376142.htm Processed ./377082db.htm [ 1:55:31.92] P:\test>419115 data dat$ Processed data/1mb.dat Processed data/1millionlines.dat Processed data/5mb.dat Processed data/bin.dat

Examine what is said, not who speaks.
Silence betokens consent.
Love the truth but pardon error.

In reply to Re: to limit active processes by BrowserUk
in thread to limit active processes by happi

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.