Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Tailing a pipe or file

by ribasushi (Monk)
on Sep 05, 2007 at 21:20 UTC ( #637267=perlquestion: print w/ replies, xml ) Need Help??
ribasushi has asked for the wisdom of the Perl Monks concerning the following question:

Greetings Monks, I need to tail either a real file, or stdin. I do not want to use Tail::File because it is not a Core module (besides I want to know how this is done anyway).

Here is my read loop ($fh might be either \*STDIN or IO::Handle of an opened file):
while (1) { my $res = sysread ($fh, my $data, $cfg->{max_line_length}); if (not defined $res) { die "Input reading failed: $!\n"; } elsif ($res == 0) { sysseek ($fh, 0, SEEK_CUR); sleep (0.5); next; } <do stuff with $data here> }

Am I correct at assuming that:
  • If $fh is a pipe (stdin), then sysread will block and I will never try to seek on stdin, of which it is not capable
  • If $fh is a file, the sysread will never block and at EOF the result will always be 0
  • If $fh is a file, and the file gets renamed, the descriptor will not be affected and I will continue attempts to read from the same file
  • If $fh is a file and it gets unlinked my open descriptor will maintain a reference to it, and I will not get an error - the file will simply stop growing, because no one will write to a non-existing file

Let me know if any of the above is fundamentally wrong.

Comment on Tailing a pipe or file
Download Code
Re: Tailing a pipe or file
by eric256 (Parson) on Sep 05, 2007 at 21:39 UTC

    Do you mean File::Tail?

    I can understand not wanting to use a module that isn't core. We'll I can understand why you would think that at least,but in general i would recommend that you use the tools provided if they fit your need. Don't let the fact that something is not core deter you from using it. I won't guess your reasons but i know I've done the same in the past because i thought it was too hard to install modules. I was wrong. ;) If you want to learn to do it yourself go for it! I would check the codings, docs and bugs for File::Tail as a way to learn about it. You can view the code for File::Tail directly on cpan.


    ___________
    Eric Hodges
Re: Tailing a pipe or file
by Joost (Canon) on Sep 05, 2007 at 21:40 UTC
    I'll try to answer these for UNIX/POSIX systems, since I don't know if they're universal:

    If $fh is a pipe (stdin), then sysread will block and I will never try to seek on stdin, of which it is not capable

    sysread will bock on pipes and sockets, unless you've specified unblocking behaviour. update: and you can't seek on pipes and sockets AFAIK, but seeking may flush system buffers.

    If $fh is a file, the sysread will never block and at EOF the result will always be 0

    AFAIK all reads block (i.e. wait for the data to arrive from disk, or network etc) unless you've got nonblocking behaviour. Reading on a file just won't block when you've reached EOF, similarly for closed pipes and sockets. I'm not sure if nonblocking behaviour does anything special on a file, but it might.

    If $fh is a file, and the file gets renamed, the descriptor will not be affected and I will continue attempts to read from the same file

    True.

    If $fh is a file and it gets unlinked my open descriptor will maintain a reference to it, and I will not get an error - the file will simply stop growing, because no one will write to a non-existing file

    True in a way, but if any other process already has that file open, they can still write to it, but if the file is completely unlinked (a file may have more than one entry point in the directory tree) you can't normally open() it (system specific tricks aside).

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (6)
As of 2014-09-21 10:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (168 votes), past polls