Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

how do i reset a filehandle?

by Anonymous Monk
on Jun 01, 2000 at 00:30 UTC ( [id://15701]=perlquestion: print w/replies, xml ) Need Help??

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

Hello, Will someone please tell me how to reset a filehandle to point to the beginning of the file it is reading? I know I can achieve this by opening the file, reading, closing the file, and reopening it, but I want to avoid this if possible. Thanks for your help! -relative newbie in dallas

Replies are listed 'Best First'.
Re: how do i reset a filehandle?
by lhoward (Vicar) on Jun 01, 2000 at 01:00 UTC
    Use the seek command.
    seek HANDLE,0,0;
    you mileage may vary if trying to seek on streams or pipes (like STDIN).
      Just to clarify lhoward's answer:
      The second parameter for seek is a position, the third parameter is what the position is relative to:

      0 Relative to the beginning of the file.
      1 Relative to the current position.
      2 Relative to the end of the file.

      So,

        seek HANDLE,0,0
      
      means to move to index 0 relative to the beginning of the file.
      Just to clarify lhoward's answer:
      The second parameter for seek is a position, the third parameter is what the position is relative to:

      0 Relative to the beginning of the file.
      1 Relative to the current position.
      2 Relative to the end of the file.

      So,

        seek HANDLE,0,0
      
      means to move to index 0 relative to the beginning of the file.
RE: how do i reset a filehandle?
by BigJoe (Curate) on Jun 01, 2000 at 00:50 UTC
    I say that this can be handled many ways. My favorite is to read the whole file into a string/array/hash to then do search and replace and testing of the text.
    open(INFILE, "infile.txt"); my $filesize = -s INFILE; read(INFILE, $temp, $filesize); close(INFILE); $temp = /(regex here)/; and your other stuff here
      This gets really bad when you have to deal with large files. (how much memory do you have?) I would suggest getting out of this habit as soon as possible. As others have mentioned, use seek.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (7)
As of 2024-04-19 12:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found