To provide a little more detail on what's happening here, your code, as written, reads the entire file in at once. If the file is in GB, you'll need GB of memory dedicated to that process just to handle the data--not counting the memory consumed by the OS, Perl itself, and so forth.

As others have suggested, you need to iterate over the filehandle, so you read one line at a time, and operate on it. You may need an intermediate step of writing the small files out unsorted, then rereading them to sort them, if they need internal sorting, since you can't hold the entire dataset in memory.

Additionally, you would be better off to use 3-argument open and a scalar filehandle:
open my $input_fh, '<', $filename or die $!;
unless you're absolutely certain your filename string is free of characters that could be misinterpreted by 2-argument open (and frankly, it's a good habit to be in anyway.) The scalar filehandle gives you scoping, which bareword filehandles don't really have.

(And, as others have also pointed out, you should follow the markup directions right below the box you write your post in that say not to use <pre> tags.)

In reply to Re: out of memory error while reading file contents into an array by ssandv
in thread out of memory error while reading file contents into an array by psr473

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.