use strict; use warnings; use Compress::Bzip2; my $error_file = "errors.txt"; open ERRORS, ">$error_file"; my @files = glob("$path"); my $correct_number_of_lines = 244812; while (@files) { # Code adapted from http://hell.jedicoder.net/?p=82 my @children = (); my %bz_handles = (); for(1..8) { my $file = shift @files; my $pid = fork(); if ($pid) { # Parent push @children, $pid; } elsif ($pid == 0) { # Child # Using bzcat function adapted from Compress::Bzip2 # documentation my $number_of_lines = 0; my $line = ""; # I tried $bz = bzopen($file, "rb") first but it # didn't work so I figured a hash might be better to # generate a new variable each time? $bz_handles{$pid} = bzopen($file, "rb") or print ERRORS "Cannot open $file" . "\n"; while ($bz_handles{$pid}->bzreadline($line) > 0) { $number_of_lines++; } if ($bz_handles{$pid}->bzerror != BZ_STREAM_END) { print ERRORS "Error reading from $file" . "\n"; } $bz_handles{$pid}->bzclose() ; if ($number_of_lines != $correct_number_of_lines) { print ERRORS $file . "\t" . $number_of_lines . "\n";; } exit(0); } else { die "couldn't fork: $!\n"; } } foreach my $child (@children) { waitpid($child, 0); } }