Skip to content

Instantly share code, notes, and snippets.

@ggl
Last active September 10, 2016 07:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ggl/e83784e203300de7ada14bd904d743d3 to your computer and use it in GitHub Desktop.
Save ggl/e83784e203300de7ada14bd904d743d3 to your computer and use it in GitHub Desktop.
Start a child process and feed it Sereal encoded data
#!/usr/bin/env perl
use strict;
use warnings;
use Child;
use Data::Dumper;
use Sereal::Encoder;
use Sereal::Decoder;
my $se = Sereal::Encoder->new({ undef_unknown => 1 });
my $sd = Sereal::Decoder->new();
my $data = {
a => 1,
b => 'foo',
c => [1, 'bar'],
d => { a => 1, b => 'foo' },
e => undef,
f => sub { 1 },
};
my $child = Child->new(sub {
my $self = shift;
while (my $reply = $self->read()) {
if ($reply and $reply =~ /hello/) {
$self->say('hello');
}
else {
$self->say($se->encode($data));
}
}
}, pipe => 1);
my $proc = $child->start;
$proc->say('hello');
my $message1 = $proc->read();
print $message1;
if ($message1 and $message1 =~ /hello/) {
my $i = 21;
while ($i--) {
$proc->say('gimme');
my $message2 = $proc->read();
print Dumper {$i => $sd->decode($message2)};
}
}
$proc->kill(9);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment