Perl 猴子修补是否允许您查看修补包的范围?

发布于 2024-07-12 13:22:59 字数 284 浏览 6 评论 0原文

我正在使用 “如何在 Perl 中对实例方法进行猴子修补?”。 我遇到的问题是,原始子例程使用了包级 my 变量,修补后的子例程似乎无法通过完整路径规范或隐式使用来访问该变量。

有没有什么方法可以获取以这种方式限定范围的数据以在修补的子例程中使用?

I'm monkey patching a package using a technique given at the beginning of "How can I monkey-patch an instance method in Perl?". The problem that I'm running into is that the original subroutine used a package-level my variable which the patched subroutine appears not to have access to, either by full path specification or implicit use.

Is there any way to get at the data scoped in this way for use in the patched subroutine?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

只等公子 2024-07-19 13:22:59

您可以使用 PadWalker 模块获取词汇。 邪恶,但有效。

You can obtain lexicals with the PadWalker module. Evil, but it works.

小姐丶请自重 2024-07-19 13:22:59

不。您错误的是它们不在包范围内。 根据定义,词法变量仅限于其词法范围,换句话说,仅限于其所在的块。

No. The thing you're mistaken in is that they are not package scoped. A lexical variable is by definition limited to its lexical scope, in other words, the block it is in.

筱武穆 2024-07-19 13:22:59

词法(即:用“my”声明)在声明它们的词法范围(文件或块)之外不可见。 这就是词法变量的全部意义。

如果有一个子例程/方法与词法 var 处于相同的范围内,那么它可以返回词法的值,并且可以允许从其范围之外间接访问 var。

词法变量不存在“完整路径规范”。 这是针对包变量的。 如果 var 是用“our”而不是“my”声明的,那么您可以这样做。

Lexicals (ie: declared with 'my') are not visible outside the lexical scope (file or block) in which they are declared. That's the whole point of lexical variables.

If there is a subroutine/method which is in the same scope as the lexical var, then it can return the value of the lexical and that can allow indirect access to the var from outside its scope.

There is no such thing as a 'full path specification' for lexical variables. That's for package variables. If the var was declared with 'our' instead of 'my' you could do that.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文