What is an example of your input?
My best guess is that the file is being read as one big string and so the while loop would execute just once or twice. Try adding a counter to see how many times the loop is being executed.
my $counter = 1;
open (MYFILE,'test.txt');
while (<MYFILE>) {
...