22\r\n
4444\r\n
333\r\n
After reading the second line, I think tell() will report the file position as 10:
01 2 34567 8 90
22\r\n4444\r\n
And the length of the second line as reported by length($line) in your code will be 5: when reading a line from a file, perl automatically converts the actual newline found in the file(in this case \r\n) to \n, so $line will be assigned "4444\n", and length($line) will be 5. So $pos = 10 - 5 = 5. Then seeking to position 5 in the file will land you here:
22\r\n
4444\r\n
^
file pointer
...which is not the start of the line. Can anyone test that on windows?
Using seek() to move to the beginning or end of the file will work because those are absolute positions--but doing a relative seek() won't work because counting the length of a line with a perl program does not give you the actual length of the line in the file. To do relative seeks, you need to turn off the automatic newline conversions that occur while reading a file by using something like binmode(). |