in reply to Sub routines

On top of what's already mentioned, also note that each iteration of your loop, you process three lines:
  1. The guard of the while reads a line, puts it in $_. This line is processed (by chomp and split).
  2. A line is assigned to $nextline. Which is then unused.
  3. Inside events, another line is read, assigned to another $nextline. This one remains unused as well.