Course of Raku / Objects, I/O, and exceptions / Input and output

Standard streams

Every program has three standard streams connecting it to its environment: standard output, standard error, and standard input. Raku makes them available through three special variables: $*OUT, $*ERR, and $*IN.

You have been writing to standard output all along: say and print send their text to $*OUT.

say 'Hello'; # goes to standard output

Diagnostic and error messages are kept separate, on standard error, so they do not get mixed into the program’s real output. The note routine writes there:

note 'Something looks wrong'; # goes to standard error

This separation is useful because the two streams can be redirected independently — for example, you can save a program’s output to a file while still seeing its error messages on screen.

You can also write to the streams directly. Each of $*OUT and $*ERR is a handle with its own say and print methods:

$*OUT.say('a normal line');
$*ERR.say('a diagnostic line');

The third stream, $*IN, is standard input. You met it indirectly through prompt, which reads a line from it. We return to reading input when we look at file handles, because $*IN is a handle just like an open file.

Practice

Complete the quiz that covers the contents of this topic.

Exercises

This section contains 3 exercises. Examine all the topics of this section before doing the coding practice.

  1. Output and error
  2. Note a warning
  3. Print without a newline

Course navigation

Input and output   |   Output and error