use strict; use warnings; use IO::Select; use IO::File; use Data::Dumper; my @childs; foreach my $kid(qw(foo bar baz quux)) { # Create and gather child processes/fd's push @childs, child_labor($kid); } my @results; my @files_ready; my $file_iter = IO::Select->new(); $file_iter->add(@childs); while (@files_ready = $file_iter->can_read(1)) { for (@files_ready) { my $fd = shift @{$_}; my $child = shift @{$_}; chomp(my $data = <$fd>); if ($data) { push @results, [ split ":", $data ]; } sleep 10; } } # Printing some results... print Dumper(@results); sub child_labor { my $name = shift; my $fd; defined (my $pid = open $fd, "-|") # Indirect fork, read child stdout or die "Couldn't fork: $!"; # Parent... if ($pid) { return [ $fd, { pid => $pid, name => $name } ]; } # Child... else { $0 .= " $name"; my $counter = 0; while (1) { # Generate some output for our parent to read print "$name:" . 100 * (int(rand(100))+1) . "\n"; sleep int(rand(10)); last if $counter++ > 13; } # Important to exit from the child. exit 0; } }