在 PowerShell 中查看嵌套私有函数定义
PowerShell 提供了一种简单的技术来查看函数的内容,例如
Get-Content function:MyFuncName # (A)
或等效地
(Get-ChildItem function:MyFuncName).definition # (B)
,其中 MyFuncName 是我的函数的名称。这对于简单函数(即仅使用基本语言构造且不调用其他函数的函数)来说非常有用。但请考虑下面所示的函数 foo
,它包含对函数 bar
的调用。在典型情况下,它们都包含在同一个模块中,该模块的公共 API 仅由函数 foo
组成,因此它是唯一导出的函数。
function foo ()
{
$p = bar "here"
"result is '$p'"
}
function bar ([string] $s)
{
$s + $s
}
Export-ModuleMember foo
有没有办法以与上面(A)或(B)类似的方式查看另一个函数中嵌套的非导出函数(例如函数bar
)? (也就是说,无需在编辑器中打开 .psm1 文件:-)
PowerShell provides a simple technique to view the contents of a function, e.g.
Get-Content function:MyFuncName # (A)
or equivalently
(Get-ChildItem function:MyFuncName).definition # (B)
where MyFuncName is the name of my function. That is great for simple functions (i.e. functions that use only base language constructs and do not call other functions). But consider the function foo
shown below that contains a call to the function bar
. In a typical scenario these would both be contained in the same module whose public API consists solely of the function foo
and thus it is the only function exported.
function foo ()
{
$p = bar "here"
"result is '$p'"
}
function bar ([string] $s)
{
$s + $s
}
Export-ModuleMember foo
Is there any way to view the nested, non-exported functions (like function bar
) within another function in a fashion comparable to (A) or (B) above? (That is, without opening the .psm1 file in an editor :-)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不确定是否可以对模块中的特定函数执行此操作,但可以对整个模块执行此操作:
我认为函数 foo 调用函数 bar 的事实直到运行时才知道。
更新
有志者事竟成:-) 以下是访问私有模块成员的方法。使用脚本块调用模块。在脚本块内部,私有成员是可见的。
感谢 PowerTips :-) http ://powershell.com/cs/blogs/tips/archive/2009/09/18/accessing-hidden-module-members.aspx
更新2
找到这个小 PowerTip 片段后,我有点好奇到底发生了什么...该片段使用带有两个参数的调用运算符
&
。System.Management.Automation.PSModuleInfo
)所以真正发生的是
PSModuleInfo
类型的Invoke
方法是被召唤。脚本块中的代码与模块代码的其余部分在相同的会话状态下运行,因此它可以访问私有成员。此代码与 PowerTip 代码段执行完全相同的操作:在此处查看调用方法: http://msdn.microsoft.com/en-us/library/system.management.automation.psmoduleinfo.invoke(v=vs.85).aspx
I'm not sure if you can do it for a particular function in a module but you can do it for the whole module:
I think the fact that function foo calls function bar is not known until runtime.
Update
Where there's a will, there's a way :-) Here's how you can access private module members. Invoke the module with a scriptblock. Inside the scriptblock the private members are visible.
Thanks to PowerTips :-) http://powershell.com/cs/blogs/tips/archive/2009/09/18/accessing-hidden-module-members.aspx
Update 2
After finding the little PowerTip snippet I was kinda curious what was really going on... The snippet uses the call operator
&
with two arguments.System.Management.Automation.PSModuleInfo
)So what's really going on is the
Invoke
method of thePSModuleInfo
type is being called. The code in the script block runs within the same session state as the rest of the module code so it has access to the private members. This code does the exact same thing as the PowerTip snippet:Check out the invoke method here: http://msdn.microsoft.com/en-us/library/system.management.automation.psmoduleinfo.invoke(v=vs.85).aspx
不。您使用的方法是通过本地范围内的函数提供者获取函数的定义。它只会看到已在本地作用域中定义的函数或在父作用域中可见的函数。
当你调用一个函数时,它在它自己的作用域中运行。该函数创建的任何函数都将在该子作用域中创建,并且仅在该函数运行期间存在。当函数完成后,它运行的作用域将被释放,它创建的所有函数也随之消失。
No. The method you're using is getting the definition of the function via the function provider in the local scope. It will only see functions that have been defined in the local scope or that are visible in parent scopes.
When you call a function, it runs in it's own scope. Any functions the function creates will be created in that child scope, and only exist during the time that function is running. When the function is done, the scope it was running it gets disposed of, and all the functions it created go with it.