As I learn about how Perl and PowerShell differ, I'm trying to pin down differences between passing and updating lists by reference. I think I get the idea now, PowerShell-wise.
Pass a hash table by reference:
When the function is called: It isn't necessary to precede the table's name with [ref]. Within the function: The table's name in the param list can be preceded simply by [hashtable], not [ref] — (because "as received" it is already a reference; so it was explained to me). If the hash table is to be updated within the function, .Value is not needed when [ref] hasn't been used. IOW: call the function this way: MyFunction $MyHashTable. The function contains:
param([hashtable]$HashNameWithinFunction)
$HashNameWithinFunction.Add('x', 'y')
Pass an array by reference:
Both when the function is called and in the function's param() list: the array's name must be preceded by [ref]. When the array is to be updated by reference, .Value must be used. The function is called this way: MyFunction ([ref]$MyArray). The function contains:
param([ref]$ArrayNameWithinFunction)
$ArrayNameWithinFunction.Value += 'something new'
Is my understanding correct? I've tested the above and I know both work. But is there any potential for some subtle error in doing it those ways?
Adding this following reply from Chrstian:
function UpdateArray {
param([ref]$ArrayNameWithinFunction)
$ArrayNameWithinFunction.Value += 'xyzzy'
}
$MyArray = @('a', 'b', 'c')
UpdateArray ([ref]$MyArray)
value?