Hi,
is there a way to capture stdout
from a function?
Like this in Perl:
#!/usr/bin/env perl
use strict;
use warnings;
use feature qw(say);
use Capture::Tiny qw(capture);
my $fn = sub {print shift};
say ((capture {$fn->("fubar")})[0]);
__END__
karl@rantanplan:~/src/crystal$ ./capture.pl
fubar
Regards, Karl
Update:
Thanks to all for the kind and helpful replies.
I just searched a cheap trick to capture output from a C function “in-a-hurry”.
Now i know that this doesn’t work for a function that returns void
- Crystal is too clever.
I also know now that i can’t pass a struct
defined in a C lib from Crystal (via new
) to the function call.
I had the same idea as Billy:
26 | fn(io, GMP.fac(3))
^--
Error: passing Void return value of lib fun call has no effect
Same result with the solution from Jamie, as expected.
In the packt book from Dietrich are some examples with callbacks…