Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling

"Resetting" a file handle

by DeusVult (Scribe)
on Feb 01, 2001 at 23:33 UTC ( #55810=perlquestion: print w/replies, xml ) Need Help??

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

Is there some way to "reset" a file handle? If, for example, I open a file with filehandle FH, and I want to do this:
my $info; while (<FH>) { $info = &searchEachLineForInterestingInformation($_); # ok, no one would ever name a subroutine that, but I'm # trying to be descriptive here }
Then, once I know what is in $info, I want to go back and read the file again, looking for something new depending upon what sort of information I received from this first pass.

So is there any way to set FH back to the beginning?

On another level, is there some way to do even more complicated seeking (read in line 14, jump back to line 11, etc.)? I can get by without it for this problem, but I always like to learn new tricks.

Thanks in advance.

Some people drink from the fountain of knowledge, others just gargle.

Replies are listed 'Best First'.
(jeffa) Re: "Resetting" a file handle
by jeffa (Bishop) on Feb 01, 2001 at 23:36 UTC
    Also, look into tell


    (the triplet paradiddle)
Re: "Resetting" a file handle
by lhoward (Vicar) on Feb 01, 2001 at 23:36 UTC
    Assuming that FH is a filehandle to a "real" file and not something like a pipe you can just do:
    seek FH,0,0;
    See the seek documents in perlfunc for more info...
Re: "Resetting" a file handle
by kilinrax (Deacon) on Feb 01, 2001 at 23:36 UTC
    You probably want to take a look at seek.
    seek FILEHANDLE, 0, 0;
    will reset a filehandle to the beginning of the file.
Re: "Resetting" a file handle
by Trinary (Pilgrim) on Feb 01, 2001 at 23:38 UTC

    <code>seek FH,0,0; will put it back to the beginning. Check docs for more arguments, etc. You might also want to check out info on $., which is the current line number of the last filehandle that was read from, written to, or seeked.



Re: "Resetting" a file handle
by DeusVult (Scribe) on Feb 02, 2001 at 00:24 UTC
    Thank you everyone for your prompt replies, though I'm a little embarrassed to have asked what appears to be a common knowledge question. I can only assure you that I did indeed try to find the answer on my own.

    Some people drink from the fountain of knowledge, others just gargle.

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (5)
As of 2022-06-25 10:28 GMT
Find Nodes?
    Voting Booth?
    My most frequent journeys are powered by:

    Results (81 votes). Check out past polls.