Dynamic variables

Dynamic variables are variables whose actual scope may change depending on when you access this variable. Dynamic variables names contain a twigil *, for example: $*x.

In the following code snippet, $*var is the dynamic variable. In either subroutine, a() or b() it is declared as a lexical variable using the my keyword. Although when it is red inside the echo() subroutine, its actual scope will be different.

sub alpha {
    my $*var = 'Alpha';


sub beta {
    my $*var = 'Beta';


sub echo() {
    say $*var;

alpha(); # Alpha
beta();  # Beta

There are predefined dynamic variables, like those for the standard IO file handles: $*IN, $*OUT, and $*ERR.