use IO::Async::FileStream; use IO::Async::Loop; my $loop = IO::Async::Loop->new; open my $logh, "<", "var/logs/daemon.log" or die "Cannot open logfile - $!"; my $filestream = IO::Async::FileStream->new( read_handle => $logh, on_initial => sub { my ( $self ) = @_; $self->seek_to_last( "\n" ); }, on_read => sub { my ( $self, $buffref ) = @_; while( $$buffref =~ s/^(.*\n)// ) { print "Received a line $1"; } return 0; }, ); $loop->add( $filestream ); $loop->run;
This class provides an API identical to IO::Async::Stream when given a "read_handle"; it should be treated similarly. In particular, it can be given an "on_read" handler, or subclassed to provide an "on_read" method, or even used as the "transport" for an IO::Async::Protocol::Stream object.
It will not support writing.
To watch a file, directory, or other filesystem entity for updates of other properties, such as "mtime", see also IO::Async::File.
Because this is a subclass of IO::Async::Stream in read-only mode, all the events supported by "Stream" relating to the read handle are supported here. This is not a full list; see also the documentation relating to IO::Async::Stream.
Note that $eof only indicates that all the data currently available in the file has now been read; in contrast to a regular IO::Async::Stream, this object will not stop watching after this condition. Instead, it will continue watching the file for updates.
This method may be useful to skip initial content in the file, if the object should only respond to new content added after it was created.
$filestream->seek( $offset, $whence )
Callable only during the "on_initial" event. Moves the read position in the filehandle to the given offset. $whence is interpreted as for "sysseek", should be either "SEEK_SET", "SEEK_CUR" or "SEEK_END". Will be set to "SEEK_SET" if not provided.
Normally this would be used to seek to the end of the file, for example
on_initial => sub { my ( $self, $filesize ) = @_; $self->seek( $filesize ); }
$success = $filestream->seek_to_last( $str_pattern, %opts )
Callable only during the "on_initial" event. Attempts to move the read position in the filehandle to just after the last occurrence of a given match. $str_pattern may be a literal string or regexp pattern.
Returns a true value if the seek was successful, or false if not. Takes the following named arguments:
To force it to always search through the entire file contents, set this explicitly to 0.
Because regular file reading happens synchronously, this entire method operates entirely synchronously. If the file is very large, it may take a while to read back through the entire contents. While this is happening no other events can be invoked in the process.
When looking for a string or regexp match, this method appends the previously-read buffer to each block read from the file, in case a match becomes split across two reads. If "blocksize" is reduced to a very small value, take care to ensure it isn't so small that a match may not be noticed.
This is most likely useful for seeking after the last complete line in a line-based log file, to commence reading from the end, while still managing to capture any partial content that isn't yet a complete line.
on_initial => sub { my $self = shift; $self->seek_to_last( "\n" ); }