my $prev_file_name; my $file; while(1) { my $new_line = get_line_of_code(); my $line_time = get_date_stamp($new_line); my $file_name = "$DATA_DIRECTORY$line_time.ascii"; if (defined($prev_file_name) and $prev_file_name != $file_name) { close($file) if defined $file; open $file, '>>', $file_name or print "$file_name could not be created\n"; } #add line to $NEW_FILE print $file $new_line."\n" or print "data COULDNT be printed to CURRENT_FILE\n"; # This bit seems to be related to code you've pruned from # your example... #} else { # #write to file and send # $file = $fileholder[0]; # print $file $new_line; #} } #end of while loop