Skip to content

Instantly share code, notes, and snippets.

@zoffixznet
Created January 8, 2016 17: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 zoffixznet/40062b484b67096ac0d8 to your computer and use it in GitHub Desktop.
Save zoffixznet/40062b484b67096ac0d8 to your computer and use it in GitHub Desktop.
use Test;
class IO::Captured is IO::Handle {
has @.w;
method say (*@w) {
@.w.push: @w.join: ""
};
method Str {@.w.join: ""}
};
sub output-capture (&code) {
my $capture = IO::Captured.new;
my $saved-out = $PROCESS::OUT;
my $saved-err = $PROCESS::ERR;
$PROCESS::OUT = $capture;
$PROCESS::ERR = $capture;
&code();
$PROCESS::OUT = $saved-out;
$PROCESS::ERR = $saved-err;
return $capture.Str;
};
#is 1, 1;
my $x = output-capture { EVAL "use Test; is 1, 2; done-testing" };
say "Captured [[$x]]";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment