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


In reply to Re: dynamic filehandlers - how to deal? by roboticus
in thread dynamic filehandlers - how to deal? by ThirtySecondNoob

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.