I agree with
AppleFritter and would add further that all these nested while and for loops make very little sense if your whole file is going to be read in just one swoop of the most inner
while (<WGL>) loop. So your outer loops are executed usefully only once, which means you might not need them as loops. Or, possibly, you need some condition to go out of the most inner loop.