in reply to My program hangs

You are reading from the wrong filehandle:

open (my $fh, '<' ,$filename) or die "ERROR: could not open $filename: + $!"; while (<>) {

Instead of reading from standard input (or the file passed on the command line), you want to read from the filehandle you just opened, $fh:

while (<$fh>) {