Powershell 大师,请阐明函数中的变量范围 [英] Powershell gurus, please clarify variable scope in function

查看:39
本文介绍了Powershell 大师,请阐明函数中的变量范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我读到当函数脚本是点源时,函数作用域内的变量可以在当前作用域中访问.

I read that variables inside the scope of a function are accessible in the current scope when the function script is dot-sourced.

是真的吗?这很奇怪,我认为不寻常......

Is it true? It's very strange, and unusual I think...

我能解释一下原因吗?例如,my-f$my-f-var 设置为某个整数,假设为 2:

Can me clarify the reasons of this. Example, my-f sets $my-f-var to some integer, let's say 2:

PS1> . .\my-f-script.ps1
PS1> my-f
PS1> $my-f-var
2

我希望 $my-f-var 不可访问,因为在函数内部!有没有办法将变量设为私有,或者有没有办法调用函数而无需点源其脚本?

I would expect that $my-f-var is not accessible because inside the function! Is there a way to make variables private, or a way to call the function without dot-sourcing its script?

推荐答案

函数内的变量是该作用域的局部变量,除非你在函数名的调用上加点.当您点源脚本时,顶级脚本变量将与函数定义一起有效地导入当前范围,例如:

Variables within the function are local to that scope unless you dot the invocation of the function name. When you dot source a script, the top-level script variables are effectively imported into the current scope along with the function definitions e.g.:

PS> '$scriptvar = 2; function my-f { ${my-f-var} = 2 }' > my-f-script.ps1
PS> Remove-Variable scriptvar, my-f-var
Remove-Variable : Cannot find a variable with name 'scriptvar'.
Remove-Variable : Cannot find a variable with name 'my-f-var'.
PS> . .\my-f-script.ps1
PS> $scriptvar
2
PS> ${my-f-var}
PS>

请注意,${my-f-var} 未在本地范围内定义.但是,如果我点"了函数的调用,那么它的内容将在当前范围内运行,例如:

Note that ${my-f-var} isn't defined in the local scope. However, if i 'dot' the invocation of the function then its contents are run in the current scope e.g.:

PS> . my-f
PS> ${my-f-var}
2

在这种情况下,函数中设置的变量设置在当前(调用)范围内,因为用于调用它的点".

In this case, the variable set in the function is set in the current (invoking) scope because of the 'dot' used to invoke it.

还有几点:您可以使用 Get-Variable -scope 或更方便(如果不那么灵活)全局、脚本、本地和私有修饰符访问各种范围内的变量.当您访问在更高范围内定义变量的函数内的变量时,您可以很好地读取它.但是,当您设置它时,PowerShell 基本上会对该变量进行写时复制"——创建一个范围从该函数向下(即到它调用的其他函数)的新副本.如果您真的想修改更高范围的变量,您可以使用 $global:Foo 或 $script:Foo 在这些范围内进行修改.

A couple of more points: you can access variables at various scopes using either Get-Variable -scope or the more convenient (if less flexible) global, script, local and private modifiers. When you access a variable inside a function where the variable is defined in a higher scope, you can read it just fine. But when you set it, PowerShell essentially does a "copy-on-write" of the variable - creating a new copy scoped from that function downward (ie to the other functions it calls). If you really want to modify a higher scoped variable you can use $global:Foo or $script:Foo to modify at those scopes.

local 作用域可以派上用场,如果您想避免无意中使用在您的函数之外定义的变量.另一位 MVP 在上次 MVP 峰会上提出了这个问题,这似乎是最佳实践"技巧之一.场景如下:

The local scope comes in handy if you want to avoid inadvertently using a variable defined outside of your function. Another MVP brought this up at the last MVP summit and it seems like one of those "best practice" tips. Here's the scenario:

PS> $foo = 'bad'
PS> function testscope { $fooo = 'good'; "The value of `$fooo is $foo" }
PS> testscope
The value of $fooo is bad

请注意,在这种情况下,在函数内使用 $foo 是一个错字,但巧合的是,有一个变量名是该错字.这不是该函数的意图,它无法正常工作,尽管在这种情况下很难看出这一点.下面,我们可以使用 local 说明符来确保函数只在变量的局部范围内查找.由于打字错误,它没有找到它,但至少现在错误更容易发现了.

Note that in this case the use of $foo inside the function is a typo but coincidentally there is a variable by that typo'd name. This was not the intent of the function and it is not working correctly although that is hard to see in this case. Below, we can use the local specifier to make sure the function only looks within the local scope for the variable. It doesn't find it because of the typo but at least the error is a bit easier to spot now.

PS> function testscope { $fooo = 'good'; "The value of `$fooo is $local:foo" }
PS> testscope
The value of $fooo is

private 作用域在您不希望某些变量对您的函数调用的其他函数可见时很有用.

The private scope is useful when you don't want certain variables to be visible to the other functions that your function calls.

这篇关于Powershell 大师,请阐明函数中的变量范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆