in reply to Tracking Lines Printed to a FileHandle
Note that this idea doesn't exactly match what you said:my $max_line_count = $whatever; my $file_count = my $line_count = 0; my $outfh; sub printAndCount { local $_ = shift; my $lines2print = tr/\n/\n/; # simple way to count \n's if ( $file_count == 0 or $line_count + $lines2print > $max_line_count ) { close $outfh if ( $file_count ); open $outfh, sprintf( ">track%03d.txt", ++$file_count ) or die + "open failed: $!"; } print $outfh; $line_count += $lines2print; }
The end goal is that when a cetain number of lines is reached i want to close the file handle, and open a new one with the same name that points to a different file.You can't "open a different file" using the same file name over and over. If you don't use a different file name each time, you'll simply be deleting the last set of lines that you wrote as soon as you open a "new" file.
|
|---|