diff --git a/lib/IO/Capture/Simple.pm b/lib/IO/Capture/Simple.pm index b1e230d..33ec2d9 100644 --- a/lib/IO/Capture/Simple.pm +++ b/lib/IO/Capture/Simple.pm @@ -1,6 +1,7 @@ module IO::Capture::Simple; my $stdout = $*OUT; +my $stderr = $*ERR; multi sub capture(Block $code) is export { ... @@ -29,13 +30,35 @@ multi sub capture_stdout($target is rw) { } multi sub capture_stderr(Block $code) is export { - ... + my $result; + + my $*ERR = class { + method print(*@args) { + $result ~= @args.join; + } + } + + $code.(); + + $result; +} + +multi sub capture_stderr($target is rw) is export { + $*ERR = class { + method print(*@args) { + $target ~= @args.join; + } + } } multi sub capture_stdin(Block $code) is export { ... } -sub stdout_off is export { +sub capture_stdout_off is export { $*OUT = $stdout; } + +sub capture_stderr_off is export { + $*ERR = $stderr; +} diff --git a/t/t0.p6 b/t/t0.p6 index fc47699..fa0bff5 100644 --- a/t/t0.p6 +++ b/t/t0.p6 @@ -12,5 +12,5 @@ $r = ''; capture_stdout($r); print $test; -stdout_off; +capture_stdout_off; ok $r ~~ $test; diff --git a/t/t1.p6 b/t/t1.p6 new file mode 100644 index 0000000..7c61ac0 --- /dev/null +++ b/t/t1.p6 @@ -0,0 +1,18 @@ +use IO::Capture::Simple; +use Test; + +plan 2; + +my $test = "OH HAI!"; + +my $r = capture_stderr { note $test } + +ok $r ~~ /$test/; + +$r = ''; + +capture_stderr($r); +note $test; +capture_stderr_off; + +ok $r ~~ /$test/;