POE::Filter::Line3pm
Serialize and parse terminated records (lines)
Usage
#!perluse POE qw(Wheel::FollowTail Filter::Line);POE::Session->create(inline_states => {_start => sub {$_[HEAP]{tailor} = POE::Wheel::FollowTail->new(Filename => "/var/log/system.log",InputEvent => "got_log_line",Filter => POE::Filter::Line->new(),);},got_log_line => sub {print "Log: $_[ARG0]\n";}});POE::Kernel->run();exit;
Overview
POE::Filter::Line parses stream data into terminated records. The default parser interprets newlines as the record terminator, and the default serializer appends network newlines (CR/LF, or "\x0D\x0A") to outbound records.
Record terminators are removed from the data POE::Filter::Line returns.
POE::Filter::Line supports a number of other ways to parse lines. Constructor parameters may specify literal newlines, regular expressions, or that the filter should detect newlines on its own.
Public filter methods
POE::Filter::Line's new() method has some interesting parameters.
new
new() accepts a list of named parameters.
In all cases, the data interpreted as the record terminator is stripped from the data POE::Filter::Line returns.
"InputLiteral" may be used to parse records that are terminated by some literal string. For example, POE::Filter::Line may be used to parse and emit C-style lines, which are terminated with an ASCII NUL:
my $c_line_filter = POE::Filter::Line->new(InputLiteral => chr(0),OutputLiteral => chr(0),);
"OutputLiteral" allows a filter to put() records with a different record terminator than it parses. This can be useful in applications that must translate record terminators.
"Literal" is a shorthand for the common case where the input and output literals are identical. The previous example may be written as:
my $c_line_filter = POE::Filter::Line->new(Literal => chr(0),);
An application can also allow POE::Filter::Line to figure out which newline to use. This is done by specifying "InputLiteral" to be undef:
my $whichever_line_filter = POE::Filter::Line->new(InputLiteral => undef,OutputLiteral => "\n",);
"InputRegexp" may be used in place of "InputLiteral" to recognize line terminators based on a regular expression. In this example, input is terminated by two or more consecutive newlines. On output, the paragraph separator is "---" on a line by itself.
my $paragraph_filter = POE::Filter::Line->new(InputRegexp => "([\x0D\x0A]{2,})",OutputLiteral => "\n---\n",);
"MaxBuffer" sets the maximum amount of data that the filter will hold onto while trying to find a line ending. Defaults to 512 MB.
"MaxLength" sets the maximum length of a line. Defaults to 64 MB.
If either the "MaxLength" or "MaxBuffer" constraint is exceeded, "POE::Filter::Line" will throw an exception.
Public filter methods
POE::Filter::Line has no additional public methods.
Subclassing
POE::Filter::Line exports the FIRST_UNUSED constant. This points to the first unused element in the $self array reference. Subclasses should store their own data beginning here, and they should export their own FIRST_UNUSED constants to help future subclassers.
Authors & copyrights
Please see POE for more information about authors and contributors.
Releases
14 releases since Debian 2.2 (Potato); the text changed 12 times.