发现 Perl 应用程序当前定义的所有变量的最佳方法是什么?

发布于 2024-07-16 15:15:25 字数 143 浏览 4 评论 0原文

我正在寻找最好、最简单的方法来做类似的事情:

$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 技术交流群。

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

发布评论

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

评论(5

疾风者 2024-07-23 15:15:25

包变量? 词汇变量?

可以通过符号表查找包变量。 尝试 Devel::Symdump

#!/path/to/perl

use Devel::Symdump;

package example;

$var = "value";
@var = ("value1", "value2");
%var = ("key1" => "value1", "key2" => "value2");

my $obj = Devel::Symdump->new('example');

print $obj->as_string();

词法变量有点诡计,你不会发现它们在符号表中。 可以通过属于定义它们的代码块的“暂存器”来查找它们。尝试 PadWalker

#!/path/to/perl

use strict;
use warnings;

use Data::Dumper;
use PadWalker qw(peek_my);

my $var = "value";
my @var = ("value1", "value2");
my %var = ("key1" =>  "value1", "key2" => "value2");

my $hash_ref = peek_my(0);

print Dumper($hash_ref);

Package variables? Lexical variables?

Package variables can be looked up via the symbol table. Try Devel::Symdump:

#!/path/to/perl

use Devel::Symdump;

package example;

$var = "value";
@var = ("value1", "value2");
%var = ("key1" => "value1", "key2" => "value2");

my $obj = Devel::Symdump->new('example');

print $obj->as_string();

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:

#!/path/to/perl

use strict;
use warnings;

use Data::Dumper;
use PadWalker qw(peek_my);

my $var = "value";
my @var = ("value1", "value2");
my %var = ("key1" =>  "value1", "key2" => "value2");

my $hash_ref = peek_my(0);

print Dumper($hash_ref);
哽咽笑 2024-07-23 15:15:25

全局符号表是%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").

寻找一个思念的角度 2024-07-23 15:15:25

PadWalker 模块为您提供 peek_mypeek_our 它采用 LEVEL 参数来确定在哪个范围中查找变量:

The LEVEL argument is interpreted just like the argument to caller.
So peek_my(0) returns a reference to a hash of all the my variables
that are currently in scope; peek_my(1) returns a reference to a hash
of all the my variables that are in scope at the point where the 
current sub was called, and so on.

下面是一个示例:

#!/usr/bin/perl

use strict;
use warnings;

use PadWalker qw/peek_my/;

my $baz = "hi";

foo();

sub foo {
    my $foo = 5;
    my $bar = 10;

    print "I have access to these variables\n";
    my $pad = peek_my(0);
    for my $var (keys %$pad) {
        print "\t$var\n";
    }

    print "and the caller has these variables\n";
    $pad = peek_my(1);
    for my $var (keys %$pad) {
        print "\t$var\n";
    }
}

The PadWalker module gives you peek_my and peek_our which take a LEVEL argument that determines which scope to look for variables in:

The LEVEL argument is interpreted just like the argument to caller.
So peek_my(0) returns a reference to a hash of all the my variables
that are currently in scope; peek_my(1) returns a reference to a hash
of all the my variables that are in scope at the point where the 
current sub was called, and so on.

Here is an example:

#!/usr/bin/perl

use strict;
use warnings;

use PadWalker qw/peek_my/;

my $baz = "hi";

foo();

sub foo {
    my $foo = 5;
    my $bar = 10;

    print "I have access to these variables\n";
    my $pad = peek_my(0);
    for my $var (keys %$pad) {
        print "\t$var\n";
    }

    print "and the caller has these variables\n";
    $pad = peek_my(1);
    for my $var (keys %$pad) {
        print "\t$var\n";
    }
}
只有影子陪我不离不弃 2024-07-23 15:15:25

Nathan 的回答是故事的一部分——不幸的是,故事的其余部分是词法变量没有在 %main:: 或其他任何地方列出(至少可以从 Perl 访问的任何地方) -- 可能可以编写一些复杂的 XS 代码,从 Perl 的 C 级内部结构中挖掘出这些信息)。

词法变量通常用于“普通局部”变量。 它们的声明如下:

my $x;

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:

my $x;
筑梦 2024-07-23 15:15:25

除了调试目的之外,这还有其他用途吗? 如果没有,您可能需要熟悉 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'.

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