๐Ÿ“˜ Dynamic variables in Perl 6

๐Ÿ“˜ Dynamic variables in Raku

N. B. Perl 6 has been renamed to Raku. Click to read more.


The scope of dynamic variables is calculated at the moment when a variable is accessed. Thus, two or more calls of the same code may produce different results.

Dynamic variables are marked with the * twigil (a character clearly referencing a wildcard).

In the following example, the echo() function prints a dynamic variable $*var, which is not declared in the function, nor is it a global variable. It, nevertheless, can be resolved when used in other functions, even if they have their own instances of the variable with the same name.

sub alpha {
ย ย ย  my $*var = 'Alpha';
ย ย ย  echo();
}

sub beta {
ย ย ย  my $*var = 'Beta';
ย ย ย  echo();
}

sub echo() {
ย ย ย  say $*var;
}

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

Leave a Reply

Your email address will not be published. Required fields are marked *

Retype the CAPTCHA code from the image
Change the CAPTCHA codeSpeak the CAPTCHA code