在 Perl 中,如何检查给定函数是从哪个模块导入的?
我有一个调用该函数的代码。但我不知道这个函数属于哪个模块。我需要它来修改这个功能。
我怎样才能检查它?
I have a code which calls the function. But I don't know the module this function belongs to. I need it to modify this function.
How can I check it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
Devel::Peek
模块非常方便获取有关变量的各种信息。您可以用它做的一件事是转储对子例程的引用并获取它来自的 glob 的名称:其中的 GVGV::GV 部分是重要的部分。
另一种解决方案是
Sub::Identify
,它实际上只为您提供代码引用的名称。然而,了解Devel::Peek
在许多其他情况下也很方便,所以我首先提到了这一点。The
Devel::Peek
module is very handy to get all sorts of information about variables. One of the things you can do with it is dump a reference to a subroutine and get the name of the glob it came from:the
GVGV::GV
part in there is the important bit.An alternative solution would be
Sub::Identify
, which really only gives you names for code references you hand to it. However, knowing aboutDevel::Peek
is handy in many other situations too, so I mentioned that first.Perl 的调试器可以按照您想要的方式进行挖掘。例如:
它使用 Devel::Peek 来执行此操作:
来执行它
您可以使用Output :
请注意,上面的示例为
Foo:bar
提供了不同的名称,但您可以获得别名子所在的包及其名称。Perl's debugger can dig down the way you want. For example:
It does this using Devel::Peek:
You might exercise it with
Output:
Note that the example above gives
Foo:bar
a different name, but you get both the package where the aliased sub resides and also its name there.如果该函数是使用
Exporter
从另一个模块自动导入的,则可以在此模块的@EXPORT
全局变量中找到它:您可以提供要使用的函数列表。这样你就总能知道函数属于哪个包:
If the function was automatically imported from another module using
Exporter
, it can be found in this module's@EXPORT
global variable:You can provide a list of functions to
use
. This way you will always know which package a function belongs:您可以将任何子例程引用传递给
Sub::Identify::sub_fullname
,它将显示定义此子例程的模块:有关详细信息,请参阅
Sub::Identify
You can pass to
Sub::Identify::sub_fullname
any subroutine reference and it will show you the module where this sub was defined:For details see
Sub::Identify