Say we have this module:
unit module outputs;
say "Loaded";
And we load it like this
use v6;
use lib ".";
require "outputs.pm6";
That will print "Loaded" when it's require
d. Say we want to capture the standard output of that loaded module. We can do that if it's an external process, but there does not seem to be a way for redirecting *OUT
to a string or,if that's not possible, to a file. Is that so?
A module output is a variable in the module that contains information generated by the module and that can supply a value to other connected variables or modules.
You could try use IO::String
:
use v6;
use lib ".";
use IO::String;
my $buffer = IO::String.new;
with $buffer -> $*OUT {
require "outputs.pm6";
};
say "Finished";
print ~$buffer;
Output:
Finished
Loaded
See also If I reassigned OUT in Perl 6, how can I change it back to stdout?
Temporarily reassigning $*OUT
so that .print
calls append to a string:
my $capture-stdout;
{
my $*OUT = $*OUT but
role { method print (*@args) { $capture-stdout ~= @args } }
require "outputs.pm6" # `say` goes via above `print` method
}
say $capture-stdout; # Loaded
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With