my %fh; # store handles of new log files open OLDLOG, $path_to_old_log or die $!; while () { my $proc = extract_process_id_from_old_log_record($_); unless (exists $fh{$proc}) { open $fh{$proc}, '>', "$proc.log" or die $!; } print $fh{$proc} $_; }