我知道我可以列出所有的包和lexcial变量在给定的范围使用
Padwalker
的peek_our和peek_my,但是如何获得所有的全局变量的名称和值,如$“和$ /?
#!/usr/bin/perl use strict; use warnings; use PadWalker qw/peek_our peek_my/; use Data::Dumper; our $foo = 1; our $bar = 2; { my $foo = 3; print Dumper in_scope_variables(); } print Dumper in_scope_variables(); sub in_scope_variables { my %in_scope = %{peek_our(1)}; my $lexical = peek_my(1); #lexicals hide package variables while (my ($var,$ref) = each %$lexical) { $in_scope{$var} = $ref; } ############################################## #FIXME: need to add globals to %in_scope here# ############################################## return \%in_scope; }
解决方法
您可以访问符号表,签出p。 “Programming Perl”的第293页
另外看看“Mastering Perl: http://www252.pair.com/comdog/mastering_perl/
具体: http://www252.pair.com/comdog/mastering_perl/Chapters/08.symbol_tables.html
另外看看“Mastering Perl: http://www252.pair.com/comdog/mastering_perl/
具体: http://www252.pair.com/comdog/mastering_perl/Chapters/08.symbol_tables.html
您正在查找的那些变量将在主命名空间下
{ no strict 'refs'; foreach my $entry ( keys %main:: ) { print "$entry\n"; } }
你也可以做
*sym = $main::{"/"}
同样对于其他值
如果你想找到你可以做的符号的类型(从掌握perl):
foreach my $entry ( keys %main:: ) { print "-" x 30,"Name: $entry\n"; print "\tscalar is defined\n" if defined ${$entry}; print "\tarray is defined\n" if defined @{$entry}; print "\thash is defined\n" if defined %{$entry}; print "\tsub is defined\n" if defined &{$entry}; }