Skip to content
Browse files

Capturing STDOUT.

  • Loading branch information...
1 parent a4697b5 commit df8be366df2493d0f0d01306b928bb3549e62607 @sergot committed Apr 14, 2012
Showing with 62 additions and 0 deletions.
  1. +6 −0 examples/e0.p6
  2. +12 −0 examples/e1.p6
  3. +44 −0 lib/IO/Capture/Simple.pm
View
6 examples/e0.p6
@@ -0,0 +1,6 @@
+use v6;
+use IO::Capture::Simple;
+
+my $r = capture_stdout { print "OH"; print " HAI", "!"; };
+
+say "RESULT: $r";
View
12 examples/e1.p6
@@ -0,0 +1,12 @@
+use v6;
+use IO::Capture::Simple;
+
+my $result;
+capture_stdout($result);
+
+print "OH ";
+say "HAI!";
+
+stdout_off;
+
+print "RESULT: $result";
View
44 lib/IO/Capture/Simple.pm
@@ -0,0 +1,44 @@
+module IO::Capture::Simple;
+
+my $stdout = $*OUT;
+
+multi sub capture(Block $code) is export {
+ ...
+}
+
+multi sub capture_stdout(Block $code) is export {
+ my $result;
+
+ $*OUT = class {
+ method print(*@args) {
+ $result ~= @args.join;
+ }
+ }
+
+ $code.();
+
+ $*OUT = $stdout;
+
+ $result;
+}
+
+multi sub capture_stdout($target is rw) {
+ $*OUT = class {
+ method print(*@args) {
+ $target ~= @args.join;
+ }
+ }
+ Bool::True;
+}
+
+multi sub capture_stderr(Block $code) is export {
+ ...
+}
+
+multi sub capture_stdin(Block $code) is export {
+ ...
+}
+
+sub stdout_off is export {
+ $*OUT = $stdout;
+}

0 comments on commit df8be36

Please sign in to comment.
Something went wrong with that request. Please try again.