waswas-fng's idea is the first thing I would try; something like this:
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; }
Note that this idea doesn't exactly match what you said:
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.

In reply to Re: Tracking Lines Printed to a FileHandle by graff
in thread Tracking Lines Printed to a FileHandle by ketema

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.