Variable references
Like in other programming languages, we have the capability of taking references to variables, by using the prefix backslash (\) operator for referencing and the prefix asterix (*) operator for dereferencing:
1
var name = "sidef"
2
var ref = \name
3
var original = *ref
Copied!
Variable references are useful when passing them to functions (or methods) for assigning values.
1
func assign2ref (ref, value) {
2
*ref = value
3
}
4
5
var x = 10
6
assign2ref(\x, 20)
7
say x # prints: 20
Copied!
There is also the possibility of taking references to the lvalues of an array or hash:
1
var a = [41, 42, 43]
2
var r = \a[1] # reference of the second element
3
4
*r = 99 # changes the value inside the array
5
say a # prints: [41, 99, 43]
Copied!
Last modified 1yr ago
Copy link