in reply to Re: 'seek' on a FileHandle.pm object
in thread 'seek' on a FileHandle.pm object

Let me see if I got this straight now, after rereading the definition of 'seek' as according to Programming Perl, 3rd Ed.(I think you know that book... =).

If 'seek' is called like so:

seek( FH, 0, 0 );
...the first argument refers to the filehandle, the second argument to the offset, and the third one of three positions in the file(beginning, current, end).

I see my mistake was thinking of 'offset' as equivalent to line numbers, which it is not. It refers to byte positions. So, my new question is: Is 'byte positions' really as simple as it sounds(the positions of the byte in the file)? So if I knew the exact byte position that I wanted to seek to I could enter it as the offset argument and be taken right there?

Amel - f.k.a. - kel

Replies are listed 'Best First'.
(bbfu) Re(3): 'seek' on a FileHandle.pm object
by bbfu (Curate) on Apr 17, 2001 at 23:11 UTC

    You are correct in your interpretation. And it is as simple as that. It only gets complicated if you need to do something like: "go to the line exactly half-way through the 20M file," since it's nigh on impossible to know the byte position of an arbitrary line number without reading through the entire file, a byte (or block, as it were) at a time. =( But as long as you know the byte position already, you're good to go.

    bbfu
    Seasons don't fear The Reaper.
    Nor do the wind, the sun, and the rain.
    We can be like they are.

      One more:
      Does 'byte position' apply only to the position of the beginning of the line, or the position of any single byte within the file?

      Amel - f.k.a. - kel

        Any byte, anywhere in the file.

        One thing to note is that seek resets the state of the filehandle, including errors, eof, as well as Perl's internal idea of whether or not it should append the filehandle to any warnings or error messages (see Re: Closing and re-opening the DATA Filehandle). That's why you might see the no-op seek (seek $fh, 0, 1) used occasionally for such things.

        bbfu
        Seasons don't fear The Reaper.
        Nor do the wind, the sun, and the rain.
        We can be like they are.