Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

'seek' on a FileHandle.pm object

by dsb (Chaplain)
on Apr 17, 2001 at 22:36 UTC ( [id://73285]=perlquestion: print w/replies, xml ) Need Help??

dsb has asked for the wisdom of the Perl Monks concerning the following question:

Can you call the seek command on a FileHandle.pm object? I've tried to but it doesn't work. THe documentation is pretty vague and gives only an example of seek with a regular FILEHANDLE.

My attempt at seek:

seek( $fh,0,1 );

I need to go through a file once, then go back to the beginning and go through again.

Perhaps there's a different function I should use?

Amel - f.k.a. - kel

Replies are listed 'Best First'.
Re: 'seek' on a FileHandle.pm object
by merlyn (Sage) on Apr 17, 2001 at 22:47 UTC
    seek( $fh,0,1 );
    That's the "no-op" seek. You're saying to seek 0 relative bytes from the current position. That's pretty much where you already are.

    Perhaps you want to rewind the file, which would be either rewind $fh or seek $fh, 0, 0.

    -- Randal L. Schwartz, Perl hacker

      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

        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.

      That did it. I guess I misinterpreted how to use 'seek'...time to hit the books again...THANKS!!!

      Amel - f.k.a. - kel

Re: 'seek' on a FileHandle.pm object
by twerq (Deacon) on Apr 17, 2001 at 22:45 UTC
    I would look into using getpos() and setpos() for doing what you want.

    From the docs:
    If the C functions fgetpos() and fsetpos() are available, then `FileHandle::getpos' returns an opaque value that represents the current position of the FileHandle, and `FileHandle::setpos' uses that value to return to a previously visited position.

    /twerq

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://73285]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (1)
As of 2025-07-10 02:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.