my $fh; open($fh,'>',$output) or fail("Failed to read $output",$!); binmode($fh); my $csv; $csv = Text::CSV_XS->new ({ binary => 1, eol => $outputeol, sep_char => $outputdelim, quote_char => $outputenc, escape_char => $outputesc }) or fail("Failed to create output CSV object",Text::CSV->error_diag()); while(1) { my $th = $q->dequeue; last unless(defined $th); my $chunk = $th->join(); foreach my $outputs(@$chunk) { $csv->print($fh,$outputs); } } close($fh);