This tablet is about the core stuff of Perl's genesis - reading and writing files, running commands, and gluing parts of the operating environment together in new ways.

Interacting with the Terminal

The command terminal (STDOUT to Perl 5 programmer, and $*OUT in Perl 6) is the primary communication device for a script with its user. Most tutorials begin with writing a line to that black window.

say

say 'Dear Earthlings!';

Write text to the standard output ($*OUT, called STDOUT in Perl 5). This example just prints a string literal, but any type of value is allowed. say adds a line ending, according to the operating system convention. So every time you "say" something, it will start on a fresh line.

You can also give say a list of values. Each item will be taken as an expression, that has to evaluated. The results will be combined, just like with ~, and finally the line ending will be added.

print

Like say, just without adding the line ending. Remember it like this: machines print explicitly, character by character without pauses. People, on the other hand, speak in sentences or speech balloons, which have to be separated.

printf

It's a print that understands also the syntax of sprintf. It's a very lightweight framework for formated output. If you need more, use formats or even a template engine.

my $num = 22/7;
printf "Even on $our_home_planet, the ratio of a circle's circumference to its diameter is approximately %0.3f\n", $num;

note

Works like say, but writes to $*ERR (known as STDERR in Perl 5). Content sent to $*ERR can be redirected separately from normal program output, and is often used for progress reports, extra debugging information, error messages, etc.

warn

Behaves somewhat like print to $*ERR (see note). Under the hood, warn is a "resumable exception", and its message can be caught and handled farther up the call stack in some way. Normally, once it's handled, Perl will resume running the code just after the warn call.

get

Reads one line of input from $*IN (called STDIN in Perl 5), and strips the line ending. So unlike in Perl 5, you don't have to chomp input. All following line-oriented commands (e.g., prompt, lines) behave that way.

prompt

Performs two-way communication with the terminal. It does an output like print, followed by a read like get.