Also check Suffering from Buffering, you might want to set $| to a nonzero, this can alleviate the 'out of memory!', and just like jethro suggested, consider divide and conquer...
Excellence is an Endeavor of Persistence.
Chance Favors a Prepared Mind.