As BrowserUk said, threads may be an easier choice. I noodled around with this before I saw his reply and came up with this adaptation of your script.

#!/usr/bin/perl use warnings; use strict; use Data::Dumper; use threads; use threads::shared; my %count : shared; my $maxthreads = 3; # there are way more lines than this but anyway... for my $i ( 1 .. 500 ) { redo if ( scalar threads->list() >= $maxthreads ); # this just represents that I've found a pair and want to run the +sub if ( $i % 50 == 0 ) { # run the sub my $thread = threads->new( \&inc, $i, 2 ); } } while ( scalar threads->list() ) { sleep 1 } # I need access to the hash that's updated in the sub print Dumper \%count; sub inc { my ( $param1, $param2 ) = @_; #long subroutine using passed params # pausing for effect! # this simulates that the sub takes a while to run sleep( 5 + ( rand 5 ) ); # let you know something is happening print "$param1\n"; # make a change to the hash $count{$param1} = $param1 * $param2; threads->self()->detach; }

In reply to Re: Is this a job for a fork? by thundergnat
in thread Is this a job for a fork? by richardwfrancis

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.