in reply to Stop buffering on STDOUT
Use while (my $line=<STDIN>) here, not for(each). while() takes its condition one scalar at a time, while foreach() tries to accumulate the entire list in memory at once, which is where you're getting your memory error.
Also, setting the autoflush you only have to do once, not each time around the loop, unless it's getting unset somewhere else in the same program; and $| = 1; would work fine in this case without the rest of the file handle manipulation (though that may of course be more useful in a specialized case).
|
|---|