Skip to content

Instantly share code, notes, and snippets.

@gryftir
Forked from spacebat/parcel-passer.pl
Created August 8, 2013 07:11
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 gryftir/6182256 to your computer and use it in GitHub Desktop.
Save gryftir/6182256 to your computer and use it in GitHub Desktop.
#!/usr/bin/env perl
# ABSTRACT: proof of concept for sending subs to a child process for evaluation
use strict;
use warnings;
use v5.10;
use Data::Dumper;
use Socket;
use Time::HiRes qw(sleep);
$|=1;
my %funcs = map {
s/\A\s+|\s+\z//g;
my ($name) = /^\s*sub\s+([[:alnum:]]+)/
or die "Failed to find name in $_";
$name => $_;
}
grep /\S/,
split /^\s*$/m,
do { local $/; <DATA> };
say Dumper(\%funcs);
pipe READER, WRITER
or die "pipe: $!";
WRITER->autoflush;
if (my $pid = fork) {
my $count = 0;
for my $fn (sort keys %funcs) {
say "$$ parent send $fn";
(my $source = $funcs{$fn}) =~ s/\n/ /g;
print WRITER $source, "\n";
printf WRITER "%s(%s)\n", $fn, ++$count;
}
sleep 1;
} else {
while (<READER>) {
chomp;
say "$$ child read $_";
eval $_;
}
}
say "done";
__DATA__
sub func1 {
sleep 0.1; say "Hello from func1 (@_)";
}
sub func2 {
sleep 0.1; say "Hello from func2 (@_)";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment