in reply to Determining what line a filehandle is on

As you can see from the above responses there are a few ways to do it, but there are a couple of things to watch out for if you decide to use the special Perl variable $. which is its special behavior that arises when reading from the the empty diamond operator (<>) and also when you localize the variable itself.

Basically I am 99% sure you just want to increment a counter and keep track of the line number yourself. Since the file will open at the beginning and read one line each time the while statement loops, a simple $line++ statement inside that loop should be sufficient.

In addition to checking that open succeeded, I'd also recommend adding \n to the end of your print statement..

  • Comment on Re: Determining what line a filehandle is on

Replies are listed 'Best First'.
Re: Re: Determining what line a filehandle is on
by Anonymous Monk on Jul 08, 2001 at 20:37 UTC
    Not to mention that $. only provides you with the lineno of the last filehandle read. If you are interspersing reads from 2+ files, this can come back and bite you. In these cases, it's much easier to maintain an index for each file.

    Mark.