在 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.
如何查看?
Devel::Peek
模块对于获取有关变量的各种信息非常方便.您可以使用它做的一件事是转储对子程序的引用并获取它来自的 glob 的名称:
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:
$ perl -MDevel::Peek -MList::Util=first -e'Dump(&first)'
SV = IV(0x1094e20) at 0x1094e28
REFCNT = 1
FLAGS = (TEMP,ROK)
RV = 0x11183b0
SV = PVCV(0x10ff1f0) at 0x11183b0
REFCNT = 3
FLAGS = (POK,pPOK)
PROTOTYPE = "&@"
COMP_STASH = 0x0
XSUB = 0x7f7ecbdc61b0
XSUBANY = 0
GVGV::GV = 0x11183c8 "List::Util" :: "first"
FILE = "ListUtil.c"
DEPTH = 0
FLAGS = 0x800
OUTSIDE_SEQ = 0
PADLIST = 0x0
OUTSIDE = 0x0 (null)
GVGV::GV
部分很重要.
另一种解决方案是Sub::Identify
,它实际上只为您提供给它的代码引用的名称.然而,了解 Devel::Peek
在许多其他情况下也很方便,所以我首先提到了这一点.
An alternative solution would be Sub::Identify
, which really only gives you names for code references you hand to it. However, knowing about Devel::Peek
is handy in many other situations too, so I mentioned that first.