in reply to speeding up script that uses filehandles

Well yes for a start you might strongly consider changing

foreach my $line (<DATASOURCE>){
to
while(my $line = <DATASOURCE>) {
as the former has to build a list (thus requiring a large memory allocation) before it can start iterating. The latter will simply read and process the lines of the file one by one.

/J\