You can use
while ( s///g ) safely (note the
/g modifier). In scalar context,
s///g will restart matching where the last match left off, so no infinite loop. However, tracing lines and particularly line numbers will be hard.
Since you mention changed lines and line numbers it appears that you expect changes to be limited to single lines, as opposed to changes spanning multiple lines. If so, you'd be much better off looping over the file line by line and logging changes as they happen. Note the $. variable, which will give you the line number.
Anno