发现 Perl 应用程序当前定义的所有变量的最佳方法是什么?
我正在寻找最好、最简单的方法来做类似的事情:
$var1="value";
bunch of code.....
**print allVariablesAndTheirValuesCurrentlyDefined;**
I am looking for best, easiest way to do something like:
$var1="value";
bunch of code.....
**print allVariablesAndTheirValuesCurrentlyDefined;**
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
包变量? 词汇变量?
可以通过符号表查找包变量。 尝试 Devel::Symdump:
词法变量有点诡计,你不会发现它们在符号表中。 可以通过属于定义它们的代码块的“暂存器”来查找它们。尝试 PadWalker :
Package variables? Lexical variables?
Package variables can be looked up via the symbol table. Try Devel::Symdump:
Lexical variables are a little tricker, you won't find them in the symbol table. They can be looked up via the 'scratchpad' that belongs to the block of code they're defined in. Try PadWalker:
全局符号表是
%main::
,因此您可以从那里获取全局变量。 然而,每个条目都是一个 typeglob,它可以保存多个值,例如 $x、@x、%x 等,因此您需要检查每个数据类型。 您可以在此处找到执行此操作的代码。 该页面上的注释可能会帮助您找到非全局变量的其他解决方案(例如用“my”声明的词法变量)。The global symbol table is
%main::
, so you can get global variables from there. However, each entry is a typeglob which can hold multiple values, e.g., $x, @x, %x, etc, so you need to check for each data type. You can find code that does this here. The comments on that page might help you find other solutions for non-global variables (like lexical variables declared with "my").PadWalker 模块为您提供
peek_my
和peek_our
它采用 LEVEL 参数来确定在哪个范围中查找变量:下面是一个示例:
The PadWalker module gives you
peek_my
andpeek_our
which take a LEVEL argument that determines which scope to look for variables in:Here is an example:
Nathan 的回答是故事的一部分——不幸的是,故事的其余部分是词法变量没有在
%main::
或其他任何地方列出(至少可以从 Perl 访问的任何地方) -- 可能可以编写一些复杂的 XS 代码,从 Perl 的 C 级内部结构中挖掘出这些信息)。词法变量通常用于“普通局部”变量。 它们的声明如下:
Nathan's answer is part of the story -- unfortunately, the rest of the story is that lexical variables aren't listed in
%main::
or anywhere else (at least anywhere accessible from Perl -- it's probably possible to write some hairy XS code that digs this information out of Perl's C-level internals).Lexical variables are what you would normally use for "ordinary local" variables. They are declared like:
除了调试目的之外,这还有其他用途吗? 如果没有,您可能需要熟悉 perl 的调试器。 进入调试器后,您可以通过发出“V”来检查所有变量。
Would this be for anything other than debugging purposes? If not, you may want to familiarize yourself with perl's debugger. Once inside the debugger you can inspect all variables by issuing 'V'.