Course of Raku / Advanced / Containers / Special and dynamic variables 🆕
Built-in special variables
Raku predefines a set of variables for you. Most of them are
dynamic (the * twigil), so — as you saw with dynamic
variables — you can read them anywhere and even override them for a
scope. Here are the ones you will reach for most often.
Standard streams
Input and output flow through three dynamic variables:
$*OUT (standard output), $*ERR (standard
error), and $*IN (standard input). say and
print write to $*OUT; to send text to standard
error instead, call the method on $*ERR:
$*OUT.say('normal output'); # goes to standard output
$*ERR.say('a diagnostic'); # goes to standard errorBecause these are dynamic, redirecting $*OUT in a block
sends the output of everything called inside that block to the new
destination — without changing any of that code.
The program and its process
A few variables describe the running program itself:
say $*PROGRAM-NAME; # the path of the script being run
say $*PID; # the process id of this program
say $*CWD; # the current working directoryTheir values change from run to run, so no fixed output is shown here.
Command line and environment
The arguments and the environment arrive in two ready-made containers:
@*ARGS— the list of command-line arguments (the same ones aMAINsubroutine receives)%*ENV— the environment variables, as a hash
say @*ARGS.elems; # how many arguments were passed
say %*ENV<HOME>; # the value of the HOME environment variableThese built-ins save you from wiring up the same information by hand.
And because they are dynamic, looked up through the call stack, any
$* built-in can be overridden for a scope exactly like a
$* variable of your own.
Alongside these dynamic variables, Raku also has the compile-time
? built-ins — $?FILE and $?LINE —
which you already met on the twigils page.
Practice
Complete the quiz that covers the contents of this topic.
Course navigation
← Quiz — Dynamic variables | Quiz — Special variables →
💪 Or jump directly to the exercises in this
section.