in reply to pattern and loop

You've got two nested while loops, both reading from the same filehandle. That may not be what you want.

Here's what'll happen: the outer loop will read lines from the file. As soon as a match is found (if ($line1 =~ /$pattern/)), the second, inner loop will start and continue reading from where the first loop left off. When it's done, the filehandle is exhausted, and the outer loop, noticing that there's no data left to read from the file, will terminate.

Note that

  1. the inner loop will not start reading from the start of the file, and
  2. the outer loop will not pick up where you left off before the inner loop started again.

Either of these may well be intended, expected behavior, but I'm mentioning it just in case.

If it's not intended/expected, you can use tell and seek to save the filehandle's current position and restore it, though there'll likely be easier, more elegant ways of achieving what you want to achieve.