in reply to dynamic filehandlers - how to deal?

ThirtySecondNoob:

You're not that far off with your attempt. You're going through a few unnecessary steps to check whether the file is open and for storing the file handle, though. I've cleaned up your example a little, here (usual disclaimers apply):

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

While going through your example, though, I feel it's possible you may have trimmed a bit too much from it. In case you wanted to keep several different files open at once (such as for processing interleaved requests), you might want something like this:

my $MAX_FILES=10; # Max open files we want my %files; # We can keep multiple file handles 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 (!exists $files{$file_name}) { # close a file if we have too many my @filenames = keys %files; if (@filenames >= $MAX_FILES) { my $t = $files{$filenames[0]}; close $t or die $!; delete $files{$filenames[0]}; } open $files{$file_name}, '>>', $filename or print "$file_name could not be created\n"; } $file = $files{$file_name}; print $file $new_line."\n" or print "data COULDNT be printed to CURRENT_FILE\n"; } #end of while loop

...roboticus