之间有什么区别
Write-Host (Get-Date) # just paren
Run Code Online (Sandbox Code Playgroud)
和
Write-Host $(Get-Date) # dollar-paren
Run Code Online (Sandbox Code Playgroud)
仅举一个简单的例子,内容中的内容可以是任何东西。两者之间有什么区别吗?
我认为自己对PS有一定的经验,但是这些小问题困扰着我,尤其是在代码审查等过程中。有没有人能找到足够详细的“语言在这里是如何工作的”很好的资料,以得出这些问题的答案?
子表达式($(...))包含一个StatementBlockAst。它可以采用任意数量的语句,包括关键字(if,foreach等),管道,命令等。解析类似于begin/ process/ 等命名块的内部end。
的括号表达式((...))可以包含单个 ExpressionAst其是AST的有限子集。语句和表达式之间最显着的区别是不分析关键字。
$(if ($true) { 'It worked!' })
# It worked!
(if ($true) { 'It worked!' })
# if : The term 'if' is not recognized as the name of a cmdlet, function,
# script file, or operable program. Check the spelling of the name, or
# if a path was included, verify that the path is correct and try again.
# At line:1 char:2
# + (if ($true) { 'It worked' })
# + ~~
# + CategoryInfo : ObjectNotFound: (if:String) [], CommandNotFoundException
# + FullyQualifiedErrorId : CommandNotFoundException
Run Code Online (Sandbox Code Playgroud)
同样,正如其他人指出的那样,子表达式将用双引号引起来的字符串扩展。
| 归档时间: |
|
| 查看次数: |
556 次 |
| 最近记录: |