Last active
September 10, 2016 07:36
-
-
Save ggl/e83784e203300de7ada14bd904d743d3 to your computer and use it in GitHub Desktop.
Start a child process and feed it Sereal encoded data
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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