Dynamic variable names
It is not possible to create a new lexical variable at run-time, but there are other various ways to do something similar.
1
var name = read("Enter a variable name: ", String); # type in 'foo'
2
3
class DynamicVar(name, value) {
4
method init {
5
DynamicVar.def_method(name, ->(_) { value })
6
}
7
}
8
9
var v = DynamicVar(name, 42); # creates a dynamic variable
10
say v.foo; # retrieves the value
Copied!
Last modified 1yr ago
Copy link