Skip to content

Instantly share code, notes, and snippets.

Created August 10, 2015 20:44
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 anonymous/de0b71ceb9ec64065478 to your computer and use it in GitHub Desktop.
Save anonymous/de0b71ceb9ec64065478 to your computer and use it in GitHub Desktop.
#!/usr/bin/perl6
use v6;
sub supply-capture-stdout (Supply $s, Callable $code) {
my $count = 0;
my $stdout = $*OUT;
$*OUT = class {
method print (*@args) {
$s.emit(@args);
$count++;
}
method flush () {}
}
$code.();
$*OUT = $stdout;
return $count;
}
my $string = "BEGIN\n";
my $out-supply = Supply.new();
my $sup-count = 0;
$out-supply.tap( -> $data { $sup-count++; $string ~= "$sup-count : Emitted $data\n"; });
my $count = supply-capture-stdout($out-supply, {
my $ticker = Supply.interval(1);
$ticker.tap( -> $sec { print $sec.WHICH , $sec; });
sleep (5);
});
$out-supply.close;
print "AFTER:\n$string";
print "COUNT: $count\t$sup-count";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment