Printing to Standard Error in Perl 6

When you run a program on the command line it automatically opens two channels for output. One is called Standard Output the other one is called Standard Error.

By default they are both connected to the terminal where you ran the program, but you can separate them and redirect them or hide them separately.

Note! This site is about Perl 6.
If you are looking for a solution for Perl 5, please check out the Perl 5 tutorial.
Specifially there is a related article: Standard output, standard error and command line redirection.

As a programmer, when writing that program you need to use these two channels cleverly. The normal output of the program should go to the Standard Output channel and all the other messages, warnings, errors, debugging messages should go to the Standard Error channel

That way you, the user (do you already feel the split identity that should come natural to every programmer?) can decide to hide these error messages or save them in a file.

If there is a program called cli-application you can run it as

cli-application 2> /dev/null

and all the output printed to the Standard Error channel will be throw away.

If you run

cli-application 2> err.txt

then all the content of the Standard Error will be saved in a file called err.txt.

But we are not here to show how to redirect the output channels. We are here to show you how to print to the Standard Error channel.

Printing to Standard Output

Before that however let me just point out that if you use the regular print or say functions in Perl 6, the output will go to the Standard Output. That should be the normal output of the code.

Printing to Standard Error

There are several ways to print to Standard Error.

As your Perl 6 program starts to run Rakudo automatically creates the channel to the Standard Error and assigns it to $*ERR. You can then print to Standard error by calling:

$*ERR.say: 'foo'

or some of the similar constructs.

You can also use the note function and method. It might be even preferable than using $*ERR.

Finally the dd dumper function also prints to the Standard Error. After all these are supposed to be debugging printouts.

Sample code printing to Standard Error


use v6;

say 'This goes to Standard Output';
say 'The rest will go to Standard Error';

$*ERR.say: 'foo 1';
$*ERR.say('foo 2');
$*ERR.print("foo 3\n");
$*ERR.print: "foo 4\n";


my %h = 
    name   => 'Perl 6',
    answer => 42,
    grades => [98, 73],

dd %h;

Try running it as

perl6 files/examples/print_to_stderr.pl6

This goes to Standard Output
The rest will go to Standard Error
foo 1
foo 2
foo 3
foo 4
Hash %h = {:answer(42), :grades($[98, 73]), :name("Perl 6")}

Then redirect the Standard Output to the nothingness in /dev/null. What remains is the Standard Error.

perl6 files/examples/print_to_stderr.pl6 > /dev/null

foo 1
foo 2
foo 3
foo 4
Hash %h = {:answer(42), :grades($[98, 73]), :name("Perl 6")}

The Perl 6 Tricks and Treats newsletter has been around for a while. If you are interested to get special notification when there is new content on this site, it is the best way to keep track:
Full name:
This is a newsletter temporarily running on my personal site ( using Mailman, till I implement an alternative system in Perl 6.
Gabor Szabo
Written by Gabor Szabo

Published on 2017-08-13


In the comments, please wrap your code snippets within <pre> </pre> tags and use spaces for indentation.
comments powered by Disqus
Suggest a change
Elapsed time: 3.82757952

Perl 6 Tricks and Treats newsletter

Register to the free newsletter now, and get updates and news.