September 2017
Beginner
402 pages
9h 52m
English
Dynamic scope is utilizing the * twigil, for example, a dynamic scalar variable $*a. Unlike regular local variables, dynamic variables can be used in functions, which are called from the current scope of a variable. Consider an example:
sub f() { $*a++;}my $*a = 1;f();say $*a; # 2
Here, $*a is a dynamic variable in the main program. It is initialized with the value of 1. The f function, when it is called, changes the value of the same variable, thus the program prints 2. The thing is that the $*a is not declared inside the f function. The compiler will search for this name looking at the scopes, in which the function is called.
In the preceding example, a simple global variable could be used instead of a dynamic variable:
Read now
Unlock full access