MAIN FEEDS
Do you want to continue?
https://www.reddit.com/r/PowerShell/comments/b9ugma/powershell_tricks_splatting/ek7h3da
r/PowerShell • u/destruktive • Apr 05 '19
58 comments sorted by
View all comments
Show parent comments
4
How it passes the arguments. $x will convert your array using ToString and doesn't pass them properly.
$x
ToString
4 u/ka-splam Apr 05 '19 $x will convert your array using ToString and doesn't pass them properly.Clearly not, as shown by my example use of $x working instead of saying 'system.object[]' is not recognized as an internal or external command 4 u/TheIncorrigible1 Apr 05 '19 You missed the point. See: about_Parsing.Also:"$('echo', 'b', 'c')"Is more akin to what is happening here. Calling ToString literally on the array calls the wrong overloaded method. 3 u/ka-splam Apr 05 '19 You missed the point.Duh, yes, that's why I'm asking about it."$('echo', 'b', 'c')" Is more akin to what is happening here.if it were exactly that, changing $OFS would change the behaviour, which it doesn't, e.g.:PS C:\> $x = @('echo', 'b', 'c') PS C:\> "$x" echo b c PS C:\> $OFS=',' PS C:\> "$x" echo,b,c PS C:\> cmd /c $x b c PS C:\> cmd /c @x b cakin to it, how?See: about_Parsing.I'm not seeing anything there which explains the difference, or in about_Splatting, except saying that you can do it.Show an example where it behaves differently? 1 u/ka-splam Apr 08 '19 Turns out it behaves differently for cmdlets (but possible not native commands(?)):PS C:\> function test { $args -join '-' } PS C:\> $x = @('echo', 'b', 'c') PS C:\> test $x System.Object[] PS C:\> test @x echo-b-cLike Python's somefunc(*var) which I've wanted before, so woooo 1 u/TheIncorrigible1 Apr 08 '19 Yes, when used with a cmdlet, it sees that as a single argument
$x will convert your array using ToString and doesn't pass them properly.
Clearly not, as shown by my example use of $x working instead of saying 'system.object[]' is not recognized as an internal or external command
'system.object[]' is not recognized as an internal or external command
4 u/TheIncorrigible1 Apr 05 '19 You missed the point. See: about_Parsing.Also:"$('echo', 'b', 'c')"Is more akin to what is happening here. Calling ToString literally on the array calls the wrong overloaded method. 3 u/ka-splam Apr 05 '19 You missed the point.Duh, yes, that's why I'm asking about it."$('echo', 'b', 'c')" Is more akin to what is happening here.if it were exactly that, changing $OFS would change the behaviour, which it doesn't, e.g.:PS C:\> $x = @('echo', 'b', 'c') PS C:\> "$x" echo b c PS C:\> $OFS=',' PS C:\> "$x" echo,b,c PS C:\> cmd /c $x b c PS C:\> cmd /c @x b cakin to it, how?See: about_Parsing.I'm not seeing anything there which explains the difference, or in about_Splatting, except saying that you can do it.Show an example where it behaves differently? 1 u/ka-splam Apr 08 '19 Turns out it behaves differently for cmdlets (but possible not native commands(?)):PS C:\> function test { $args -join '-' } PS C:\> $x = @('echo', 'b', 'c') PS C:\> test $x System.Object[] PS C:\> test @x echo-b-cLike Python's somefunc(*var) which I've wanted before, so woooo 1 u/TheIncorrigible1 Apr 08 '19 Yes, when used with a cmdlet, it sees that as a single argument
You missed the point. See: about_Parsing.
about_Parsing
Also:
"$('echo', 'b', 'c')"
Is more akin to what is happening here. Calling ToString literally on the array calls the wrong overloaded method.
3 u/ka-splam Apr 05 '19 You missed the point.Duh, yes, that's why I'm asking about it."$('echo', 'b', 'c')" Is more akin to what is happening here.if it were exactly that, changing $OFS would change the behaviour, which it doesn't, e.g.:PS C:\> $x = @('echo', 'b', 'c') PS C:\> "$x" echo b c PS C:\> $OFS=',' PS C:\> "$x" echo,b,c PS C:\> cmd /c $x b c PS C:\> cmd /c @x b cakin to it, how?See: about_Parsing.I'm not seeing anything there which explains the difference, or in about_Splatting, except saying that you can do it.Show an example where it behaves differently? 1 u/ka-splam Apr 08 '19 Turns out it behaves differently for cmdlets (but possible not native commands(?)):PS C:\> function test { $args -join '-' } PS C:\> $x = @('echo', 'b', 'c') PS C:\> test $x System.Object[] PS C:\> test @x echo-b-cLike Python's somefunc(*var) which I've wanted before, so woooo 1 u/TheIncorrigible1 Apr 08 '19 Yes, when used with a cmdlet, it sees that as a single argument
3
You missed the point.
Duh, yes, that's why I'm asking about it.
"$('echo', 'b', 'c')" Is more akin to what is happening here.
if it were exactly that, changing $OFS would change the behaviour, which it doesn't, e.g.:
$OFS
PS C:\> $x = @('echo', 'b', 'c') PS C:\> "$x" echo b c PS C:\> $OFS=',' PS C:\> "$x" echo,b,c PS C:\> cmd /c $x b c PS C:\> cmd /c @x b c
akin to it, how?
See: about_Parsing.
I'm not seeing anything there which explains the difference, or in about_Splatting, except saying that you can do it.
Show an example where it behaves differently?
1
Turns out it behaves differently for cmdlets (but possible not native commands(?)):
PS C:\> function test { $args -join '-' } PS C:\> $x = @('echo', 'b', 'c') PS C:\> test $x System.Object[] PS C:\> test @x echo-b-c
Like Python's somefunc(*var) which I've wanted before, so woooo
somefunc(*var)
1 u/TheIncorrigible1 Apr 08 '19 Yes, when used with a cmdlet, it sees that as a single argument
Yes, when used with a cmdlet, it sees that as a single argument
4
u/TheIncorrigible1 Apr 05 '19
How it passes the arguments.
$xwill convert your array usingToStringand doesn't pass them properly.