my ($t0, $path0, $out); $t0 = time; $path0 = path_for_time($t0); open($out, ">>", $path0); # note below while (defined(my $line = readline())) { my $t = time; my $path1 = path_for_time($t); if ($path1 ne $path0) { close($out); $path0 = $path1; open($out, ">>", $path0); } print $out $line; flush($out); # note below ... perform other processing on $line ... }