我正在寻找最好,最简单的方法来做类似的事情:
$var1="value"; bunch of code..... **print allVariablesAndTheirValuesCurrentlyDefined;**
解决方法
包变量?词汇变量?
可以通过符号表查找包变量.试试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);