If you manually close ARGV when you get to eof of a particular file (see eof), $. restarts at 0. Another thing you can do is check when a new file is opened (@ARGV will have one less element, the filename stored in $ARGV will be changed.)
By the way, there seems to be no reason for you to do the s///, since you aren't printing the line.