POE::F...erence(3pm)
π POE::Filter::Reference π
okay bestie, so this is POE::Filter::Reference from the User Contributed Perl Documentation, and it's literally the filter that lets you yeet whole Perl data structures across a pipe or a socket without ever thinking about line protocols. no cap. it's giving "serialize my vibes and ship them". π¦
β¨ NAME β¨
POE::Filter::Reference β freeze and thaw arbitrary Perl data π
πΈ SYNOPSIS πΈ
here's the whole vibe, copy it exactly bestie:
#!perl
use YAML;
use POE qw(Wheel::ReadWrite Filter::Reference);
POE::Session->create(
inline_states => {
_start => sub {
pipe(my($read, $write)) or die $!;
$_[HEAP]{io} = POE::Wheel::ReadWrite->new(
InputHandle => $read,
OutputHandle => $write,
Filter => POE::Filter::Reference->new(),
InputEvent => "got_perl_data",
);
$_[HEAP]{io}->put(
{ key_1 => 111, key_2 => 222 }
);
},
got_perl_data => sub {
print "Got data:\n", YAML::Dump($_[ARG0]);
print "Bye!\n";
delete $_[HEAP]{io};
}
}
);
POE::Kernel->run();
exit;
periodt. that's the whole flow. π
π¦ DESCRIPTION π¦
so POE::Filter::Reference lets your programs send and receive arbitrary Perl data structures without you having to stress about a line protocol. not even a little. its put() method serializes Perl data into a byte stream that's suitable for transmission, and get_one() parses those data structures right back out of such a stream. iconic behavior honestly. β¨
by default, POE::Filter::Reference uses Storable to do its magic. but you can specify a different serializer at construction time if you're feeling spicy. π
π PUBLIC FILTER METHODS π
π new π
new() creates and initializes a POE::Filter::Reference object. it accepts a list of named parameters. so many options, we love a customizable queen. π
π Serializer π
any class that supports nfreeze() (or freeze()) and thaw() may be used as a Serializer. and listen: if a Serializer implements both nfreeze() and freeze(), then the "network" (nfreeze) version will be used. network girlies win. π₯
Serializer may be a class name:
# Use Storable explicitly, specified by package name.
my $filter = POE::Filter::Reference->newer( Serializer=>"Storable" );
# Use YAML instead. Compress its output, as it may be verbose.
my $filter = POE::Filter::Reference->new("YAML", 1);
Serializer may also be an object:
# Use an object.
my $serializer = Data::Serializer::Something->new();
my $filter = POE::Filter::Reference->newer( Serializer => $serializer );
if Serializer is omitted or undef, the Reference filter will try to use Storable, then FreezeThaw, then YAML β in that exact order, no skipping. π«Ά and POE::Filter::Reference will die if it cannot find one of these serializers, but ngl that rarely happens now that Storable and YAML are bundled with Perl. lucky us. π
π Compression π
if Compression is true, Compress::Zlib will be called upon to reduce the size of serialized data. it will also decompress the incoming stream data. two jobs, one flag, she's versatile. π
π MaxBuffer π
"MaxBuffer" sets the maximum amount of data that the filter will hold onto while trying to build a new reference. it defaults to 512 MB. big buffer energy. π§
π NoFatals π
if NoFatals is true, messages will be thawed inside a block eval. by default, however, thaw() is allowed to die normally β dramatic, but valid. π
if an error occurs while NoFatals is in effect, POE::Filter::Reference will return a string containing the contents of $@ at the time the eval failed. so when you're using NoFatals, it's important to check whether input is really a reference:
sub got_reference {
my $message = $_[ARG0];
if (ref $message) {
print "Got data:\n", YAML::Dump($message);
}
else {
warn "Input decode error: $message\n";
}
}
π¨ bestie this is the part people forget: withNoFatalson, an error comes back as a string, not a ref. checkreffirst or you'll be decoding your own error message like a clown. π
new() will try to load any classes it needs for "Compression" or "Serializer". self-sufficient queen. β¨
π new [SERIALIZER [, COMPRESSION [, NO_FATALS]]] π
this is the old constructor synatx. it does not conform to the normal POE::Filter constructor parameter syntax. please use the new syntax instead, fr fr. π
calling "new" like this is equivalent to
POE::Filter::Reference->new( Serializer => SERIALIZER,
Compression => COMPRESSION,
NoFatals => NO_FATALS );
β οΈ please note: if you have a custom serializer class called "Serializer" you will have to update your code to the new syntax. no cap, go do it. π
π SERIALIZER API π
here's what POE::Filter::Reference expects of its serializers. it has standards, and honestly? same.
π thaw SERIALIZED π
thaw() is required. it accepts two parameters: $self and a scalar containing a SERIALIZED byte stream representing a single Perl data structure. it returns a reconstituted Perl data structure. glow up complete. π¦
sub thaw {
my ($self, $stream) = @_;
my $reference = $self->_deserialization_magic($stream);
return $reference;
}
π nfreeze REFERENCE π
either nfreeze() or freeze() is required. they behave identically, except that nfreeze() is guaranteed to be portable across networks and between machine architectures. she travels well. βοΈ
these freezers accept two parameters: $self and a REFERENCE to Perl data. they return a serialized version of the REFERENCEd data.
sub nfreeze {
my ($self, $reference) = @_;
my $stream = $self->_serialization_magic($reference);
return $stream;
}
π freeze REFERENCE π
freeze() is an alternative form of nfreeze(). it has the same call signature as nfreeze(), but it doesn't guarantee that serialized data will be portable across machine architectures. so like... it works, but it's not a world traveler. π
if you must choose between implementing freeze() and nfreeze() for use with POE::Filter::Reference, go with nfreeze(). that's the assignment. π
π«Ά SEE ALSO π«Ά
please see POE::Filter for documentation regarding the base interface. that's where the lore lives. π
the SEE ALSO section in POE contains a table of contents covering the entire POE distribution. go explore bestie. π
π BUGS π
not so much bugs as caveats:
- it's important to use identical serializers on each end of a connection. even different versions of the same serializer can break data in transit. π like, same class, same version, or chaos.
- most (if not all) serializers will re-bless data at the destination, but many of them will not load the necessary classes to make those blessings work. so make sure the same classes and versions are available on either end of the wire. otherwise you've got blessed refs with no home and that's tragic. ποΈ
π AUTHORS & COPYRIGHTS π
the Reference filter was contributed by Artur Bergman, with changes by Philip Gwyn. legends, both of them. π
please see POE for more information about authors and contributors.
and that's the whole page, bestie!! this one came from perl v5.34.0, dated 2022-03-25 β yeah she's from the archives but she still slays. now go serialize something and remember: same serializer on both ends, nfreeze() over freeze(), and check ref when NoFatals is on. mwah. π
β¨ππ¦
π¦ where this page comes from
- debian bookworm: libpoe-perl 2:1.3700-1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- debian trixie: libpoe-perl 2:1.3700-1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu noble: libpoe-perl 2:1.3700-1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu questing: libpoe-perl 2:1.3700-1
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms