Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

Reading from an appended file

by Lhamo_rin (Friar)
on Oct 17, 2005 at 13:31 UTC ( #500718=perlquestion: print w/replies, xml ) Need Help??
Lhamo_rin has asked for the wisdom of the Perl Monks concerning the following question:

Fellow monks,

I have a file that is being appended every 10 minutes. I am trying to read from it at the same rate but only want to read the additional information that is being appended, I don't want to read the whole file each time I try to access it. How can I do this?

Replies are listed 'Best First'.
Re: Reading from an appended file
by Zaxo (Archbishop) on Oct 17, 2005 at 13:38 UTC

    Once you reach the end of the file, you can call tell to get the offset to eof. Then when you reopen the file, seek to that point and read on. Assumes your program runs continuously. Store the value somewhere if not.

    After Compline,

Re: Reading from an appended file
by marto (Archbishop) on Oct 17, 2005 at 13:39 UTC
    Hi Lhamo_rin,

    The module File::Tail may be of use to you.
    You can set an interval of 600 seconds so that the file would be read every 10 minutes for new lines.

    Hope this helps.

Re: Reading from an appended file
by blazar (Canon) on Oct 17, 2005 at 13:39 UTC
    This is another faq in just a few minutes. Well, sort of: in any case you may be interested in checking what perldoc -q tail has to tell you. To quote from it:
    First try seek(GWFILE, 0, 1); The statement "seek(GWFILE, 0, 1)" doesn't change the current position +, but it does clear the end-of-file condition on the handle, so that the next <GWFILE> makes Perl try again to read something.
Re: Reading from an appended file
by gopalr (Priest) on Oct 17, 2005 at 13:44 UTC


    Please take a look at File::Slurp Module.

    use File::Slurp; use strict; my $text = read_file( 'de.txt' ) ;


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://500718]
Approved by marto
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (10)
As of 2018-06-25 16:03 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (127 votes). Check out past polls.