.\" Automatically generated by Pod::Man 2.28 (Pod::Simple 3.28) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" Set up some character translations and predefined strings. \*(-- will .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left .\" double quote, and \*(R" will give a right double quote. \*(C+ will .\" give a nicer C++. Capital omega is used to do unbreakable dashes and .\" therefore won't be available. \*(C` and \*(C' expand to `' in nroff, .\" nothing in troff, for use with C<>. .tr \(*W- .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p' .ie n \{\ . ds -- \(*W- . ds PI pi . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch . ds L" "" . ds R" "" . ds C` "" . ds C' "" 'br\} .el\{\ . ds -- \|\(em\| . ds PI \(*p . ds L" `` . ds R" '' . ds C` . ds C' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is turned on, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .\" .\" Avoid warning from groff about undefined register 'F'. .de IX .. .nr rF 0 .if \n(.g .if rF .nr rF 1 .if (\n(rF:(\n(.g==0)) \{ . if \nF \{ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{ . nr % 0 . nr F 2 . \} . \} .\} .rr rF .\" .\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2). .\" Fear. Run. Save yourself. No user-serviceable parts. . \" fudge factors for nroff and troff .if n \{\ . ds #H 0 . ds #V .8m . ds #F .3m . ds #[ \f1 . ds #] \fP .\} .if t \{\ . ds #H ((1u-(\\\\n(.fu%2u))*.13m) . ds #V .6m . ds #F 0 . ds #[ \& . ds #] \& .\} . \" simple accents for nroff and troff .if n \{\ . ds ' \& . ds ` \& . ds ^ \& . ds , \& . ds ~ ~ . ds / .\} .if t \{\ . ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u" . ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u' . ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u' . ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u' . ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u' . ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u' .\} . \" troff and (daisy-wheel) nroff accents .ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V' .ds 8 \h'\*(#H'\(*b\h'-\*(#H' .ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#] .ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H' .ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u' .ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#] .ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#] .ds ae a\h'-(\w'a'u*4/10)'e .ds Ae A\h'-(\w'A'u*4/10)'E . \" corrections for vroff .if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u' .if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u' . \" for low resolution devices (crt and lpr) .if \n(.H>23 .if \n(.V>19 \ \{\ . ds : e . ds 8 ss . ds o a . ds d- d\h'-1'\(ga . ds D- D\h'-1'\(hy . ds th \o'bp' . ds Th \o'LP' . ds ae ae . ds Ae AE .\} .rm #[ #] #H #V #F C .\" ======================================================================== .\" .IX Title "IO::Async::FileStream 3pm" .TH IO::Async::FileStream 3pm "2014-10-21" "perl v5.20.1" "User Contributed Perl Documentation" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH "NAME" "IO::Async::FileStream" \- read the tail of a file .SH "SYNOPSIS" .IX Header "SYNOPSIS" .Vb 1 \& 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( "\en" ); \& }, \& \& on_read => sub { \& my ( $self, $buffref ) = @_; \& \& while( $$buffref =~ s/^(.*\en)// ) { \& print "Received a line $1"; \& } \& \& return 0; \& }, \& ); \& \& $loop\->add( $filestream ); \& \& $loop\->run; .Ve .SH "DESCRIPTION" .IX Header "DESCRIPTION" This subclass of IO::Async::Stream allows reading the end of a regular file which is being appended to by some other process. It invokes the \f(CW\*(C`on_read\*(C'\fR event when more data has been added to the file. .PP This class provides an \s-1API\s0 identical to \f(CW\*(C`IO::Async::Stream\*(C'\fR when given a \&\f(CW\*(C`read_handle\*(C'\fR; it should be treated similarly. In particular, it can be given an \f(CW\*(C`on_read\*(C'\fR handler, or subclassed to provide an \f(CW\*(C`on_read\*(C'\fR method, or even used as the \f(CW\*(C`transport\*(C'\fR for an \f(CW\*(C`IO::Async::Protocol::Stream\*(C'\fR object. .PP It will not support writing. .PP To watch a file, directory, or other filesystem entity for updates of other properties, such as \f(CW\*(C`mtime\*(C'\fR, see also IO::Async::File. .SH "EVENTS" .IX Header "EVENTS" The following events are invoked, either using subclass methods or \s-1CODE\s0 references in parameters. .PP Because this is a subclass of IO::Async::Stream in read-only mode, all the events supported by \f(CW\*(C`Stream\*(C'\fR relating to the read handle are supported here. This is not a full list; see also the documentation relating to \&\f(CW\*(C`IO::Async::Stream\*(C'\fR. .ie n .SS "$ret = on_read \e$buffer, $eof" .el .SS "\f(CW$ret\fP = on_read \e$buffer, \f(CW$eof\fP" .IX Subsection "$ret = on_read $buffer, $eof" Invoked when more data is available in the internal receiving buffer. .PP Note that \f(CW$eof\fR only indicates that all the data currently available in the file has now been read; in contrast to a regular \f(CW\*(C`IO::Async::Stream\*(C'\fR, this object will not stop watching after this condition. Instead, it will continue watching the file for updates. .SS "on_truncated" .IX Subsection "on_truncated" Invoked when the file size shrinks. If this happens, it is presumed that the file content has been replaced. Reading will then commence from the start of the file. .ie n .SS "on_initial $size" .el .SS "on_initial \f(CW$size\fP" .IX Subsection "on_initial $size" Invoked the first time the file is looked at. It is passed the initial size of the file. The code implementing this method can use the \f(CW\*(C`seek\*(C'\fR or \&\f(CW\*(C`seek_to_last\*(C'\fR methods to set the initial read position in the file to skip over some initial content. .PP 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. .SH "PARAMETERS" .IX Header "PARAMETERS" The following named parameters may be passed to \f(CW\*(C`new\*(C'\fR or \f(CW\*(C`configure\*(C'\fR, in addition to the parameters relating to reading supported by \&\f(CW\*(C`IO::Async::Stream\*(C'\fR. .SS "filename => \s-1STRING\s0" .IX Subsection "filename => STRING" Optional. If supplied, watches the named file rather than the filehandle given in \f(CW\*(C`read_handle\*(C'\fR. The file will be opened by the constructor, and then watched for renames. If the file is renamed, the new filename is opened and tracked similarly after closing the previous file. .SS "interval => \s-1NUM\s0" .IX Subsection "interval => NUM" Optional. The interval in seconds to poll the filehandle using \f(CWstat(2)\fR looking for size changes. A default of 2 seconds will be applied if not defined. .SH "METHODS" .IX Header "METHODS" .ie n .SS "$filestream\->seek( $offset, $whence )" .el .SS "\f(CW$filestream\fP\->seek( \f(CW$offset\fP, \f(CW$whence\fP )" .IX Subsection "$filestream->seek( $offset, $whence )" Callable only during the \f(CW\*(C`on_initial\*(C'\fR event. Moves the read position in the filehandle to the given offset. \f(CW$whence\fR is interpreted as for \f(CW\*(C`sysseek\*(C'\fR, should be either \f(CW\*(C`SEEK_SET\*(C'\fR, \f(CW\*(C`SEEK_CUR\*(C'\fR or \f(CW\*(C`SEEK_END\*(C'\fR. Will be set to \&\f(CW\*(C`SEEK_SET\*(C'\fR if not provided. .PP Normally this would be used to seek to the end of the file, for example .PP .Vb 4 \& on_initial => sub { \& my ( $self, $filesize ) = @_; \& $self\->seek( $filesize ); \& } .Ve .ie n .SS "$success = $filestream\->seek_to_last( $str_pattern, %opts )" .el .SS "\f(CW$success\fP = \f(CW$filestream\fP\->seek_to_last( \f(CW$str_pattern\fP, \f(CW%opts\fP )" .IX Subsection "$success = $filestream->seek_to_last( $str_pattern, %opts )" Callable only during the \f(CW\*(C`on_initial\*(C'\fR event. Attempts to move the read position in the filehandle to just after the last occurance of a given match. \&\f(CW$str_pattern\fR may be a literal string or regexp pattern. .PP Returns a true value if the seek was successful, or false if not. Takes the following named arguments: .IP "blocksize => \s-1INT\s0" 8 .IX Item "blocksize => INT" Optional. Read the file in blocks of this size. Will take a default of 8KiB if not defined. .IP "horizon => \s-1INT\s0" 8 .IX Item "horizon => INT" Optional. Give up looking for a match after this number of bytes. Will take a default value of 4 times the blocksize if not defined. .Sp To force it to always search through the entire file contents, set this explicitly to \f(CW0\fR. .PP 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. .PP 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 \f(CW\*(C`blocksize\*(C'\fR is reduced to a very small value, take care to ensure it isn't so small that a match may not be noticed. .PP 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. .PP .Vb 4 \& on_initial => sub { \& my $self = shift; \& $self\->seek_to_last( "\en" ); \& } .Ve .SH "TODO" .IX Header "TODO" .IP "\(bu" 4 Move the actual file update watching code into \f(CW\*(C`IO::Async::Loop\*(C'\fR, possibly as a new watch/unwatch method pair \f(CW\*(C`watch_file\*(C'\fR. .IP "\(bu" 4 Consider if a construction-time parameter of \f(CW\*(C`seek_to_end\*(C'\fR or \f(CW\*(C`seek_to_last\*(C'\fR might be neater than a small code block in \f(CW\*(C`on_initial\*(C'\fR, if that turns out to be the only or most common form of use. .SH "AUTHOR" .IX Header "AUTHOR" Paul Evans