in reply to Is this a job for a fork?
Threads would be much simpler.
This could be refined in any of a number of ways given a real problem description, but just supplying the filename as an input argument should work:
#! perl -slw use strict; use threads ( stack_size => 4096 ); use threads::shared; use Thread::Queue; sub process { my $tid = threads->tid; my( $hashref, $Q ) = @_; while( my $key = $Q->dequeue ) { print "$tid : Processing $key"; sleep 10; lock $hashref; ++$hashref->{ $key }; } } our $T //= 16; my %hash :shared; my $Q = new Thread::Queue; my @t = map threads->create( \&process, \%hash, $Q ), 1 .. $T; while( my $key = <> ) { chomp $key; if( exists $hash{ $key } ) { $Q->enqueue( $key ); } else { $hash{ $key } = 0; } } $Q->enqueue( (undef) x $T ); $_->join for @t;
|
|---|