I fail to see the irrelevance. OP asked about capturing line number, and a counter variable is a tad clunky. Using $. is a very simple way of doing it, and the $.->$_ scheme is an easy way to keep track of it all in an intuitive way. It is true that the regex may be m/^#/ is overly simple (then again, perhaps not...), but that can be fleshed out; all in all this seems a simple, pure-perl way to do the job.