Capturing a module output

  • A+
Category:Languages

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 required. 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?

 


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?

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: