POE::Filter::Map(3pm)

πŸ’… POE::Filter::Map β€” the manual page, but make it cute ✨

okay bestie, so POE::Filter::Map is a Perl thing from the User Contributed Perl Documentation squad, and it's basically the glow-up your data gets on the way through a filter stack. transforming input and/or output? yeah. that's the whole vibe. πŸ¦‹


✨ SYNOPSIS ✨

like, here's the whole thing in action, no notes, iconic behaviour:

#!perl

use POE qw(
  Wheel::FollowTail
  Filter::Line Filter::Map Filter::Stackable
);

POE::Session->create(
  inline_states => {
    _start => sub {
      my $parse_input_as_lines = POE::Filter::Line->new();

      my $redact_some_lines = POE::Filter::Map->new(
        Code => sub {
          my $input = shift;
          $input = "[REDACTED]" unless $input =~ /sudo\[\d+\]/i;
          return $input;
        },
      );

      my $filter_stack = POE::Filter::Stackable->new(
        Filters => [
          $parse_input_as_lines, # first on get, last on put
          $redact_some_lines, # first on put, last on get
        ]
      );

      $_[HEAP]{tailor} = POE::Wheel::FollowTail->new(
        Filename => "/var/log/system.log",
        InputEvent => "got_log_line",
        Filter => $filter_stack,
      );
    },
    got_log_line => sub {
      print "Log: $_[ARG0]\n";
    }
  }
);

POE::Kernel->run();
exit;

🌸 DESCRIPTION 🌸

so POE::Filter::Map transforms data inside the filter stack. it can do input, output, or both β€” depends on how you construct it, bestie. it's literally named and modeled after Perl's built-in map() function, which, slay. πŸ’–

and the way it's designed to be combined with other filters through POE::Filter::Stackable? obsessed. in that "SYNOPSIS" example up there, a filter stack gets created to parse logs as lines and redact every entry that doesn't pertain to a sudo process. so all the not-sudo stuff gets the [REDACTED] treatment. privacy queen behaviour. πŸ‘‘


πŸŽ€ PUBLIC FILTER METHODS πŸŽ€

on top of the usual POE::Filter methods, POE::Filter::Map also supports these:

πŸ’« new

new() constructs a new POE::Filter::Map object. it must either be called with a single Code parameter, or both a Put and a Get parameter. no half-measures, pick a lane.

the values for Code, Put and Get are code references that, when invoked, return transformed versions of their sole parameters. a Code function gets used for both input and output, while Get and Put functions let input and output be filtered in different ways. very versatile of them honestly.

# Decrypt rot13.
sub decrypt_rot13 {
  my $encrypted = shift;
  $encrypted =~ tr[a-zA-Z][n-za-mN-ZA-M];
  return $encrypted;
}

# Encrypt rot13.
sub encrypt_rot13 {
  my $plaintext = shift;
  $plaintext =~ tr[a-zA-Z][n-za-mN-ZA-M];
  return $plaintext;
}

# Decrypt rot13 on input, and encrypt it on output.
my $rot13_transcrypter = POE::Filter::Map->new(
  Get => \&decrypt_rot13,
  Put => \&encrypt_rot13,
);

ngl rot13 is symmetric, so that whole example can be simplified down to a single Code function. less code, same energy:

my $rot13_transcrypter = POE::Filter::Map->new(
  Code => sub {
    local $_ = shift;
    tr[a-zA-Z][n-za-mN-ZA-M];
    return $_;
  }
);

πŸ’« modify

modify() changes a POE::Filter::Map object's behavior at run-time. it accepts the same parameters as new(), and it replaces the existing transforms with new ones. glow-up mid-operation, we love to see it. πŸ¦‹

# Switch to "reverse" encryption for testing.
$rot13_transcrypter->modify(
  Code => sub { return scalar reverse shift }
);

πŸ‘€ SEE ALSO πŸ‘€

  • POE::Filter for more information about filters in general.
  • POE::Filter::Stackable for more details on stacking filters.

πŸ’€ BUGS πŸ’€

None known.

like, no known bugs?? we stan a flawless filter. 😭✨


🫢 AUTHORS & COPYRIGHTS 🫢

The Map filter was contributed by Dieter Pearcey. Documentation is provided by Rocco Caputo. absolute icons, ty for your service. πŸ’œ

Please see the POE manpage for more information about authors and contributors.


okay that's the whole thing, bestie!! you're now fully fluent in POE::Filter::Map. go transform some data, you main character. πŸ’…πŸŒˆ

this page came from perl v5.34.0, dated 2022-03-25, and it's page POE::Filter::Map(3pm) of the User Contributed Perl Documentation β€” mwah, bye!! πŸŽ€

πŸ“¦ where this page comes from