jackyex has asked for the wisdom of the Perl Monks concerning the following question:

Hello... I want to make a perl script that will use ForkManager and have some v +ariables loop inside like this : 1. I have file x: jacky@ubuntu:~/Desktop$ cat x 1 2 3 4 5 ######################## 2. And I have file y: jacky@ubuntu:~/Desktop$ cat y jack john joe ######################## 3. I than have script.pl: jacky@ubuntu:~/Desktop$ cat script.pl: #!/usr/bin/perl use Parallel::ForkManager; use LWP::Simple; my $file = x; my $pm = new Parallel::ForkManager(3); open($fileh, "<" . $file); while (<$fileh>) { $pm->start and next; $linex = $_; $linex =~ s/\x0a//g; open OUTPUT,">$linex"; print OUTPUT ("$linex\n"); close OUTPUT; $pm->finish } close($fileh); $pm->wait_all_children(); ################################## Now I want inside the line "print OUTPUT ("$linex\n");" to do somethin +g like this "print OUTPUT ("$linex$liney\n");" and loop the content o +f "y" file inside every "$linex" file created...in the end I want the + $linex files with this content: cat 1 = 1jack cat 2 = 2john cat 3 = 3joe cat 4 = 1jack cat 5 = 2john Is that possible? Thank you and please excuse my bad english, Jack

Replies are listed 'Best First'.
Re: Variables loop in Parallel::ForkManager
by ikegami (Patriarch) on Nov 29, 2008 at 21:43 UTC

    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.

      Thank you but it doesn't work...Maybe I'm doing something wrong : #!/usr/bin/perl use Parallel::ForkManager; my $pm = new Parallel::ForkManager(3); 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 (@y) { print $fh_out ("$linex$liney\n"); } $pm->finish } When I perl script.pl it gives me the 5 "$filex" files but like this : jacky@ubuntu:~/Desktop/test$ cat 1 1 1 1 So definatelly there is something wrong in there...Thank you again
        for (@y) should be for my $liney (@y)