The special symbol $. contains the line number of the file you are reading. If you only want the frist 1000 lines, put "last if $. > 1000 just after the start of the while loop. If you want a group from the middle or end just keep track of $. and only start using the lines when you get to where you want to start. If you want the last 1000 lines in the file and it's a big file, you probably should use
File::Tail