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

Hi, I want to read a file twice. No I cant get it into memory (array) so I need to read it once, save some data and then read it again and search some other data depending on the first saves. But as the file gets read first some internal perl flag is set that the file end has been reached so any second open to the file gets nothing. How can I get rid of this flag (i just cannot remember anymore, was it some IO::File option?)? Thank you very much.

Replies are listed 'Best First'.
Re: How to read a file twice
by linuxer (Curate) on Apr 20, 2009 at 22:53 UTC

    Have a look at seek to reset the filehandle's position.

Re: How to read a file twice
by almut (Canon) on Apr 20, 2009 at 23:26 UTC
    some internal perl flag is set that the file end has been reached so any second open to the file gets nothing.

    That's unlikely.  Re-opening a (regular, seekable) file should always allow for its contents to be read another time.  When opening a file for reading, the file pointer (indicating the position where to read from) is automatically set to the beginning.

    Could you provide some code that demonstrates the issue?

Re: How to read a file twice
by Sinistral (Monsignor) on Apr 21, 2009 at 01:16 UTC
    You can open a file for read and write at the same time (you probabably want '+<' as the flag to the open call). In addition while the file is open you can seek to any position in the file. See perlfunc (HTMLized nicely at perdoc.perl.org alphabetical listing of functions)
Re: How to read a file twice
by Anonymous Monk on Apr 21, 2009 at 10:48 UTC
    Arghh... as (almost) always it was my fault. Of course just reopening the file resets the flags and also seek $fhi, 0, SEEK_SET; does work as intended. Thank you for your help.