Course of Raku / Advanced / Containers / Special and dynamic variables 🆕 / Dynamic variables

Quiz — Dynamic variables

What does the following program print?

my $*n = 1;
sub show { say $*n }

sub run {
    my $*n = 2;
    show();
}

run();
01
12
0Nothing
0An error

show is written at the top level, right next to my $*n = 1, but that placement is irrelevant — a dynamic variable is looked up through the call stack, not by where the code sits. Here show is called from run, which redeclared $*n as 2, so that is the value in effect and the program prints 2.

Course navigation

Dynamic variables   |   Built-in special variables