1POE::Filter::Grep(3) User Contributed Perl Documentation POE::Filter::Grep(3)
2
3
4
6 POE::Filter::Grep - select or remove items based on simple rules
7
9 #!perl
10
11 use POE qw(
12 Wheel::FollowTail
13 Filter::Line Filter::Grep Filter::Stackable
14 );
15
16 POE::Session->create(
17 inline_states => {
18 _start => sub {
19 my $parse_input_as_lines = POE::Filter::Line->new();
20
21 my $select_sudo_log_lines = POE::Filter::Grep->new(
22 Put => sub { 1 },
23 Get => sub {
24 my $input = shift;
25 return $input =~ /sudo\[\d+\]/i;
26 },
27 );
28
29 my $filter_stack = POE::Filter::Stackable->new(
30 Filters => [
31 $parse_input_as_lines, # first on get, last on put
32 $select_sudo_log_lines, # first on put, last on get
33 ]
34 );
35
36 $_[HEAP]{tailor} = POE::Wheel::FollowTail->new(
37 Filename => "/var/log/system.log",
38 InputEvent => "got_log_line",
39 Filter => $filter_stack,
40 );
41 },
42 got_log_line => sub {
43 print "Log: $_[ARG0]\n";
44 }
45 }
46 );
47
48 POE::Kernel->run();
49 exit;
50
52 POE::Filter::Grep selects or removes items based on simple tests. It
53 may be used to filter input, output, or both. This filter is named and
54 modeled after Perl's built-in grep() function.
55
56 POE::Filter::Grep is designed to be combined with other filters through
57 POE::Filter::Stackable. In the "SYNOPSIS" example, a filter stack is
58 created to parse logs as lines and remove all entries that don't
59 pertain to a sudo process. (Or if your glass is half full, the stack
60 only selects entries that DO mention sudo.)
61
63 In addition to the usual POE::Filter methods, POE::Filter::Grep also
64 supports the following.
65
66 new
67 new() constructs a new POE::Filter::Grep object. It must either be
68 called with a single Code parameter, or both a Put and a Get parameter.
69 The values for Code, Put, and Get are code references that, when
70 invoked, return true to select an item or false to reject it. A Code
71 function will be used for both input and output, while Get and Put
72 functions allow input and output to be filtered in different ways. The
73 item in question will be passed as the function's sole parameter.
74
75 sub reject_bidoofs {
76 my $pokemon = shift;
77 return 1 if $pokemon ne "bidoof";
78 return;
79 }
80
81 my $gotta_catch_nearly_all = POE::Filter::Grep->new(
82 Code => \&reject_bidoofs,
83 );
84
85 Enforce read-only behavior:
86
87 my $read_only = POE::Filter::Grep->new(
88 Get => sub { 1 },
89 Put => sub { 0 },
90 );
91
92 modify
93 modify() changes a POE::Filter::Grep object's behavior at run-time. It
94 accepts the same parameters as new(), and it replaces the existing
95 tests with new ones.
96
97 # Don't give away our Dialgas.
98 $gotta_catch_nearly_all->modify(
99 Get => sub { 1 },
100 Put => sub { return shift() ne "dialga" },
101 );
102
104 POE::Filter for more information about filters in general.
105
106 POE::Filter::Stackable for more details on stacking filters.
107
109 None known.
110
112 The Grep filter was contributed by Dieter Pearcey. Documentation is
113 provided by Rocco Caputo.
114
115 Please see the POE manpage for more information about authors and
116 contributors.
117
118
119
120perl v5.36.0 2022-07-22 POE::Filter::Grep(3)