#! /usr/bin/env perl use strict; use warnings; use Getopt::Long; use POSIX ":sys_wait_h"; GetOptions('-i=s' => \my $infile); my ($counter,$index,@lines) = (1,0); open my $fh,"<",$infile or die "Can't open $infile: $!"; while(my $line = <$fh>){ push @{$lines[$index]},$line; $index++ if $counter++ % 10 == 0; } fork_it(\@lines); #------------------------------------------# # Subroutines # #------------------------------------------# sub do_something_with_lines{ my ($lines,$nr) = @_; print $nr,": ",$_ for @$lines; } sub fork_it{ my ($linesref) = @_; my %pids; for (1..scalar(@$linesref)){ my $pid=fork(); if($pid==-1){ warn($!); last; } if($pid){ $pids{$pid}=1; } else{ do_something_with_lines($linesref->[$_-1],$_-1); exit(0); } } while(keys %pids){ my $pid=waitpid( -1, WNOHANG ); die "$!" if $pid == -1; delete $pids{$pid}; } }