Without forking, it would be
open(my $fh_x, '<', 'x') or die $!; while (<$fh_x>) { chomp( my $linex = $_ ); open my $fh_y, '<', 'y' or die $!; open my $fh_out, '>', $linex or die $!; while (<$fh_y>) { chomp( my $liney = $_ ); print $fh_out ("$linex$liney\n"); } }
So with forking, it would be
open(my $fh_x, '<', 'x') or die $!; while (<$fh_x>) { $pm->start and next; chomp( my $linex = $_ ); open my $fh_y, '<', 'y' or die $!; open my $fh_out, '>', $linex or die $!; while (<$fh_y>) { chomp( my $liney = $_ ); print $fh_out ("$linex$liney\n"); } $pm->finish }
Since every thread reads file y, you could preload it into memory to save the threads from all doing the same work.
Without forking, it would be
my @y; { open my $fh_y, '<', 'y' or die $!; chomp( @y = <$fh_y> ); } open(my $fh_x, '<', 'x') or die $!; while (<$fh_x>) { chomp( my $linex = $_ ); open my $fh_out, '>', $linex or die $!; for my $liney (@y) { print $fh_out ("$linex$liney\n"); } }
So with forking, it would be
my @y; { open my $fh_y, '<', 'y' or die $!; chomp( @y = <$fh_y> ); } open(my $fh_x, '<', 'x') or die $!; while (<$fh_x>) { $pm->start and next; chomp( my $linex = $_ ); open my $fh_out, '>', $linex or die $!; for my $liney (@y) { print $fh_out ("$linex$liney\n"); } $pm->finish }
Update: Added preloading version.
Update: Fixed bug mentioned in reply.
In reply to Re: Variables loop in Parallel::ForkManager
by ikegami
in thread Variables loop in Parallel::ForkManager
by jackyex
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |