Course of Raku / Objects, I/O, and exceptions / Input and output / Standard streams / Exercises / Output and error
Solution: Output and error
Here is a possible solution to the task.
Code
my @data = 3, 5, 7;
my $sum = 0;
for @data -> $n {
$sum += $n;
$*ERR.say("running total: $sum");
}
$*OUT.say($sum);🦋 You can find the source code in the file output-and-error.raku.
Output
On screen, both streams appear — the running totals, then the final result:
running total: 3
running total: 8
running total: 15
15If standard error is discarded, only the real output remains:
$ raku output-and-error.raku 2>/dev/null
15Comments
$*OUTand$*ERRare the handles for standard output and standard error. Each has its ownsaymethod.Each pass of the loop adds the next number and writes the running total to
$*ERR; only the final sum goes to$*OUT. On the screen the progress and the result look alike, but they travel through different streams — which is why redirecting standard error away with2>/dev/nullleaves just the final15behind.