1POE::Filter::Map(3) User Contributed Perl Documentation POE::Filter::Map(3)
2
3
4
6 POE::Filter::Map - transform input and/or output within a filter stack
7
9 #!perl
10
11 use POE qw(
12 Wheel::FollowTail
13 Filter::Line Filter::Map 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 $redact_some_lines = POE::Filter::Map->new(
22 Code => sub {
23 my $input = shift;
24 $input = "[REDACTED]" unless $input =~ /sudo\[\d+\]/i;
25 return $input;
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 $redact_some_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::Map transforms data inside the filter stack. It may be
53 used to transform input, output, or both depending on how it is
54 constructed. This filter is named and modeled after Perl's built-in
55 map() function.
56
57 POE::Filter::Map is designed to be combined with other filters through
58 POE::Filter::Stackable. In the "SYNOPSIS" example, a filter stack is
59 created to parse logs as lines and redact all entries that don't
60 pertain to a sudo process.
61
63 In addition to the usual POE::Filter methods, POE::Filter::Map also
64 supports the following.
65
66 new
67 new() constructs a new POE::Filter::Map 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 transformed versions of their sole parameters. 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.
73
74 # Decrypt rot13.
75 sub decrypt_rot13 {
76 my $encrypted = shift;
77 $encrypted =~ tr[a-zA-Z][n-za-mN-ZA-M];
78 return $encrypted;
79 }
80
81 # Encrypt rot13.
82 sub encrypt_rot13 {
83 my $plaintext = shift;
84 $plaintext =~ tr[a-zA-Z][n-za-mN-ZA-M];
85 return $plaintext;
86 }
87
88 # Decrypt rot13 on input, and encrypt it on output.
89 my $rot13_transcrypter = POE::Filter::Map->new(
90 Get => \&decrypt_rot13,
91 Put => \&encrypt_rot13,
92 );
93
94 Rot13 is symmetric, so the above example can be simplified to use a
95 single Code function.
96
97 my $rot13_transcrypter = POE::Filter::Map->new(
98 Code => sub {
99 local $_ = shift;
100 tr[a-zA-Z][n-za-mN-ZA-M];
101 return $_;
102 }
103 );
104
105 modify
106 modify() changes a POE::Filter::Map object's behavior at run-time. It
107 accepts the same parameters as new(), and it replaces the existing
108 transforms with new ones.
109
110 # Switch to "reverse" encryption for testing.
111 $rot13_transcrypter->modify(
112 Code => sub { return scalar reverse shift }
113 );
114
116 POE::Filter for more information about filters in general.
117
118 POE::Filter::Stackable for more details on stacking filters.
119
121 None known.
122
124 The Map filter was contributed by Dieter Pearcey. Documentation is
125 provided by Rocco Caputo.
126
127 Please see the POE manpage for more information about authors and
128 contributors.
129
130
131
132perl v5.34.0 2021-07-22 POE::Filter::Map(3)