How counter in the file is moving to next row when I am not moving it and I am also not using any loops anywhere? I would just like to understand that? Does that mean loops are not required for moving through the file or I am getting it wrong somewhere?

Thanks in advance for any help on this!

Moderator's Comments:

Use code tags please, see PM.

Last edited by zaxxon; 07-12-2013 at 07:51 AM..
Reason: code tags and indention

Everytime you do read from a filehandle and store it in a scalar variable, then one line of input is read. (By one line, I mean the data until the next record separator).

Here is how it works internally:
When you open a filehandle, lets say, there's a mark that is at the beginning of file. The first time $rec = scalar <$INF> is encountered, the first line from file is read and referred by $rec. Now, the mark that we spoke of earlier, is at the character just after newline character (assuming newline as the default record separator). Now again when $rec = scalar <$INF> is encountered, one more line of data is read.

In the example code you provided in your post, a wrapper routine "readfile" reads one line of data and prints it.

The usual way of reading a file is to use a loop. Then again, that depends on what you really want to do: