As part of learning its basics i am implementing a ternary operator cmdlet in pws. I have it taking scriptblocks, to emulate the conditional evaluation ternary operators usually have. And in most instances it works fine.
function valOf($var){
if($var -is [scriptblock]){
return & $var }
else {
return $var }
}
function ternary([bool]$condition, $t, $f){
if($condition){
return valOf($t) }
else{
return valOf($f) }
#return @($t,$f)[!$condition]
}
I got in trouble when i started nesting scriptblocks:
$i=56;
&{
$i=0
ternary($true) {$script:i+=2} {write-host "onFalse"}
$i #wanted:2 #reality: 58
<# without '$script: $i' is indeed 0, but cannot be edited #>
}
$i #wanted:56 #reality:58
How can i access the middle scope?
browsing the documentation as well as the forum this seems to be quite a common issue, but the theme is anything but clear x.x
Perhaps an invokeCommand that optsOut from the copyOnWrite behaviour..?